Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stack trace to all current suspensions on early exit #46

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/EventLoop/Internal/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ public function getSuspension(): Suspension
return $this->suspensions[$fiber ?? $this] ??= new DriverSuspension(
$this->runCallback,
$this->queueCallback,
$this->interruptCallback
$this->interruptCallback,
$this->suspensions
);
}

Expand Down
39 changes: 37 additions & 2 deletions src/EventLoop/Internal/DriverSuspension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@ final class DriverSuspension implements Suspension

private bool $pending = false;

private \WeakReference $suspensions;

/**
* @param \Closure $run
* @param \Closure $queue
* @param \Closure $interrupt
*
* @internal
*/
public function __construct(\Closure $run, \Closure $queue, \Closure $interrupt)
public function __construct(\Closure $run, \Closure $queue, \Closure $interrupt, \WeakMap $suspensions)
{
$this->run = $run;
$this->queue = $queue;
$this->interrupt = $interrupt;
$this->fiber = \Fiber::getCurrent();
$this->suspensions = \WeakReference::create($suspensions);
}

public function resume(mixed $value = null): void
Expand Down Expand Up @@ -87,7 +90,22 @@ public function suspend(): mixed
$this->pending = false;
$result && $result(); // Unwrap any uncaught exceptions from the event loop

throw new \Error('Event loop terminated without resuming the current suspension');
$info = '';
$suspensions = $this->suspensions->get();
if ($suspensions) {
\gc_collect_cycles();

foreach ($suspensions as $suspension) {
if ($suspension->fiber === null) {
continue;
}

$reflectionFiber = new \ReflectionFiber($suspension->fiber);
$info .= "\n\n" . $this->formatStacktrace($reflectionFiber->getTrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
}
}

throw new \Error('Event loop terminated without resuming the current suspension:' . $info);
}

return $result();
Expand All @@ -108,4 +126,21 @@ public function throw(\Throwable $throwable): void
($this->interrupt)(static fn () => throw $throwable);
}
}

private function formatStacktrace(array $trace): string
{
return \implode("\n", \array_map(static function ($e, $i) {
$line = "#{$i} ";

if (isset($e["file"])) {
$line .= "{$e['file']}:{$e['line']} ";
}

if (isset($e["class"], $e["type"])) {
$line .= $e["class"] . $e["type"];
}

return $line . $e["function"] . "()";
}, $trace, \array_keys($trace)));
}
}