NokiaTube | New address | Mode Desktop | On high quality
Skip to content

agent: Fix pending-call underflow on disconnect - #1300

Merged
oleavr merged 1 commit into
frida:mainfrom
kkuehl:fix/agent-pending-calls-underflow
Nov 23, 2026
Merged

oleavr merged 1 commit into
frida:mainfrom
kkuehl:fix/agent-pending-calls-underflow

Conversation

@kkuehl

@kkuehl kkuehl commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Closing a DeviceManager occasionally hangs the whole process (macOS). The in-process agent's worker thread never exits, so AgentContainer.destroy() blocks frida-core's only message-loop thread forever in stop_worker_thread()'s thread.join(), and every later sync call also waits.

Cause

The agent's pending_calls counter tracks incoming method calls that expect a reply; each outgoing METHOD_RETURN/ERROR decrements it, from a GDBus filter. GDBus runs outgoing filters on its worker thread as it writes each message, not when the reply is sent. So a reply that was queued before the peer vanished can pass through the filter after on_connection_closed() has reset pending_calls to zero on the agent's thread. The unsigned counter then wraps, perform_unload() waits on pending_close for calls that will never complete, never schedules main_loop.quit(), and the worker never exits.

Fix

Stop counting replies once the connection is closed. GDBus sets the closed flag on its worker thread before scheduling the closed signal, and runs filters on that same thread, so after the flag is set no decrement can happen, and every earlier one happens before the reset. The counter stays exact, rather than being clamped at zero.

Reproduction / verification

The hang is a rare (~1%) concurrency race that needs several concurrent frida-core clients repeatedly creating and closing a manager. With the frida-rust gir test binary run 4-up, 300-run batches:

  • main (17.27.4): 3 hangs (reproduced on both the released devkit and a local build)
  • the original revision of this branch (clamping the decrement at zero): 0 hangs (two 300-run batches, 0/600 total)

The branch has since been rewritten to fix the race itself instead of clamping the counter. @kkuehl, could you re-run your batches against the new revision?

GDBus runs outgoing filters on its worker thread as it writes each
message, so a reply queued before the peer vanished can pass through
our filter after on_connection_closed() has reset pending_calls to
zero. The counter then wraps, and perform_unload() waits forever for
calls that will never complete, hanging the agent's thread.

Stop counting replies once the connection is closed. The worker sets
the closed flag before scheduling the closed signal, and runs filters
on the same thread, so no decrement can land after the reset.
@oleavr
oleavr force-pushed the fix/agent-pending-calls-underflow branch from 3d1a1e4 to ab3b196 Compare November 23, 2026 15:24
@oleavr oleavr changed the title agent: Guard pending-call teardown against underflow agent: Fix pending-call underflow on disconnect Nov 23, 2026
@oleavr
oleavr merged commit 3e21a99 into frida:main Nov 23, 2026
13 of 36 checks passed
@oleavr

oleavr commented Nov 23, 2026

Copy link
Copy Markdown
Member

Thanks! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants