Conversation
Unplugging the phone while the relay is running can drop the device handle while the relay goroutines are still submitting USB transfers. gousb then crashes inside cgo (SIGSEGV in _Cfunc_submit), the daemon dies and the watcher never gets a chance to re-attach. - run all bulk transfers through ReadContext/WriteContext so they observe relay cancellation and never race with a closed handle - track the USB-facing goroutines with a WaitGroup and wait for them in Relay.Stop before the device handle is released - stop force-closing the device from a watcher goroutine; context cancellation is enough to unblock the reads - treat LIBUSB_ERROR_NO_DEVICE/NOT_FOUND as fatal for the session so a disconnect ends the relay instead of looping forever, which lets the USB watcher re-attach when the phone comes back - call relay.Stop from the attach callback error path Reproduced on macOS 26 (Apple Silicon) + vivo X200 Pro mini: pulling the cable crashed the daemon twice with SIGSEGV during cgo. With this change the disconnect is logged, the session ends gracefully and re-attaching works without restarting the daemon.
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
Unplugging the phone while the daemon is running can take the whole process down:
*gousb.Devicehandle after the attach callback returns, while relay goroutines may still be inside gousb transfers;dev.Close()from a watcher goroutine races with in-flight cgo transfers (gousb._Cfunc_submit), producingSIGSEGV: signal arrived during cgo execution.Observed twice on macOS 26 (Apple Silicon) + vivo X200 Pro mini:
Relay session ended: USB device disconnectedis followed by a SIGSEGV dump, launchd restarts the daemon, and the session is stuck until it does.Changes
ReadContext/WriteContextso they observe relay cancellation and never race with a closed handle.sync.WaitGroup;Relay.Stopwaits for them to finish (2s cap) before the device handle is released.LIBUSB_ERROR_NO_DEVICE/NOT_FOUND(andLIBUSB_TRANSFER_NO_DEVICE) as fatal for the session so a disconnect ends the relay instead of looping forever, letting the USB watcher re-attach when the phone comes back.relay.Stop()from the daemon's session-end path so the handle is only released once no transfers are in flight.Testing
go vet ./...,go build ./...clean.Note: this branch alone does not make the vivo X200 Pro mini work end to end — the companion PR fixes the framing/filter side.