A scan stopped early leaks its coroutine frames when the executor shuts down with file reads in flight
Symptom#
trace/views/test_view_duql fails under Valgrind in CI (C++ FAILED (exit 1)) with a few hundred bytes "definitely lost" and millions "indirectly lost". The root block is a coroutine frame: StreamRowFold::send (stream_row_fold.h) or the scanner generator frame made in fuse_worker. It fails on one machine and passes on another. The test case that leaks runs a take query once per Runtime.
Cause#
A take stops the scan early, and the producer keeps running after the consumer is gone. When the Runtime shuts down, ThreadPoolExecutor::quiesce() waited only for queued and running work, not for file reads still in flight in the I/O pool. The read finished after the workers stopped, and its completion resumed a coroutine that the orphan sweep in shutdown() then destroyed in the middle of its chain. Every frame above it leaked. The result depends on timing, so it differs between machines.
Fix#
Make quiesce() wait for in-flight file reads. IoThreadPool counts file operations (begin_tracked before submit, end_tracked after the completion has enqueued the coroutine; accept, recv and send are not counted, because a peer can hold them forever). IoBackend::in_flight() returns the count (0 by default), ThreadPoolFileOps returns the pool's count, and quiesce() flushes the backend and keeps looping while in_flight() != 0. Do not wait for the producer inside ~StreamViewCursor: that hung under Valgrind (a worker kept driving the pool while a producer stayed parked). Keep busy = busy || ... in quiesce(), because a plain assignment there overwrote a busy worker.
Evidence#
In the CI container on an x86-64 host, scripts/ci/run_ci_local.sh --filter 'trace/views/test_view_duql$' valgrind-cpp failed before the fix (definitely lost: 392 bytes, indirectly lost: 20,907,111 bytes) and passed after it (C++ summary: 1/1 passed, 0 bytes lost), also with the original-size test data. Running each of the first 63 test cases alone, 8 at a time (about 6 minutes), found the leaking case. 12 executor and I/O tests also passed under Valgrind. The wait in the destructor was tried, hung (stack in ThreadPoolExecutor::drive_until from ~StreamViewCursor), and was reverted.
Also found by searching
- valgrind definitely lost 392 bytes indirectly lost 20,907,111 bytes in test_view_duql
- StreamRowFold::send frame definitely lost after a take query
- ViewScannerUtility operator() generator frame leaked in fuse_worker
- valgrind leak only in CI, test passes locally, a scan stopped early with take