fix(tail): pre-size scratch before non-fixed io_uring read SQEs - #315
Open
detail-app[bot] wants to merge 3 commits into
Open
detail-app[bot] wants to merge 3 commits into
detail-app[bot] wants to merge 3 commits into
Conversation
ApprovabilityVerdict: Approved at Macroscope's review found this PR approvable — This is a narrowly scoped io_uring bug fix that stabilizes non-fixed read buffers without changing fixed or scalar paths. A forced-relocation regression test verifies byte-exact output and confirms the actual CQE path was exercised. You can add or adjust custom eligibility rules. Learn more. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Detail bug report: View on Detail
Fixes ENG-716 — https://linear.app/usetero/issue/ENG-716/detail-bug-tail-io-uring-non-fixed-multi-read-batches-can-emit
Bug
In the io_uring
Scheduler.processBatchnon-fixed fallback (src/tail/read_scheduler/uring_linux.zig),self.scratchwas grown per event inside the dispatch loop whileself.ring.readhad already pinned each event's slot pointer intosqe.addr. Reads are not submitted untilsubmit_and_waitruns after the loop, so a mid-loopArrayListrelocation freed the backing buffer that earlier SQEs still referenced. The kernel then read file bytes into freed memory while the completion handler sliced the relocatedscratch, feeding wrong/zero bytes to the framer — silent wrong output on the daemon's primary data path. This fallback runs on every cycle when fixed-buffer/fixed-file registration is unavailable (kernels lackingregister_files_sparse, lowRLIMIT_NOFILE, orENOMEM). Introduced in a32652c (PR #148).Fix
Pre-size
self.scratchonce for the whole batch before preparing any SQEs: a preview pass sums the per-event read sizes (mirroring the dispatch loop's skip/cap logic), then per-event slots are taken withaddManyAsSliceAssumeCapacity, which only advancesitems.lenand never reallocates. Everysqe.addrnow stays live and at a stable address until its CQE is reaped — the stability the fixed path already had incidentally. The Debug assert inaddManyAsSliceAssumeCapacityguards that the preview and dispatch passes agree.Testing
zig fmt --check,ziglint,zig build, andzig build test(Debug and ReleaseSafe) all pass — 520 passed, 1 skipped (pre-existing s3-e2e), 0 failed.read scheduler uring non-fixed path: multi-event batch keeps read buffers stable, backed by aMovingAllocatorthat forces every grow to relocate while retaining freed memory, deterministically exercising the invariant regardless of allocator size-class luck. Verified it passes with the fix and fails when the bug is restored (the completion handler reads relocated/poison bytes instead of the file's bytes) in both Debug and ReleaseSafe — the productionc_allocatorconfig where the wrong-output symptom manifests.edge-tailbinary against two growing files with--io-engine uring, appending distinct NDJSON lines across poll cycles; confirmed byte-exact emission on both the fixed path (default rlimit) and the non-fixed path, the latter forced viaulimit -n 96(a probe confirmed this makesregister_files_sparsefail withUserFdQuotaExceeded, flippingfixed_enabled = false— the exact production fallback the bug lives in). An initial e2e attempt produced no output because lines were written before the daemon started (--read-fromdefaults to tail/EOF) under an rlimit too low for the daemon to open its files; re-run with post-start appends and a sufficient rlimit passed.addManyAsSliceAssumeCapacityassert trips; edge cases for zero-range/no-op events (0 ops, early return), batches exceeding the 256-slot ring cap (capped correctly), and events larger thanread_buf(remainder funnelled through the scalar tail-continuation path) were all byte-exact in both build modes.Automatic Fixes PRs can be configured here.