Skip to content

Commit

Permalink
fix: Host Scopes not being removed (#2624)
Browse files Browse the repository at this point in the history
close Root Scopes on Flush
  • Loading branch information
PROFeNoM authored Apr 15, 2024
1 parent 871f9d7 commit f6dab57
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/DDTrace/ScopeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public function getActive()
return null;
}

private function deactivateHostRoot(Scope $scope)
{
$i = array_search($scope, $this->hostRootScopes, true);
if (false !== $i) {
array_splice($this->hostRootScopes, $i, 1);
}
}

public function deactivate(Scope $scope)
{
$i = array_search($scope, $this->scopes, true);
Expand All @@ -82,6 +90,10 @@ public function deactivate(Scope $scope)
if ($span instanceof Span && ObjectKVStore::get($span->internalSpan, 'ddtrace_scope_activated') !== null) {
ObjectKVStore::put($span->internalSpan, 'ddtrace_scope_activated', false);
}

if ($span->getContext()->isHostRoot()) {
$this->deactivateHostRoot($scope);
}
}

/** @internal */
Expand Down
1 change: 1 addition & 0 deletions src/DDTrace/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ public function flush()
}

$this->transport->send($this);
$this->scopeManager->close();
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/GlobalTracerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@

final class GlobalTracerTest extends BaseTestCase
{
private static function startTracingTest() {
$tracer = GlobalTracer::get();
GlobalTracer::set($tracer);
$tracer->startRootSpan('foo');

GlobalTracer::get()->getRootScope()->close();
GlobalTracer::get()->flush();
}

public function testMemoryResetAfterFlush()
{
self::putenv('DD_TRACE_GENERATE_ROOT_SPAN=0');
dd_trace_internal_fn('ddtrace_reload_config');

try {
self::startTracingTest();
$mem1 = memory_get_usage();
self::startTracingTest();
$mem2 = memory_get_usage();
} catch (\Throwable $e) {
self::putenv('DD_TRACE_GENERATE_ROOT_SPAN=');
throw $e;
} finally {
$this->assertEquals($mem1, $mem2);
self::putenv('DD_TRACE_GENERATE_ROOT_SPAN=');
}
}

public function testDDTracerIsSetAsIs()
{
$tracer = new Tracer(new Noop());
Expand Down

0 comments on commit f6dab57

Please sign in to comment.