Skip to content

Commit

Permalink
Move debug traces from local scope storage to debug scope properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevay committed Jan 11, 2024
1 parent c35e88b commit af9c04a
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/Context/DebugScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@
*/
final class DebugScope implements ScopeInterface
{
private const DEBUG_TRACE_CREATE = '__debug_trace_create';
private const DEBUG_TRACE_DETACH = '__debug_trace_detach';

private static bool $shutdownHandlerInitialized = false;
private static bool $finalShutdownPhase = false;

private ContextStorageScopeInterface $scope;
private ?int $fiberId;
private array $createdAt;
private ?array $detachedAt = null;

public function __construct(ContextStorageScopeInterface $node)
public function __construct(ContextStorageScopeInterface $scope)
{
$this->scope = $node;
$this->scope[self::DEBUG_TRACE_CREATE] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$this->scope = $scope;
$this->fiberId = self::currentFiberId();
$this->createdAt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

if (!self::$shutdownHandlerInitialized) {
self::$shutdownHandlerInitialized = true;
Expand All @@ -42,15 +41,15 @@ public function __construct(ContextStorageScopeInterface $node)

public function detach(): int
{
$this->scope[self::DEBUG_TRACE_DETACH] ??= debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$this->detachedAt ??= debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

$flags = $this->scope->detach();

if (($flags & ScopeInterface::DETACHED) !== 0) {
trigger_error(sprintf(
'Scope: unexpected call to Scope::detach() for scope #%d, scope was already detached %s',
spl_object_id($this),
self::formatBacktrace($this->scope[self::DEBUG_TRACE_DETACH]),
self::formatBacktrace($this->detachedAt),
));
} elseif (($flags & ScopeInterface::MISMATCH) !== 0) {
trigger_error(sprintf(
Expand All @@ -69,7 +68,7 @@ public function detach(): int

public function __destruct()
{
if (!isset($this->scope[self::DEBUG_TRACE_DETACH])) {
if (!$this->detachedAt) {
// Handle destructors invoked during final shutdown
// DebugScope::__destruct() might be called before fiber finally blocks run
if (self::$finalShutdownPhase && $this->fiberId !== self::currentFiberId()) {
Expand All @@ -79,7 +78,7 @@ public function __destruct()
trigger_error(sprintf(
'Scope: missing call to Scope::detach() for scope #%d, created %s',
spl_object_id($this->scope),
self::formatBacktrace($this->scope[self::DEBUG_TRACE_CREATE]),
self::formatBacktrace($this->createdAt),
));
}
}
Expand Down

0 comments on commit af9c04a

Please sign in to comment.