I recently stumbled on a potential issue in PHP's pcntl module. I tried getting the attention of the PHP development community via php-bugs@lists.php.net and more, to confirm one way or other. PHP bugs general mailing list bounced back. And, I haven't had much luck yet, getting the attention of the right folks in the PHP/pcntl development team, via other means as well. So, I figured I would publish here, what may be a potential issue.
While using PHP's pcntl module and it's alarm feature, it is able to interrupt sleep, whereas, it is unable to do the same for netcat listen. This issue is further demonstrated using the code below (borrowed from here, and modified for our test). It would help to known whether this is a bug or an overlooked user error. <?php /** * Because we shouldn't handle asynchronous * events in synchronous manner. */ pcntl_async_signals(TRUE); /** * Some flag we can change to know for sure * that our operation timed out. */ $timed_out = FALSE; /** * Register SIGALRM signal handler to avoid * getting our process killed when signal arrives. */ pcntl_signal(SIGALRM, function($signal) use ($timed_out) { $timed_out = TRUE; }); /** * Now we set our timeout for 2 seconds, but it's not set in stone * we can call pcntl_alarm() anytime to extend or to turn if off. */ pcntl_alarm(2); /* Sleep will snap out in 2 sec while nc won't follow suit. * Can be tested by commenting one or the other as well. */ sleep(2000); echo `nc -nlvp 8000`; /** * If our blocking operation didn't timed out then * timer is still ticking, we should turn it off ASAP. */ $timed_out || pcntl_alarm(0); /** * And now we do whatever we want to do. */ echo "Operation aborted\n"; ?>
0 Comments
|
AuthorFounder of KryptoGuard™ technology initiative, product and services. Archives
June 2021
Categories |