Repository navigation
files: fix truncated ranged reads that reach the end of a file - #346572
Merged
Merged
Conversation
`readFileIntoStream` capped the last, partially filled buffer at the remaining `length` allowance after the bytes just read had already been subtracted from it. Ranged reads whose `length` extends past the end of the file therefore lost bytes when more than half of `length` was read: reading 65536 bytes at position 65536 of a 100000 byte file returned 31072 instead of 34464 bytes. The buffer is already sized to the allowance, so the last buffer is now written as is. Co-authored-by: Copilot <[email protected]>
Ross Wollman (rwoll)
previously approved these changes
Nov 6, 2026
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the end-of-file ranged-read fix.
No open issues 🎉
What changed in this PR
Fixes truncated ranged reads in the file service when the requested length extends beyond the end of a file.
Changes:
- Writes the final partial buffer without incorrectly capping it by the remaining read allowance.
- Adds regression tests for buffered and streamed reads that extend past the end of a file.
No actionable findings remain.
| File | Description |
|---|---|
src/vs/platform/files/test/node/diskFileService.integrationTest.ts |
Adds end-of-file ranged-read regression tests for both provider modes. |
src/vs/platform/files/common/io.ts |
Removes the incorrect cap on the final partial buffer. |
Review effort: Balanced
The test runs the "Connect via Dev Tunnel" command, which is registered when `remoteAgentHostActions.ts` loads. Since #346421 the test imports the command ID from `remoteAgentHostCommandIds.ts` instead, so it only passed when another test file had loaded the actions first, and failed when run on its own. Co-authored-by: Copilot <[email protected]>
Federico Brancasi (federicobrancasi)
approved these changes
Nov 6, 2026
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.
readFileIntoStreamtruncated ranged reads whoselengthextends past the end of the file.When the read loop stopped at the end of the file, the last, partially filled buffer was capped with
Math.min(posInBuffer, allowedRemainingBytes), butallowedRemainingByteshad already been reduced by the bytes just read. Whenever more than half oflengthwas read before the end of the file, bytes were dropped. For example, reading 65536 bytes at position 65536 of a 100000 byte file returned 31072 bytes instead of 34464.Each buffer is allocated with at most the remaining allowance, so
posInBuffercan never exceed it and the cap isn't needed. The last buffer is now written as is.This affects
readFileStream(and bufferedreadFile) withpositionandlengthon providers that read throughreadFileIntoStream: buffered providers in the renderer and the disk provider's streamed reads, which also serve webview resource range requests. I found it while working on #346571, which works around it so that the two changes are independent.How to test
./scripts/test-integration.sh --run src/vs/platform/files/test/node/diskFileService.integrationTest.ts: passed (189 tests). The two newreadFile - length past the end (large)tests (buffered and streamed) fail without the fix../scripts/test.sh --run src/vs/platform/files/test/browser/fileService.test.ts --run src/vs/platform/files/test/browser/inmemoryFileService.test.ts --run src/vs/workbench/contrib/webview/test/browser/resourceLoading.test.ts: passed (87 tests, 8 pending as before).pnpm run typecheck-client: passed.Also included
connectionDiagnosticsService.test.tsnow importsremoteAgentHostActions.ts, which registers the "Connect via Dev Tunnel" command that one of its tests runs. Since #346421 it only imports the command ID, so the test passed only when another test file had loaded the actions first. Because this PR changesio.ts, its checks run the whole unit test suite, where that test failed on Linux and Windows in both Electron and the browser. It also fails on its own onmain../scripts/test.sh --run src/vs/sessions/contrib/providers/remoteAgentHost/test/browser/connectionDiagnosticsService.test.ts: passed (failed withCannot read properties of undefined (reading 'handler')before).pnpm run test-browser-no-install --run src/vs/sessions/contrib/providers/remoteAgentHost/test/browser/connectionDiagnosticsService.test.ts --browser chromium: passed.