libimobiledevice / libirecovery

Library and utility to talk to iBoot/iBSS via USB on Mac OS X, Windows, and Linux
https://libimobiledevice.org
GNU Lesser General Public License v2.1
550 stars 207 forks source link

Fix racecondition in even handler shutdown #84

Closed tihmstar closed 3 years ago

tihmstar commented 3 years ago

There is a racecondition when you call irecv_device_event_unsubscribe shortly after irecv_device_event_subscribe. If the _irecv_event_handler thread didn't reach CFRunLoopRun(); before irecv_device_event_unsubscribe calls CFRunLoopStop(), the call doesn't actually stop the runloop and then irecv_device_event_unsubscribe hangs forever at thread_join(th_event_handler);.

This patch makes sure that CFRunloop is actually idle, before calling CFRunLoopStop.

nikias commented 3 years ago

Didn't work with my test case which is literally just

irecv_device_event_context_t context = NULL;
irecv_device_event_subscribe(&context, irecv_device_cb, NULL);
irecv_device_event_unsubscribe(context);

So I came up with a different solution that makes sure irecv_device_event_subscribe() doesn't return before the thread has started and reached a certain point (right before CFRunLoopRun() in case of macOS).

See commit 5718dd2ae149f615191552eef40f8f90e60a3455.

tihmstar commented 3 years ago

Doesn't that hide the issue even more? I mean the race is still there.