Bug #81254 [NEW]: PHP8.1: undefined method Fiber::this()

From: Date: Mon, 12 Jul 2021 21:04:48 +0000
Subject: Bug #81254 [NEW]: PHP8.1: undefined method Fiber::this()
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-234991@lists.php.net to get a copy of this message
From:             rainer dot jung at kippdata dot de
Operating system: Linux, Solaris Sparc
PHP version:      8.1.0alpha3
Package:          Unknown/Other Function
Bug Type:         Bug
Bug description:PHP8.1: undefined method Fiber::this()

Description:
------------
I tried the second example from

https://wiki.php.net/rfc/fibers

The script fails, details see below.

Test script:
---------------
<?php
class EventLoop
{
    private string $nextId = 'a';
    private array $deferCallbacks = [];
    private array $read = [];
    private array $streamCallbacks = [];

    public function run(): void
    {
        while (!empty($this->deferCallbacks) || !empty($this->read)) {
            $defers = $this->deferCallbacks;
            $this->deferCallbacks = [];
            foreach ($defers as $id => $defer) {
                $defer();
            }

            $this->select($this->read);
        }
    }

    private function select(array $read): void
    {
        $timeout = empty($this->deferCallbacks) ? null : 0;
        if (!stream_select($read, $write, $except, $timeout, $timeout))
{
            return;
        }

        foreach ($read as $id => $resource) {
            $callback = $this->streamCallbacks[$id];
            unset($this->read[$id], $this->streamCallbacks[$id]);
            $callback($resource);
        }
    }

    public function defer(callable $callback): void
    {
        $id = $this->nextId++;
        $this->deferCallbacks[$id] = $callback;
    }

    public function read($resource, callable $callback): void
    {
        $id = $this->nextId++;
        $this->read[$id] = $resource;
        $this->streamCallbacks[$id] = $callback;
    }
}

[$read, $write] = stream_socket_pair(
    stripos(PHP_OS, 'win') === 0 ? STREAM_PF_INET : STREAM_PF_UNIX,
    STREAM_SOCK_STREAM,
    STREAM_IPPROTO_IP
);

// Set streams to non-blocking mode.
stream_set_blocking($read, false);
stream_set_blocking($write, false);

$loop = new EventLoop;

// Read data in a separate fiber after checking if the stream is
readable.
$fiber = new Fiber(function () use ($loop, $read): void {
    echo "Waiting for data...\n";

    $fiber = Fiber::this();
    $loop->read($read, fn() => $fiber->resume());
    Fiber::suspend();

    $data = fread($read, 8192);

    echo "Received data: ", $data, "\n";
});

// Start the fiber, which will suspend while waiting for a read event.
$fiber->start();

// Defer writing data to an event loop callback.
$loop->defer(fn() => fwrite($write, "Hello, world!"));

// Run the event loop.
$loop->run();
?>


Expected result:
----------------
From the RFS:

Waiting for data...
Received data: Hello, world!

Actual result:
--------------
/path/to/bin/php fiber.php

Waiting for data...

Fatal error: Uncaught Error: Call to undefined method Fiber::this() in
/var/tmp/fiber.php:66
Stack trace:
#0 [internal function]: {closure}()
#1 /var/tmp/fiber.php(76): Fiber->start()
#2 {main}
  thrown in /var/tmp/fiber.php on line 66


-- 
Edit bug report at https://bugs.php.net/bug.php?id=81254&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=81254&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=81254&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=81254&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=81254&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=81254&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=81254&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=81254&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=81254&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=81254&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=81254&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81254&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=81254&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=81254&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=81254&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=81254&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=81254&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=81254&r=mysqlcfg


Thread (3 messages)

« previous php.bugs (#234991) next »