amphp / amphp/websocket-server

Fatal error: Uncaught AssertionError: assert($fiber !== $this->fiber) when server::stop() in Windows sapi_windows_set_ctrl_handler()

Open
#29 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
126
Forks
18
PR merge metrics
No merged PRs in 30d

Description

I adapted the last lines of the example in the README to Windows, because SIGINT (pcntl) is not available there.
I converted it this way making use of sapi_windows_set_ctrl_handler($callback) to intercept CTRL+C whilst the script is running in a terminal.

EventLoop::defer( function () use ( $logger, $server ): void {
    $logger->info( 'OS: Windows' );
    // async wait for server admin to hit CTRL+C
    sapi_windows_set_ctrl_handler( function ( int $event ) use ( $logger, $server ) {
        $logger->info( sprintf( "Received signal, stopping HTTP server" ) );
        $server->stop();
    } );
} );

EventLoop::run();

The callback is fired, the server says is stopping, but an assertion fires afterward, I don't think it successfully stops:

...
...
[2025-01-05T14:39:38.870938+00:00] server.info: OS: Windows
[2025-01-05T14:39:41.882420+00:00] server.info: Received signal, stopping HTTP server
[2025-01-05T14:39:41.882738+00:00] server.info: Stopping server

Fatal error: Uncaught AssertionError: assert($fiber !== $this->fiber) in D:\server-php\vendor\revolt\event-loop\src\EventLoop\Internal\AbstractDriver.php:293
Stack trace:
#0 D:\server-php\vendor\revolt\event-loop\src\EventLoop\Internal\AbstractDriver.php(293): assert(false, 'assert($fiber !...')
#1 D:\server-php\vendor\revolt\event-loop\src\EventLoop.php(392): Revolt\EventLoop\Internal\AbstractDriver->getSuspension()
#2 D:\server-php\vendor\amphp\amp\src\Internal\FutureIterator.php(130): Revolt\EventLoop::getSuspension()
#3 D:\server-php\vendor\amphp\amp\src\Future.php(59): Amp\Internal\FutureIterator->consume()
#4 D:\server-php\vendor\amphp\amp\src\Future\functions.php(118): Amp\Future::iterate(Array, NULL)
#5 D:\server-php\vendor\amphp\http-server\src\SocketHttpServer.php(402): Amp\Future\awaitAll(Array)
#6 D:\server-php\start-php8.php(88): Amp\Http\Server\SocketHttpServer->stop()
#7 [internal function]: {closure:D:\server-php\start-php8.php:85}(0)
#8 D:\server-php\vendor\revolt\event-loop\src\EventLoop\Driver\StreamSelectDriver.php(254): stream_select(Array, Array, Array, 0, 999642)
#9 D:\server-php\vendor\revolt\event-loop\src\EventLoop\Driver\StreamSelectDriver.php(133): Revolt\EventLoop\Driver\StreamSelectDriver->selectStreams(Array, Array, 0.9996424000019)
#10 D:\server-php\vendor\revolt\event-loop\src\EventLoop\Internal\AbstractDriver.php(489): Revolt\EventLoop\Driver\StreamSelectDriver->dispatch(true)
#11 D:\server-php\vendor\revolt\event-loop\src\EventLoop\Internal\AbstractDriver.php(552): Revolt\EventLoop\Internal\AbstractDriver->tick(true)
#12 [internal function]: Revolt\EventLoop\Internal\AbstractDriver->{closure:Revolt\EventLoop\Internal\AbstractDriver::createLoopFiber():533}()
#13 D:\server-php\vendor\revolt\event-loop\src\EventLoop\Internal\AbstractDriver.php(113): Fiber->resume()
#14 D:\server-php\vendor\revolt\event-loop\src\EventLoop.php(406): Revolt\EventLoop\Internal\AbstractDriver->run()
#15 D:\server-php\start-php8.php(106): Revolt\EventLoop::run()
#16 {main}
  thrown in D:\server-php\vendor\revolt\event-loop\src\EventLoop\Internal\AbstractDriver.php on line 293

This is not breaking the script atm but maybe it's worth taking a look into it?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the Windows README example with sapi_windows_set_ctrl_handler() and CTRL+C, then trace server->stop() through SocketHttpServer.php and the AssertionError at Revolt EventLoop's AbstractDriver.php:293. Done means the server stops cleanly after CTRL+C without the fiber assertion.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.