Repository navigation
Description
Summary
If a log sink's receiver stops reading, the server cannot shut down: the process aborts with
ZEND_ASYNC_REACTOR_LOOP_ALIVE() == false && "The event loop must be stopped"
at ext/async/scheduler.c:2041
Found while adding a test for the new log_records_dropped_total metric (#5 branch, after the log thread landed in e4b4413). The drop accounting itself behaves correctly — records are dropped and counted exactly as designed. The failure is in teardown.
Repro
/* one end of the pair is a sink; nobody ever reads the other */
[$reader, $writer] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
$config = (new HttpServerConfig())
->addListener('127.0.0.1', $port)
->setStatsEnabled(true)
->setLogSinks([
['type' => 'stream', 'stream' => $writer, 'format' => 'plain',
'level' => LogSeverity::INFO],
]);
$server = new HttpServer($config);
$server->addHttpHandler(fn($req, $res) => $res->setBody('x')->end());
spawn(function () use ($server) {
usleep(50000);
_http_log_flood($server, 40000); /* test hook: emit N records from this thread */
$server->stop();
});
$server->start();The flood fills the kernel socket buffer, then the sink's 64 KiB ring behind it; drops are counted (getStats()['totals']['log_records_dropped_total'] > 0, and the count matches what the hook reports). Then stop() runs and the process aborts on the assert above.
What happens
The sink's transport is owned by the log thread (src/log/http_log.c). At stop:
- the stopping thread marks the sink
closingand waits on a trigger the log thread is supposed to fire once the rings are drained (http_log_sink_stop, 3 s budget); - on the log thread,
writer_kick_nextseesactive_req != NULL— a write that will never complete, because the peer is not reading — and returns without tearing anything down; writer_teardowntherefore never runs: the io stays open with a write queued, the log thread's loop never goes idle, the thread does not join, and the parent trips the loop-alive assert on the way out.
The waiter's 3 s budget expires and it abandons the sink, but abandoning does not help: the transport is still live on the other thread.
Attempted fix (reverted — it hung)
I tried a hard deadline on the log-thread side: arm a one-shot timer at close, and when it fires force writer_teardown — close the io (cancelling the queued write), free the rings, fire the caller's trigger, and mark the writer cb abandoned so the cancelled write's completion is the one that frees it.
That hung (the test never finished, not even under a 40 s timeout), so it was reverted rather than committed. It was not investigated further.
Where to look
- What
ZEND_ASYNC_IO_CLOSEactually does to a write already queued on the handle, and whether disposing the io immediately after closing it is safe while a completion is still pending. - How
reactor_pool_destroyjoins a reactor thread whoseuv_runis blocked with a live handle — the loop body iswhile (!stopping) ZEND_ASYNC_REACTOR_EXECUTE(/*no_wait=*/false), so the stop sentinel has to wake it. - Which event is left alive on the parent's loop when the log thread does not exit (most likely the thread-pool completion event for that worker).
Notes
- Only reachable with a genuinely stalled receiver (a socket whose peer never reads, a wedged syslog collector). A file sink cannot stall this way.
- The producer side is fine: emit never blocks, the record is dropped whole, and the loss is counted per producer thread.
- The test that reproduces this is not in the tree — it was removed rather than committed as a failing/hanging test. The snippet above is it.
Activity
5 docs+test: document the log thread in the plan; h3client warns on a…
Merge pull request #126 from true-async/121-log-sink-whose-receiver-s…