Keep the bytes a cancelled read took from the descriptor - #289
Merged
Merged
Conversation
A file read is submitted with offset -1, so read(2) moves the descriptor offset. A cancelled coroutine returned -1 and disposed the request, and the bytes the worker had already taken went to nobody: the next reader carried on past them, and ftell() answered a number unrelated to the descriptor. A read abandoned mid-flight now seeks the descriptor back by what it took, and file reads are serialized per handle the way file writes already are, so nothing reads from the advanced offset before that rewind. The two queues share one unlink helper: the copy this replaces cleared q_next while looking in the other queue, which cut the list in two. A submit that fails now completes the request with its exception instead of throwing, which is what the write path already does: fread() reports the error as -1 with a notice rather than as an exception. Evidence: tests/io/100-cancel_keeps_the_position.phpt, red 4 runs in 5 before and green 10 in 10 after. The queue costs nothing measurable — 256 MB in 8 KB chunks on a release build, medians of five runs, 2226.3 ms before, 2171.6 ms after.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Closes #288.
A file read is submitted with
offset = -1, soread(2)moves the descriptor offset. A cancelled or timed-out coroutine returned-1and disposed the request, and the bytes the worker had already taken went to nobody: the next reader on that handle carried on past them, andftell()answered a number unrelated to the descriptor. On a file whose every 16 bytes carry their own record number, after a cancelled 64 KB read the next read returned the record at byte 65536 whileftell()said 32.Three parts, two of them here:
fsyncor asendfileon the same handle moves that offset too.q_nextwhile looking in the other queue, which cut the list in two.true-asyncc9932d7d878: a read that finished before the exception arrived hands its count back instead of-1, since the bytes are already in the caller's buffer. A stream with a read filter keeps the old answer — the filter call under a pending exception returns without running, and the filter layer reads that as a fatal error the stream never recovers from.A submit that fails now completes the request with its exception instead of throwing, which is what the write path already does:
fread()reports the error as-1with a notice rather than as an exception.Verification
tests/io/100-cancel_keeps_the_position.phpt: red 4 runs in 5 before, green 10 in 10 after.ext/async/testsplus php-srcfile,streams,filters,general_functions,zlib,phar,session— 3831 tests, 3 failures, all three the local symlink and network artefacts (curl 063, curl 064, io 082).ext/async/testsplus php-srcfile,streams,filters— 1937 tests, 1 failure, the local network artefact.