Postgres and event issue

Issue #45 wontfix
socketman2016
created an issue

I want to add read event to pg_socket with event extension both don't accept resource of pg_socket but stream_select works well

For simplicity I write test script with well known library called reactphp/event-loop

if you run test script using event extension , this warning shown and there is no read event

PHP Warning: Event::add(): Epoll ADD(1) on fd 30584288 failed. Old events were 0; read change was 1 (add); write change was 0 (none); close change was 0 (none): Bad file descriptor in vendor/react/event-loop/src/ExtEventLoop.php on line 259 PHP Warning: Event::add(): Failed adding event in vendor/react/event-loop/src/ExtEventLoop.php on line 259

Test script:

$loop = React\EventLoop\Factory::create();
$connection = pg_connect("host=localhost port=5432 dbname=mary");
$socket = pg_socket($connection);
$loop->addReadStream($socket , function (){
  echo "READ EVENT\n";
});

pg_send_query($connection , "select * from authors;");

Actual result:

PHP Warning: Event::add(): Epoll ADD(1) on fd 30584288 failed. Old events were 0; read change was 1 (add); write change was 0 (none); close change was 0 (none): Bad file descriptor in vendor/react/event-loop/src/ExtEventLoop.php on line 259 PHP Warning: Event::add(): Failed adding event in vendor/react/event-loop/src/ExtEventLoop.php on line 259

Comments (11)

  1. Ruslan Osmanov repo owner

    Is that the complete test script? I don't see how the event loop is launched, and how the socket variable is preserved until the loop finishes.

    I'm not very familiar with ReactPHP. Probably, it launches the event loop implicitly in the shutdown handler. If it's so, your $socket variable gets destroyed before the event loop tries to access it. So you should preserve all variables (such as $socket) that are supposed to be used in the event handlers until they are no longer needed.

  2. socketman2016 reporter

    See https://github.com/reactphp/event-loop/issues/121 , It is hard to repro issue But if you install react/event-loop with composer and run this code

    <?php
    require_once 'vendor/autoload.php';
    $loop = React\EventLoop\Factory::create();
    $connection = pg_connect("host=localhost port=5432 dbname=mary");
    $socket = pg_socket($connection);
    $loop->addReadStream($socket , function (){
      echo "READ EVENT\n";
    });
    
    pg_send_query($connection , "select * from authors;");
    $loop->run();
    

    We have Segmentation fault on PHP 7.0

  3. Ruslan Osmanov repo owner

    @socketman2016 I will try to reproduce and fix the issue, if it is fixable from the source of Event. But I'm very busy these days, and I will not promise anything.

    Right now, I can only say that Event actually does not support postgres socket resources. Look at this function, for instance.

    As always, pull requests are welcome.

  4. Ruslan Osmanov repo owner

    I have finally tested Event with pgsql extension. As I guessed, Event extension does not support pgsql resources =) And the Event::add method rightfully returns false. However, React\EventLoop\ExtEventLoop::subscribeStreamEvent method ignores the returned value.

    I would have suggested to add support for the "PostgreSQL link" resource into Event, if pgsql had a public API for accessing the resource internals.

  5. Log in to comment