Skip to content

files: fix truncated ranged reads that reach the end of a file - #346572

Merged
Logan Ramos (lramos15) merged 2 commits into
mainfrom
agents/fix-ranged-read-truncation
Nov 6, 2026
Merged

Logan Ramos (lramos15) merged 2 commits into
mainfrom
agents/fix-ranged-read-truncation

Conversation

@lramos15

@lramos15 Logan Ramos (lramos15) commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

readFileIntoStream truncated ranged reads whose length extends 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), but allowedRemainingBytes had already been reduced by the bytes just read. Whenever more than half of length was 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 posInBuffer can never exceed it and the cap isn't needed. The last buffer is now written as is.

This affects readFileStream (and buffered readFile) with position and length on providers that read through readFileIntoStream: 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 new readFile - 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.ts now imports remoteAgentHostActions.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 changes io.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 on main.

  • ./scripts/test.sh --run src/vs/sessions/contrib/providers/remoteAgentHost/test/browser/connectionDiagnosticsService.test.ts: passed (failed with Cannot 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.

`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]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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]>
@lramos15
Logan Ramos (lramos15) merged commit 3bd71c3 into main Nov 6, 2026
51 checks passed
@lramos15
Logan Ramos (lramos15) deleted the agents/fix-ranged-read-truncation branch November 6, 2026 18:37
@vs-code-engineering vs-code-engineering Bot added this to the 1.146.0 milestone Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants