8000 frida-glue: don't block forever in GLib's shutdown (workaround, not the fix) by kkuehl · Pull Request #1307 · frida/frida-core · GitHub
[go: up one dir, main page]

Skip to content

frida-glue: don't block forever in GLib's shutdown (workaround, not the fix) - #1307

Closed
kkuehl wants to merge 1 commit into
frida:mainfrom
kkuehl:kkuehl/fix-deinit-hang
Closed

kkuehl wants to merge 1 commit into
frida:mainfrom
kkuehl:kkuehl/fix-deinit-hang

Conversation

@kkuehl
@kkuehl kkuehl commented Nov 27, 2026 •
Copy link
Copy Markdown
Contributor

This is a workaround. It makes frida_deinit() return; it does not identify or release the reference that keeps GLib's D-Bus worker alive.

The hang

frida_deinit() → gio_shutdown() joins the shared GDBus worker thread, and that worker exits only when the last GDBusConnection object is finalized. After a local attach one connection never finalizes, so the join never returns.

Reproduced on macOS arm64 at 17.27.5 and 17.28.0 alike (not a regression of this release), with a three-line host (DeviceManager::new() → local device → attach(0) → create_script("1;") → load → detach → close() → frida_deinit()) and with frida's own frida-core-example attaching to itself.

What the reference hunt produced

An lldb breakpoint on g_object_ref/g_object_unref, filtered to GDBusConnection by checking the object's GType from a Python callback, logs every reference with its call site:

agent connection   43 refs / 43 unrefs   balanced
idun connection    19 refs / 18 unrefs   one unaccounted for
                   created by idun_daemon_client_try_open_endpoint_co

That idun leak is real and fixed separately in frida/idun#13 — but it is not this hang: measured with that fix and no other change, the host still blocks in frida_deinit().

What this change does

GLib's shutdown runs on its own thread; the caller waits five seconds, and if it has not finished, gives up and skips gio_deinit(), which runs the very same shutdown.

before:  round 0 → blocks in frida_deinit (killed by timeout)
after:   round 0 → deinit returned → exit 0 (after the 5 s)

Cost, stated plainly: one thread left behind at process exit, and a half-shut-down GLib in that process. It is a safety net for a teardown that must return, not a fix.

What a real fix needs

Find the remaining unaccounted reference — the idun one is removed, so it is something else (the agent's connection balances, its entry is finalized, and its closed handler never runs) — and release it, after which GLib's shutdown completes normally. I am happy for this PR to be rejected if carrying a workaround is worse than carrying the hang.

`frida_deinit()` blocks after a local attach: `gio_shutdown()` joins the shared
GDBus worker thread, and that worker exits only once the last `GDBusConnection`
object is finalized. A connection can outlive its session — after attaching to
this process the agent's connection keeps one reference that nothing in frida's
own bookkeeping releases — so the join never returns and the host hangs in a
teardown call that is supposed to finish.

Measured on macOS arm64 at 17.27.5 and 17.28.0 alike: `frida-core-example`
attaching to itself, and a three-line host that creates a manager, attaches to
pid 0, loads a script and tears everything down, both block in
`_g_dbus_shutdown` forever. The agent's own environment deinit completes; the
block is the host's.

Wait for the shutdown, but not forever: it runs on its own thread and the caller
gives up after five seconds, leaving the thread to the process. `gio_deinit()`
later runs the same shutdown, so it is skipped in that case.

With this, both harnesses print `deinit returned` and exit 0; the reporter that a
connection is still referenced is a separate question from a teardown that can
hang forever.
@kkuehl
kkuehl commented Nov 27, 2026
Copy link
Copy Markdown
Contributor Author

Closing this: the root cause is found, and this workaround is no longer wanted.

The connection that kept GLib's D-Bus worker alive was idun's, not frida-core's: DaemonClient.try_open_endpoint() creates a connection, then asks it for the service object, and when there is no daemon to talk to — the normal case on a host with no iOS device — that step throws and the connection is left behind, unowned. An lldb breakpoint on g_object_ref/g_object_unref filtered to GDBusConnection showed it ending with one reference unaccounted for (19/18) while every tracked connection balanced exactly.

The real fix is frida/idun#13: close that connection on the failure path. Verified on macOS here: with it, frida_deinit() returns and the harness exits 0 — with no bounded wait, i.e. GLib's shutdown completes normally.

frida-core will pick it up by bumping subprojects/idun.wrap once idun has it. Thanks!

@kkuehl kkuehl closed this Nov 27, 2026
@kkuehl kkuehl reopened this Nov 27, 2026
@kkuehl
kkuehl force-pushed the kkuehl/fix-deinit-hang branch from 27da923 to 80edc2a Compare November 27, 2026 15:47
@kkuehl kkuehl changed the title frida-glue: don't block forever in GLib's shutdown subprojects: close idun's daemon connection when there is no service Nov 27, 2026
@kkuehl kkuehl changed the title subprojects: close idun's daemon connection when there is no service frida-glue: don't block forever in GLib's shutdown (workaround, not the fix) Nov 27, 2026
@kkuehl
kkuehl force-pushed the kkuehl/fix-deinit-hang branch from 80edc2a to 27da923 Compare November 27, 2026 15:52
@kkuehl
kkuehl commented Nov 27, 2026
Copy link
Copy Markdown
Contributor Author

Closing this — the root cause is found and the real fix is #1308 (with the origin in frida/idun#14), so the bounded shutdown is not needed.

What the workaround was hiding: _g_dbus_shutdown() waits for every GDBusConnection to be finalized. One connection — created by idun_daemon_client_try_open_endpoint_co — was kept alive by DeviceMonitor's stopped-but-not-released backends, so it never finalized. Releasing the backends on stop makes frida_deinit() return in 3.4 s without any wait, GLib shutdown included.

Thanks to everyone who looked at it.

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.

1 participant

0