ISSUE-4073: Fix RPC app crash on BLE/USB disconnect (fixes #4073)#4354
Draft
herbenderbler wants to merge 1 commit intoflipperdevices:devfrom
Draft
ISSUE-4073: Fix RPC app crash on BLE/USB disconnect (fixes #4073)#4354herbenderbler wants to merge 1 commit intoflipperdevices:devfrom
herbenderbler wants to merge 1 commit intoflipperdevices:devfrom
Conversation
When transport disconnects while an RPC app is running, the session teardown could free RpcAppSystem while the app thread was still inside rpc_system_app_confirm() -> rpc_send(), causing use-after-free and furi_check crash. - Add rpc_session_wait_send_done() to wait for in-flight rpc_send to complete (via session callbacks_mutex). - In rpc_system_app_free(), call rpc_session_wait_send_done() before freeing RpcAppSystem so no thread uses it after free. Apps already receive RpcAppEventTypeSessionClose and exit gracefully; this change only ensures teardown does not free the context until in-flight sends are done.
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.
Description
Fixes #4073 — RPC app crash and lost pairing when BLE (or USB) disconnects while an app is running in RPC mode (e.g. Sub-GHz started from a companion app).
Root cause
On disconnect, the transport calls
rpc_session_close(). Session teardown runsrpc_system_app_free(), which notifies the app withRpcAppEventTypeSessionCloseand then freesRpcAppSystem. The app thread could still be insiderpc_system_app_confirm()→rpc_send(), leading to use-after-free ofrpc_appandrpc_app->sessionand afuri_checkcrash.Solution
rpc_session_wait_send_done(RpcSession*)(inrpc.c): Acquires and releases the sessioncallbacks_mutex. Becauserpc_send()uses the same mutex, this ensures no thread is in the middle of an RPC send when it returns.rpc_system_app_free()(inrpc_app.c): After the app clears its callback (and the existingwhile(rpc_app->callback)loop exits), we callrpc_session_wait_send_done(rpc_app->session)beforefree(rpc_app), so we never free the app context while a send is in progress.Existing behavior is unchanged: apps still get
RpcAppEventTypeSessionClose, clear their callback, and stop (SubGhz, NFC, LFRFID, etc. already handle this). The fix only orders teardown so in-flight sends complete before the context is freed.Verification
./fbt).Checklist (For Reviewer)