Repository navigation
Fix hang in usbmuxd_events_unsubscribe() right after subscribing - #158
Closed
Sorvigolova wants to merge 1 commit into
Closed
Sorvigolova wants to merge 1 commit into
Sorvigolova wants to merge 1 commit into
Conversation
If usbmuxd_events_unsubscribe() is called before the device monitor thread has connected to usbmuxd, listenfd is still -1, so there is nothing to shut down. Where thread_cancel() is not available (Windows, or builds without pthread_cancel) unsubscribe then relies on clearing `running`, but device_monitor() sets `running = 1` and `cancelling = 0` itself when it starts and overwrites that. The thread goes on to connect, sends Listen and blocks in receive_packet() forever, so thread_join() never returns. This is easy to hit with ideviceinstaller on Windows with a network device and redirected stdout: the command completes before idevice_wait_for_command_to_complete() subscribes, so subscribe and unsubscribe happen back to back and the tool hangs on exit. Fix this by: - initializing `running`/`cancelling` in usbmuxd_events_subscribe() under listener_mutex before the monitor thread is created; - publishing listenfd in device_monitor() under the same mutex, and only if no cancellation was requested in the meantime (otherwise close the new socket and exit); - setting `cancelling`/`running` and shutting down listenfd under the mutex in usbmuxd_events_unsubscribe(), and only if the socket exists.
Member
|
Merged with e478ab4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
usbmuxd_events_unsubscribe()can hang forever inthread_join()when it iscalled shortly after
usbmuxd_events_subscribe(), before the device monitorthread has connected to usbmuxd.
Sequence:
usbmuxd_events_subscribe()startsdevice_monitor().usbmuxd_events_unsubscribe()runs before the thread has connected:listenfdis still-1, sosocket_shutdown()does nothing.thread_cancel()returns-1on Windows (and withoutpthread_cancel),so unsubscribe sets
running = 0and callsthread_join().device_monitor()starts and setsrunning = 1andcancelling = 0,overwriting the cancel request. It then connects, sends
Listenand blocksin
receive_packet()waiting for events that never come.thread_join()never returns.How it shows up
ideviceinstaller on Windows with a network (Wi-Fi) device hangs when stdout is
redirected, e.g.
The same command without redirection works, and over USB it works either way.
With fast output to a file the browse command completes before
idevice_wait_for_command_to_complete()subscribes to device events, sosubscribe and unsubscribe run back to back and hit the race. This also affects
GUI front-ends that read the tools' output through a pipe.
Fix
runningandcancellingare initialized byusbmuxd_events_subscribe()under
listener_mutexbefore the thread is created, instead of by thethread itself.
device_monitor()publisheslistenfdunder the same mutex, and only if nocancellation was requested in the meantime. Otherwise it closes the new
socket and exits.
usbmuxd_events_unsubscribe()setscancelling/runningand shuts downlistenfdunder the mutex, and only if the socket exists.So either unsubscribe sees the published socket and shuts it down, or the
thread sees
cancellingbefore it starts waiting. There is no window inbetween.
Testing
Reproducer: a loop of
usbmuxd_events_subscribe()immediately followed byusbmuxd_events_unsubscribe(), against a minimal fake usbmuxd that answersListenafter a short delay and then stays silent. Built on Linux with glueand libusbmuxd configured without
pthread_cancel, sothread_cancel()behaves as on Windows.
Also tested on Windows (MSYS2/MinGW64 build) with ideviceinstaller and a
network device:
ideviceinstaller -n ... list > out.txtno longer hangs.Behavior over USB and with a console is unchanged.
Reproducer used for the table above:
race_test.c
fake_usbmuxd.py