fix(loader): avoid background dlclose at shutdown - #4229
Conversation
The telemetry reaper pins the loader with dlopen() so it can keep waiting after Zend shuts down. Its eventual dlclose() can overlap glibc's atexit handlers on another thread and race libdatadog teardown. Tail-calling dlclose protects the reaper's return address, but does not serialize these destructor paths. Joining the thread during module shutdown avoids that race but makes short-lived CLI processes wait for telemetry delivery. Keeping the loader permanently mapped would preserve stale state across Apache reloads. Copy a small x86-64 or AArch64 reaper and its context into a separate executable mapping before fork. The detached thread waits for its child with EINTR retries, then tail-calls libc's munmap() to release the mapping and return directly to pthread cleanup. It no longer pins the loader or calls dlclose(), so Zend can unload the library while telemetry is still running. Clean up preparations in the child and on fork failures. Skip telemetry if setup fails, reap synchronously if thread creation fails, and use _exit() if exec fails to avoid inherited exit handlers. Preserve ELF hardening properties and enable BTI on the copied page when the AArch64 kernel reports support. Restore the shutdown regression with a gated forwarder: PHP must exit before the test releases telemetry children. Validation: built the loader and ran the restored functional test on native AArch64 and ubuntu-vm amd64. Reaper checks also covered EINTR, unload/reload, setup failures, CET/BTI/GCS properties and BTI enforcement under QEMU.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
❌ ErrorsYour PR has failed checks. Please review the issues below and take necessary action before merging. 🚦 9 Pipeline jobs failed
ℹ️ InfoNo other issues found (see more)❄️ No new flaky tests detected 🎯 Code Coverage (details) Useful? React with 👍 / 👎 This comment will be updated automatically if new data arrives.🔗 Commit SHA: dfd83d0 | Docs | View more details | Give us feedback! |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 78ce2d7201
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
I approve of the approach of that PR. I'll look at it more in depth later. |
Avoiding the asm trampoline
|
@cataphract The idea is right, but the implementation far more complex than needed: https://github.com/DataDog/dd-trace-php/compare/bob/ssi-shutdown-crash?expand=1 If you like this, I'll cherry-pick that onto this PR. |
I'm OK with it. |
bwoebi
left a comment
There was a problem hiding this comment.
Pushed my commit then and acking.
|
Thanks for setting up this patch :-) |
Benchmarks [ tracer ]Benchmark execution time: 2026-09-25 19:32:39 Comparing candidate commit dfd83d0 in PR branch Found 1 performance improvements and 0 performance regressions! Performance is the same for 192 metrics, 1 unstable metrics.
|
The telemetry reaper pins the loader with dlopen() so it can keep waiting after Zend shuts down. Its eventual dlclose() can overlap glibc's atexit handlers on another thread and race libdatadog teardown.
But if we don't issue dlopen(), then we don't need to call dlclose(), and the problem goes away.
So copy a small x86-64 or AArch64 reaper and its context into a separate executable mapping before fork. The detached thread waits for its child with EINTR retries, then tail-calls libc's munmap() to release the mapping and return directly to pthread cleanup. It no longer pins the loader or calls dlclose(), so Zend can unload the library while telemetry is still running.
Validation: built the loader and ran the restored functional test on native AArch64 and ubuntu-vm amd64. Reaper checks also covered EINTR, unload/reload, CET/BTI/GCS properties and BTI enforcement under QEMU.
Description
Reviewer checklist