Repository navigation
fix(win): stop Disconnect from freezing the UI, and a 100% CPU spin on link loss - #21
Open
ssuyash3000 wants to merge 1 commit into
Open
ssuyash3000 wants to merge 1 commit into
ssuyash3000 wants to merge 1 commit into
Conversation
…n link loss Three related problems around teardown on Windows: 1. `BluetoothWrapper::disconnect()` took `_connectorMtx` before closing the socket, but a worker thread holds that same lock while blocked in `recv()`. Closing the socket is what releases that thread, so the UI thread waited on the lock — Disconnect froze the window until the 2.5s recv timeout expired, and closing the app while a read was in flight hung it. On Windows, close first, then take the lock; other platforms keep the old order. 2. `_socket` was written by the UI thread (disconnect) while a worker thread read it inside `send()`/`recv()` — a data race that could hand `recv()` a closed or recycled handle. It is now `std::atomic<SOCKET>`, swapped through a single `_closeSocket()`. 3. `recv()` returning 0 (graceful close by the headphones) was treated as a successful read of an empty buffer, so `BluetoothWrapper::_readMessage()` looped forever looking for a start marker — one core pegged until the app was killed. Both the connector and the wrapper now treat it as a disconnect. Also loops `send()` until the whole frame is out: a partial send left the device waiting for the rest of a command that never arrived. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This branch has not been deployed
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.
Three related problems around teardown on Windows:
BluetoothWrapper::disconnect()took_connectorMtxbefore closing thesocket, but a worker thread holds that same lock while blocked in
recv().Closing the socket is what releases that thread, so the UI thread waited on
the lock — Disconnect froze the window until the 2.5s recv timeout expired,
and closing the app while a read was in flight hung it. On Windows, close
first, then take the lock; other platforms keep the old order.
_socketwas written by the UI thread (disconnect) while a worker threadread it inside
send()/recv()— a data race that could handrecv()aclosed or recycled handle. It is now
std::atomic<SOCKET>, swapped througha single
_closeSocket().recv()returning 0 (graceful close by the headphones) was treated as asuccessful read of an empty buffer, so
BluetoothWrapper::_readMessage()looped forever looking for a start marker — one core pegged until the app
was killed. Both the connector and the wrapper now treat it as a
disconnect.
Also loops
send()until the whole frame is out: a partial send left thedevice waiting for the rest of a command that never arrived.