From: sopl dot wang at gmail dot com Operating system: ALL PHP version: master-Git-2012-07-25 (Git) Package: cURL related Bug Type: Bug Bug description:When enable âwith-curlwrappers option, stream_get_contents() may cpu 100%
Description: ------------ When enable `âwith-curlwrappers` option, with http(s)/ftp(s)/other curl-wrapped stream protocols, file_get_contents(), stream_get_contents() and alike functions may lead consume 100% cpu cycle on waiting for receive. When `strace` problem php process, following will be develop: <pre> select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) </pre> The reason we found is in 'ext/curl/streams.c', function php_curl_stream_read(), line 174: <code> select(curlstream->maxfd + 1, &curlstream->readfds, &curlstream->writefds, &curlstream->excfds, &tv) </code> When select(), `writefds` always can write and immediate return, no block at here and, upper module will detect that no data could read and re-invoke this function (`php_curl_stream_read()`), a nonblock loop, lead this consume 100% cpu bug. If the link very slow, this 100% cpu problem may take the system load very high. Test script: --------------- Enable `âwith-curlwrappers` and test: <code> <?php while (true) file_get_contents('http://soplwang.com'); </code> Expected result: ---------------- Expect the behavior like native php does⦠When `strace`, native php generate: <pre> poll([{fd=3, events=POLLIN|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=3, revents=POLLIN}]) </pre> Actual result: -------------- When `strace`, generate: <pre> select(4, [3], [3], [], {15, 0}) = 1 (out [3], left {15, 0}) poll([{fd=3, events=POLLIN}], 1, 0) = 0 (Timeout) select(4, [3], [3], [], {15, 0}) = 1 (out [3], left {15, 0}) poll([{fd=3, events=POLLIN}], 1, 0) = 0 (Timeout) select(4, [3], [3], [], {15, 0}) = 1 (out [3], left {15, 0}) poll([{fd=3, events=POLLIN}], 1, 0) = 0 (Timeout) select(4, [3], [3], [], {15, 0}) = 1 (out [3], left {15, 0}) poll([{fd=3, events=POLLIN}], 1, 0) = 0 (Timeout) select(4, [3], [3], [], {15, 0}) = 1 (out [3], left {15, 0}) poll([{fd=3, events=POLLIN}], 1, 0) = 0 (Timeout) ... </pre> -- Edit bug report at https://bugs.php.net/bug.php?id=62658&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=62658&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=62658&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=62658&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=62658&r=fixed Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=62658&r=needdocs Fixed in release: https://bugs.php.net/fix.php?id=62658&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=62658&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=62658&r=needscript Try newer version: https://bugs.php.net/fix.php?id=62658&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=62658&r=support Expected behavior: https://bugs.php.net/fix.php?id=62658&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=62658&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=62658&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=62658&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=62658&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=62658&r=dst IIS Stability: https://bugs.php.net/fix.php?id=62658&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=62658&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=62658&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=62658&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=62658&r=mysqlcfg