Socket resource id overflowing

前端 未结 1 587
一向
一向 2021-01-14 06:42

What: I have a PHP script running that awaits socket connections. When I connect to the socket the script prints out the resource ID.

Probl

相关标签:
1条回答
  • 2021-01-14 07:10

    PHP does not re-use resource IDs internally, so eventually you'll hit a problem with PHP using them all up, causing the warning you got. See the bug report. Since a bunch of things in PHP will register a resource, incrementing the resource ID, this bug is easy to encounter in long running scripts.

    The max ID depends on your architecture. You can print the constant PHP_INT_MAX to get the number for your install, but on 32 bit systems it's generally 2,147,483,647. It's significantly higher on 64 bit systems. Mine prints out 9,223,372,036,854,775,807. You're pretty unlikely to exhaust the resource ID limit on 64 bit systems.

    Also, you call file_get_contents in your unbounded while (true) loop. You have no sleep period between each iteration of the while loop, so the loop basically executes as fast as it can. Each file_get_contents call results in the resource ID pointer being incremented by 2, as it uses 2 resources under the hood. Example:

    <?php
    echo gmp_init("0x41682179fbf5"). "\n"; // Resource id #4
    echo gmp_init("0x41682179fbf5"). "\n"; // Resource id #5
    echo gmp_init("0x41682179fbf5"). "\n"; // Resource id #6
    file_get_contents('/etc/hosts');
    echo gmp_init("0x41682179fbf5"). "\n"; // Resource id #9
    echo gmp_init("0x41682179fbf5"). "\n"; // Resource id #10
    echo gmp_init("0x41682179fbf5"). "\n"; // Resource id #11
    
    0 讨论(0)
提交回复
热议问题