Skip to content

set_time_limit() counts time while pg_get_result() waiting for asynchronous PostgreSQL query #14769

Closed

Description

@lezso6

Description

The following code:

<?php
// Connect
$conn = pg_connect("dbname='somedb'");
// Send a postgres sleep query (or anything long running query)
pg_send_query($conn, "SELECT pg_sleep(5)");
// Set PHP max execution time less than above
set_time_limit(2);
// Waiting for query to complete (but fails and error is shown)
$res = pg_get_result($conn); 
// Never printed
echo "OK"; 

Resulted in this output:

PHP Fatal error: maximum execution time of 2 seconds exceeded

But I expected this output instead:

OK

PHP Version

8.3.8

Operating System

Gentoo Linux

Activity

  1. lezso6 commented on Jul 2, 2024

    @lezso6
    Author

    The code worked as expected in PHP 8.1, until I upgraded to PHP 8.3. I think it should not count time while the PHP script is doing nothing but waiting for the PostgreSQL server. I did not find any documentation about this change in code behaviour.

  2. devnexen commented on Jul 2, 2024

    @devnexen
    Member

    I have tried with the following cases :

    • last master built on ubuntu 24.04 on local pgsql server.
    • 8.3.6 from same ubuntu with same server.
    • 8.3.8 from macOs remote connection on this same pgsql server.

    In every case I get my OK.

  3. lezso6 commented on Jul 2, 2024

    @lezso6
    Author

    Thanks for the quick reply. Indeed, the code works as it should on Ubuntu, I tried it too. I have two very different Gentoo systems with PHP 8.3 and both produces the bug. I also tested PHP 8.2 on Gentoo (same configuration) and got the OK. It's weird, seems like a Gentoo specific bug on version 8.3.

  4. MorganLOCode commented on Jul 3, 2024

    @MorganLOCode

    As another datapoint, 8.3.8 on Windows times out as well.

  5. lezso6 commented on Jul 3, 2024

    @lezso6
    Author

    I tried version 8.3.6 and 8.3.3 on Gentoo and both throws the error.

  6. lezso6 commented on Jul 3, 2024

    @lezso6
    Author

    I think I found the problem, the error depends on whether thread safety is enabled or not. If I compile PHP 8.3.8 on Gentoo without threads, the above code works as should. But if I compile PHP with POSIX threads, the code breaks. On Ubuntu the PHP 8.3.6 is compiled without threads, that's why it's working okay.

  7. devnexen commented on Jul 3, 2024

    @devnexen
    Member

    does it work with ZTS and in a non async way ? e.g. just pg_query

  8. lezso6 commented on Jul 3, 2024

    @lezso6
    Author

    Suprisingly, the script times out in non-async / blocking mode too:

    <?php
    $conn = pg_connect("host='127.0.0.1' port='5432' dbname='template1' user='postgres'");
    ini_set('display_errors', '1');
    // Setting execution time less than below
    set_time_limit(2);
    // PHP Fatal error: maximum execution time of 2 seconds exceeded
    pg_query($conn, "SELECT pg_sleep(5)");
    // Never printed
    echo "OK";
    
  9. self-assigned this
    on Jul 5, 2024
  10. devnexen commented on Jul 5, 2024

    @devnexen
    Member

    I ll have a look in the following days.

  11. devnexen commented on Jul 7, 2024

    @devnexen
    Member

    Anybody using mysql ? If yes what happens with ZTS build with 'SELECT SLEEP(5)' query?

  12. lezso6 commented on Jul 7, 2024

    @lezso6
    Author

    I tried mysqli extension with MariaDB and the PHP script times out as well. Code:

    <?php
    ini_set('display_errors', '1');
    echo PHP_VERSION . "\n";
    $conn = mysqli_connect('127.0.0.1', 'root', '1234');
    // Setting execution time less than SLEEP()
    set_time_limit(2);
    // PHP Fatal error: maximum execution time of 2 seconds exceeded
    mysqli_query($conn, "SELECT SLEEP(5)");
    // Never printed
    echo "OK"; 
    
  13. lezso6 commented on Jul 7, 2024

    @lezso6
    Author

    Even PHP's sleep() function times out with ZTS enabled:

    <?php
    ini_set('display_errors', '1');
    echo PHP_VERSION . "\n";
    // Setting execution time less than PHP sleep()
    set_time_limit(2);
    // PHP Fatal error: maximum execution time of 2 seconds exceeded
    sleep(5);
    // Never printed
    echo "OK"; 
    

    Without ZTS, there is no timeout.

  14. devnexen commented on Jul 7, 2024

    @devnexen
    Member

    Ok gonna bisect it. I think that s better support Linux timeouts with ZTS part but will double-check.

  15. devnexen commented on Jul 7, 2024

    @devnexen
    Member

    Was wrong about the commit, @lezso6, if you build from source, can you add --enable-zend-max-execution-timers=no to the ./configure command ? I forgot about that change ..

  16. lezso6 commented on Jul 8, 2024

    @lezso6
    Author

    I recompiled with ZTS enabled and Zend Max Execution Timers disabled, and the bug is gone. It works, no timeouts.

  17. devnexen commented on Jul 8, 2024

    @devnexen
    Member

    It appears on the migration guide. Distribution packagers would need to modify their build workflow to take it into account.

  18. lezso6 commented on Jul 8, 2024

    @lezso6
    Author

    So if I understand correctly, this is a feature. And will be enabled for non-ZTS builds, I read that here: #6504 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions