Skip to content

Finished profiled transactions keep their Excimer thread until a GC cycle runs #2423

In review

Description

@Mohammad-Alavi

How do you use Sentry?

Sentry SaaS (sentry.io)

SDK version

4.18.0, and also reproduced on 4.37.0 (4.38.0 not tested). PHP 8.3.36, ext-excimer 1.2.6, Linux.

Steps to reproduce

Run with php leak.php <dir with sentry/sentry and guzzlehttp/guzzle installed>:

<?php
require $argv[1].'/vendor/autoload.php';
function thr(){ preg_match('/Threads:\s+(\d+)/', file_get_contents('/proc/self/status'), $m); return (int)$m[1]; }
\Sentry\init(['dsn'=>'https://public@o0.ingest.sentry.io/0','traces_sample_rate'=>1.0,'profiles_sample_rate'=>1.0,'transport'=>null]);
$hub=\Sentry\SentrySdk::getCurrentHub();
for($i=0;$i<50;$i++){ $c=\Sentry\Tracing\TransactionContext::make()->setName('job')->setOp('queue.process'); $t=$hub->startTransaction($c); $hub->setSpan($t); $s=$t->startChild(\Sentry\Tracing\SpanContext::make()->setOp('db')); usleep(2000); $s->finish(); $t->finish(); $hub->setSpan(null); unset($t,$s); }
echo \Composer\InstalledVersions::getPrettyVersion('sentry/sentry')." after50=".thr();
gc_collect_cycles(); echo " after_gc=".thr()."\n";

Expected result

The thread count stays flat once each transaction has finished and been dropped.

Actual result

4.18.0 after50=51 after_gc=1
4.37.0 after50=51 after_gc=1

Each profiled transaction leaves one thread behind. The thread goes away only when PHP's cycle collector runs. A standalone ExcimerProfiler that is started, stopped and unset frees its thread at once, so the extra lifetime looks like it comes from the transaction and its spans referencing each other. Profiler::stop() stops the timer but keeps the ExcimerProfiler, and Transaction::finish() keeps the Profiler.

In a long-running worker (Laravel queue:work with sentry-laravel's queue integration) the automatic collector does not always run often enough. In a container limited to 256 processes and threads, the worker reached the limit, and from then on every ExcimerProfiler::start() failed:

ErrorException: ExcimerProfiler::start(): Error in pthread_create(): Resource temporarily unavailable

Laravel turns that warning into an exception, so every queued job failed until the worker restarted. Over 600 simulated jobs, the collector ran twice in the first 150, then not at all, and the thread count stayed at the limit.

Possible fixes: release the profiler when the transaction finishes (for example via detachProfiler(), after the profile has been read), or break the transaction and span reference cycle.

Activity

  1. linear-code commented on Nov 24, 2026

    @linear-code
  2. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Nov 24, 2026
  3. stayallive commented on Nov 24, 2026

    @stayallive
    Collaborator

    I've opened #2424 to hopefully resolve this. There was indeed a circular reference causing it to hold on to the thread longer then needed. If you can test this patch that would be great to verify it's doing it correct now.

  4. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Nov 24, 2026
  5. Mohammad-Alavi commented on Nov 24, 2026

    @Mohammad-Alavi
    Author

    Thanks for the quick fix. I tested #2424 with the repro from the issue and the leak is gone.

    I took a clean 4.38.0 install, replaced src/Profiling/Profiler.php with the file from #2424 (56a9c45; the diff against 4.38.0 is only the PR's lines), and ran the repro from the issue, in a variant that starts each transaction with \Sentry\startTransaction() and keeps the default transport (50 profiled transactions, PHP 8.3.36, ext-excimer 1.2.6):

    4.38.0 unpatched:   after50=51 after_gc=1
    4.38.0 + #2424:     after50=1  after_gc=1
    

    The thread count now stays flat without a GC run.

  6. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions