Possible race condition leading to pthread_cancels being lost

Number:rdar://31131997 Date Originated:2017-03-18
Status:Open Resolved:
Product:macOS + SDK Product Version:10.12.3
Classification: Reproducible:Always
Performance (slow, hung)

Executing pthread_cancel shortly after pthread_create might lead to the cancel request being lost. This is in violation of POSIX, as by the time pthread_create returns, use of the pthread_t in pthread_cancel should be valid.

Steps to Reproduce:
1. Compile the attached cancel.c file
2. Run following shell command 
for i in `seq 100000`; do ./a.out $i ; done && echo ' All is well!'

Expected Results:
Shell command will count to 100000 and finish with a "100000 All is well!"

Actual Results:
The command will hang, usually before $i reached 1000, because the cancellation request was lost.

LLDB inspection shows that the thread is blocking on read as expected. If the user call pthread_cancel(pthread) manually on main's frame, the thread is cancelled. So it seems, there's a race between pthread_create and pthread_cancel.

macOS Sierrra v10.12.3
/usr/lib/system/libsystem_pthread.dylib (compatibility version 1.0.0, current version 218.30.1)


macOS Sierrra v10.12.3
/usr/lib/system/libsystem_pthread.dylib (compatibility version 1.0.0, current version 218.30.1)

'cancel.c' was successfully uploaded.

See https://gist.github.com/a3f/0fe444521feca3096fc91e0bfddc1a2c


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!