8000 darwin: give the agent a file per session by kkuehl · Pull Request #1306 · frida/frida-core · GitHub
[go: up one dir, main page]

Skip to content

darwin: give the agent a file per session - #1306

Merged
oleavr merged 1 commit into
frida:mainfrom
kkuehl:kkuehl/upstream-agent-per-session
Nov 27, 2026
Merged

oleavr merged 1 commit into
frida:mainfrom
kkuehl:kkuehl/upstream-agent-per-session

Conversation

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

A second DeviceManager, after a script has been loaded into this process through the first one, faults in the agent container thread:

thread 'frida-agent-con', EXC_BAD_ACCESS
  #0 pthread_rwlock_rdlock + 8
  #1 g_type_from_name("GTlsBackendOpenssl")       gtype.c:3222
  #2 g_type_register_static("GTlsBackendOpenssl") gtype.c:2572
  #4 g_tls_backend_openssl_register_type
  #6 _frida_agent_environment_init                agent-glue.c:77
  #7 frida_agent_runner_create_and_run            agent.vala:124
  #8 frida_agent_container_run                    agent-container.vala:119

The second session's environment init runs gio_init and the OpenSSL GIO module registration into a GLib whose type system the previous session already shut down.

Why 17.28.0

2dd9ebd2 moved the agent into the shared resource directory, so the image outlived the session that shut its GLib down; 17.27.5 had one image per session for free, because each manager had its own temporary directory. Bisected on macOS arm64:

commit three rounds
17.27.5 clean
4a421169 (before the resource-directory work) clean
c90e78ac clean
2dd9ebd2 faults
17.28.0 with a0b9b89a reverted still faults
17.28.0 with frida-gum 17.27.5 still faults

The change

AgentResource takes the session's temporary directory again, so each session writes its own agent file and maps its own image. The helper stays in the resource directory — it is stateless and safe to share, so the release's intent is kept where it is safe.

(~TemporaryFile deletes the file when it is dropped, so AgentResource keeps the reference.)

Checked

Three rounds of new DeviceManager → get_local_device → attach(0) → create_script("1;") → load → detach → close:

17.28.0          round 0 released → SIGSEGV (139)
with this change round 0, round 1, round 2 released

The same loop through frida-python 17.28.0 faults identically, so it is not specific to any binding.

Note: frida_deinit() hanging after a local attach is a separate, older issue (it hangs at 17.27.5 too) and is not touched here.

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

One update on the frida_deinit() note at the end — it is no longer an open question, and it has a cause and a fix.

_g_dbus_shutdown() waits for every GDBusConnection to be finalized, not merely closed, because a worker stays in gdbus_workers until the connection that created it goes. On a host where idund is running, idun's DeviceMonitor.stop() stops its backends but keeps them in backends; the daemon backend's client therefore outlives the stop, and with it the connection object — so that one worker never leaves the list and the wait never returns. At the breakpoint: exactly one worker left, created by idun_daemon_client_try_open_endpoint_co.

The fix is frida/idun#14 — clear the backends once the stops have completed, since a stopped monitor cannot be restarted. With it in:

17.28.0                        round 0 released → blocks in frida_deinit
+ this change                  round 1 → SIGSEGV (the crash this PR fixes)
+ this change + idun#14        round 0, 1, 2 released → deinit returned → exit 0 (3.4 s)

So the two are independent, and neither needs the other's code: this PR fixes the crash, idun#14 fixes the shutdown wait. Nothing here depends on it.

(For the record, since both are visible from this thread: the earlier bounded-shutdown PR #1307 and the frida-core wrap patch #1308 are withdrawn and closed — carrying a workaround for this turned out to be unnecessary once the connection's owner was identified.)

@oleavr oleavr left a comment •
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for digging into this!

Hmm, I think it would be better if the AgentContainer was shared among sessions. It supports multiple sessions natively, and will clean up scripts/etc. associated with a specific agent session when it goes away.

@oleavr
oleavr commented Nov 27, 2026
Copy link
Copy Markdown
Member

I'm about to cut a new release, so taking a stab at this now.

@oleavr
oleavr force-pushed the kkuehl/upstream-agent-per-session branch from 81eecc5 to 7bac33e Compare November 27, 2026 22:27
Loading the agent into this process a second time, after an earlier
container already shut down its GLib, crashed in g_type_from_name()
during environment init. This happened whenever a DeviceManager was
created after another one had attached to the system session and been
closed.

Keep one container per process instead, created on first use and torn
down by frida_shutdown() or frida_deinit(), on the main context it was
created on. Host sessions now close only their own sessions in it,
rather than the connection.
@oleavr
oleavr force-pushed the kkuehl/upstream-agent-per-session branch from 7bac33e to 160be38 Compare November 27, 2026 22:39
@oleavr
oleavr merged commit 24ac9ab into frida:main Nov 27, 2026
39 checks passed
kkuehl added a commit to kkuehl/frida-rust that referenced this pull request Nov 29, 2026
17.28.0 faulted in the agent container the first time a second device manager's
session machinery ran, and this loop was ignored so CI stayed green while the fix
was in frida-core. The fix is frida/frida-core#1306 (the agent container is shared
across host sessions), which 17.28.1 carries: the loop passes on its devkit, so
the test runs with the rest again.
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

0