You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Reactor does not wake parked fread() on child pipe when proc is terminated + reaped #144
$r = spawn(fn() => fread($pipes[1], 4096)); // parks on stdout
$k = spawn(function() use ($proc) {
delay(50);
proc_terminate($proc, 15); // SIGTERM child
proc_close($proc); // reap
});
await_all([$r, $k]);
```
`R` parks in `fread()` on the child's stdout pipe. After `K` runs
`proc_terminate` + `proc_close`, the child is dead and the kernel has
closed the child's write-end of the pipe — `R` should wake with EOF
(`fread() == ""` or `false`).
Instead, `R` stays parked forever. The deadlock detector eventually
kicks in, fires `Async\DeadlockError`, and only then `fread()` returns
`false`.
This is the missing-wakeup mirror of #129/#133 (spurious-wakeup on shared
handle). For a typical pattern — "stream output of a child process while
something else can kill the child" — every parked reader hangs until the
deadlock detector aborts the request.
Same dance with `stream_socket_pair` + `fclose($write_end)` (covered by
`fuzzy-tests/io/stream_close_during_read.feature`) works correctly, so
this is specific to the pipe(2)-backed reader half of `proc_open` —
likely a `uv_pipe` / `uv_poll` mismatch on POLLHUP, or proc-event
cleanup not notifying the per-pipe poll watcher.
Found by
Discovered while drafting `fuzzy-tests/exec/proc_open_chaos.feature` for
umbrella #143 (chaos coverage gaps). The two cancel-vs-close scenarios
that exercise this race are blocked on the fix; the rapid-storm scenario
(no parked reader) is unaffected.
Acceptance
A parked `fread()` on a proc_open pipe wakes with EOF when the child is
externally terminated and `proc_close`'d. The minimal repro above
prints "R: post-fread: ''" (or "…: false") and "OK" without firing
the deadlock detector.
Tried notifying the parked active_req from libuv_io_close (PR #148, since closed). Local tests passed; CI's debug-ASAN-ZTS caught a real heap-use-after-free:
proc_close → proc_open_rsrc_dtor → zend_list_close(pipe_rsrc) → stream_resource_regular_dtor → php_stream_free(stream, PHP_STREAM_FREE_CLOSE). `PHP_STREAM_FREE_CLOSE` = `CALL_DTOR | RELEASE_STREAM` — the latter `pefree`s the php_stream and php_stdio_stream_dataregardless of resource refcount. So by the time my notify wakes the parked coroutine and it resumes inside php_stdiop_read, both stream and data are gone. Every post-suspend access UAFs:
So waking-with-EOF, waking-with-exception, and waking-with-cancellation all UAF identically — they all return into php_stdiop_read post-suspend code that touches the now-freed stream.
What's actually needed
Architectural: php_stream_free must defer the pefree while a parked reader holds a reference — track "reader parked" via data->async_io->active_req != NULL (or a refcount on the stream itself), and finish the free only after the parked coroutine has resumed and dropped its reference. Spans main/streams/streams.c, php_stdiop_close, and the async-IO close contract. Could share design space with #130 (per-request completion events).
Marking #144 as architectural / requires-design rather than a quick fix. The four cancel-mid-read scenarios in fuzzy-tests/exec/proc_open_chaos.feature stay commented # Blocked: #144 until the redesign lands.
Activity
#143 fuzzy-tests: proc_open / proc_close storm chaos + finding #144
Revert "#144 libuv: notify parked reader on close (no-wake hang)"
EdmondDantes commentedon May 24, 2026
Update — quick fix attempted, reverted.
Tried notifying the parked
active_reqfromlibuv_io_close(PR #148, since closed). Local tests passed; CI's debug-ASAN-ZTS caught a real heap-use-after-free:```
==89119==ERROR: AddressSanitizer: heap-use-after-free
SUMMARY: …php_stdiop_read +697 → stream->eof = 1
```
Why the simple fix fails
proc_close→proc_open_rsrc_dtor→zend_list_close(pipe_rsrc)→stream_resource_regular_dtor→php_stream_free(stream, PHP_STREAM_FREE_CLOSE). `PHP_STREAM_FREE_CLOSE` = `CALL_DTOR | RELEASE_STREAM` — the latter `pefree`s thephp_streamandphp_stdio_stream_dataregardless of resource refcount. So by the time my notify wakes the parked coroutine and it resumes insidephp_stdiop_read, bothstreamanddataare gone. Every post-suspend access UAFs:stream->eof = 1(line 697)stream->flags & PHP_STREAM_FLAG_SUPPRESS_ERRORS(line 678)data->async_ioalready nulled before thatSo waking-with-EOF, waking-with-exception, and waking-with-cancellation all UAF identically — they all return into
php_stdiop_readpost-suspend code that touches the now-freedstream.What's actually needed
Architectural:
php_stream_freemust defer thepefreewhile a parked reader holds a reference — track "reader parked" viadata->async_io->active_req != NULL(or a refcount on the stream itself), and finish the free only after the parked coroutine has resumed and dropped its reference. Spansmain/streams/streams.c,php_stdiop_close, and the async-IO close contract. Could share design space with #130 (per-request completion events).Marking #144 as architectural / requires-design rather than a quick fix. The four cancel-mid-read scenarios in
fuzzy-tests/exec/proc_open_chaos.featurestay commented# Blocked: #144until the redesign lands.#144 libuv: notify parked subscribers before tearing watcher down
#143 fuzzy-tests: chaos coverage gaps — proc_open, curl_multi, feof, …
#144 libuv: notify parked subscribers before tearing watcher down (#149)
#144 #145 #146: reinstate cancel chaos scenarios after fixes