php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80737 blocking flock() with timeout
Submitted: 2021-02-12 10:50 UTC Modified: 2021-05-12 06:14 UTC
From: maggus dot staab+php at googlemail dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 7.4.15 OS: ubuntu16/18/20
Private report: No CVE-ID: None
 [2021-02-12 10:50 UTC] maggus dot staab+php at googlemail dot com
Description:
------------
I did a lot of research regarding flock() and timeouts.

I found workarounds using pcntl_signal/alarm which only work on the CLI:
https://github.com/php-lock/lock/blob/8b254edfe1400a733e3cde571e981687e6f6d804/classes/util/PcntlTimeout.php#L68-L94

our current workaround is to use LOCK_NB in a loop + usleep() until it succeeds, which feels rather hacky and wastes a lot of IO especially on NFS based volumes.
This was the only way I found to get it working in a web sapi.

wouldn't it be possible to e.g. take a stream_set_timeout() into account for the blocking flock() case?

Test script:
---------------
$fd = fopen("/path/to/my/file", 'c+');

stream_set_timeout($fd, 0, 1);
var_dump(flock($fd, LOCK_EX)); // should return false on timeout

$info = stream_get_meta_data($fd);
var_dump($info); // should report a timeout



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-12 15:54 UTC] requinix@php.net
-Type: Bug +Type: Feature/Change Request -Package: Streams related +Package: Filesystem function related
 [2021-05-12 06:14 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2021-05-12 06:14 UTC] krakjoe@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

I'm afraid there's no portable way to achieve this, and so nothing we can (or should try to) do in php-src.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 17:01:29 2024 UTC