Repository navigation
fix(ext/io): cancel pending FileResource reads on close - #34544
Merged
Merged
Conversation
A `ReadableStream` backed by a file resource (`Deno.FsFile.readable`) could not be cancelled while a read was in flight. Reads on pipes / FIFOs may block in the kernel indefinitely, so the in-flight `op_read` kept the runtime alive long after JS had finished. Add a `CancelHandle` to `FileResource`, wrap `read` / `read_byob` with `try_or_cancel`, and trigger the handle from `Resource::close()`. Fixes #21186. Co-Authored-By: Divy Srivastava <me@littledivy.com>
3 tasks done
CI's resource sanitizer flagged the piped child stderr as a leak when the test was retried under reduced concurrency. Replace the manual spawn/Promise.race plumbing with Deno.Command.output() + a signal option, which drains both pipes and surfaces the watchdog via out.signal. Co-Authored-By: Divy Srivastava <me@littledivy.com>
The 1-second watchdog interval can fire after the for-await loop on stderr has drained but before `clearInterval` runs in the `finally` block. If the child exited in that window, `proc.kill` throws and fails the wpt job. Wrap the kill in a try/catch. Unrelated to the fs-cancellation fix in this PR, but the wpt job intermittently fails on this race and I happened to catch it in CI. Co-Authored-By: Divy Srivastava <me@littledivy.com>
littledivy
added a commit
to crowlKats/deno
that referenced
this pull request
Jun 10, 2026
## Summary A `ReadableStream` backed by a file resource (`Deno.FsFile.readable`) could not be cancelled while a read was in flight. Reads on pipes / FIFOs may block in the kernel indefinitely, so the in-flight `op_read` kept the runtime alive long after JS had finished, preventing the process from exiting. This PR adds a `CancelHandle` to `FileResource`, wraps `read` / `read_byob` with `try_or_cancel`, and triggers the handle from `Resource::close()`. The op completes promptly on the JS side when the stream is cancelled; the spawned blocking thread may still complete its own kernel read (we cannot interrupt `spawn_blocking`), but its result is discarded and the runtime no longer waits on it. This mirrors the pattern already used by `ChildStdoutResource`, `TcpStreamResource`, etc. Fixes denoland#21186. ## Reproducer Before the fix, this would print `OK` and then hang indefinitely: ```ts // open both ends so the open syscall doesn't block const file = await Deno.open("/path/to/fifo", { read: true, write: true }); const r = file.readable.getReader(); const reading = r.read(); await new Promise((res) => setTimeout(res, 50)); await r.cancel(); await reading; // resolves with {done:true} console.log("OK"); // runtime then hangs on the orphaned op_read ``` After the fix, the same script prints `OK` and exits cleanly. ## Test plan - New unit test `readableStreamCancelOnFifo` in `tests/unit/files_test.ts` (Unix only): spawns a subprocess that performs the above, with a 10s watchdog. Verified it fails on `main` and passes with this PR. - Existing `readableStream` / `readableStreamTextEncoderPipe` tests still pass. - `cargo clippy -p deno_io` clean. Closes denoland/orchid#288 --------- Co-authored-by: divybot <divybot@users.noreply.github.com> Co-authored-by: Divy Srivastava <me@littledivy.com>
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.
Summary
A
ReadableStreambacked by a file resource (Deno.FsFile.readable) could not be cancelled while a read was in flight. Reads on pipes / FIFOs may block in the kernel indefinitely, so the in-flightop_readkept the runtime alive long after JS had finished, preventing the process from exiting.This PR adds a
CancelHandletoFileResource, wrapsread/read_byobwithtry_or_cancel, and triggers the handle fromResource::close(). The op completes promptly on the JS side when the stream is cancelled; the spawned blocking thread may still complete its own kernel read (we cannot interruptspawn_blocking), but its result is discarded and the runtime no longer waits on it.This mirrors the pattern already used by
ChildStdoutResource,TcpStreamResource, etc.Fixes #21186.
Reproducer
Before the fix, this would print
OKand then hang indefinitely:After the fix, the same script prints
OKand exits cleanly.Test plan
readableStreamCancelOnFifointests/unit/files_test.ts(Unix only): spawns a subprocess that performs the above, with a 10s watchdog. Verified it fails onmainand passes with this PR.readableStream/readableStreamTextEncoderPipetests still pass.cargo clippy -p deno_ioclean.Closes denoland/orchid#288