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

Register shutdown function to clear event loop #101

Closed
wants to merge 2 commits into from
Closed
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
36 changes: 36 additions & 0 deletions examples/shutdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php';

use Revolt\EventLoop;

\register_shutdown_function(function (): void {
\register_shutdown_function(function (): void {
EventLoop::defer(function (): void {
print 'Shutdown function registered within pre-loop-run shutdown function' . PHP_EOL;
});
});


EventLoop::defer(function (): void {
print 'Shutdown function registered before EventLoop::run()' . PHP_EOL;
});
});

EventLoop::run();

\register_shutdown_function(function (): void {
\register_shutdown_function(function (): void {
EventLoop::defer(function (): void {
print 'Shutdown function registered within post-loop-run shutdown function' . PHP_EOL;
});
});

EventLoop::defer(function (): void {
print 'Shutdown function registered after EventLoop::run()' . PHP_EOL;
});
});

print 'End of script' . PHP_EOL;
31 changes: 29 additions & 2 deletions src/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ final class EventLoop
public static function setDriver(Driver $driver): void
{
/** @psalm-suppress RedundantPropertyInitializationCheck, RedundantCondition */
if (isset(self::$driver) && self::$driver->isRunning()) {
throw new \Error("Can't swap the event loop driver while the driver is running");
if (isset(self::$driver)) {
if (self::$driver->isRunning()) {
throw new \Error("Can't swap the event loop driver while the driver is running");
}
} else {
\register_shutdown_function(self::onShutdown(...), 0);
}

try {
Expand Down Expand Up @@ -67,6 +71,29 @@ protected function now(): float
}
}

private static function onShutdown(int $invocationCount): void
{
\gc_collect_cycles();

$driver = self::getDriver();

$pending = false;
foreach ($driver->getIdentifiers() as $identifier) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this also check for pending microtasks/queued callbacks?

Copy link
Member Author

@trowski trowski Dec 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. I guess AbstractDriver::isEmpty() also isn't doing that and maybe should (though that function is private and due to how the queue is invoked, I'm not sure there ever can be queued microtasks when isEmpty() is called).

Unfortunately there is nothing on Driver which will let me query if there are queued microtasks, so that would need to be added.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my code, I just unconditionally do

                if (!EventLoop::getDriver()->isRunning()) {
                        $delay = PHP_SAPI === 'cli' ? 5 : 1;
                        $id2 = null;
                        EventLoop::unreference($id1 = EventLoop::delay((float)$delay, static function () use ($delay, &$id2): void {
                                $t = time() - $delay;
                                EventLoop::unreference($id2 = EventLoop::repeat(1.0, static function () use ($t): void {
                                        $t = time() - $t;
                                        Logger::get()->error("Still waiting for shutdown of the event loop for {$t} seconds...");
                                }));
                        }));
                        EventLoop::run();
                        EventLoop::cancel($id1);
                        if ($id2 !== null) {
                                EventLoop::cancel($id2);
                        }
                }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e. an unconditional run()

if ($driver->isEnabled($identifier) && $driver->isReferenced($identifier)) {
$pending = true;
break;
}
}

if ($pending) {
$driver->run();
}

if (!$invocationCount++ || $pending) {
\register_shutdown_function(self::onShutdown(...), $invocationCount);
}
}

/**
* Queue a microtask.
*
Expand Down
Loading