Skip to content

fix(ext/io): cancel pending FileResource reads on close - #34544

Merged
littledivy merged 3 commits into
mainfrom
orch/divybot-288
May 30, 2026
Merged

littledivy merged 3 commits into
mainfrom
orch/divybot-288

Conversation

@divybot

@divybot divybot commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

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 #21186.

Reproducer

Before the fix, this would print OK and then hang indefinitely:

// 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

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>
@divybot divybot changed the title [denoland/deno#21186] FileResource-backed streams cannot be cancelled fix(ext/io): cancel pending FileResource reads on close May 29, 2026
divybot and others added 2 commits May 29, 2026 21:29
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
littledivy merged commit 161b09e into main May 30, 2026
136 checks passed
@littledivy
littledivy deleted the orch/divybot-288 branch May 30, 2026 01:34
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>
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.

FileResource-backed streams cannot be cancelled

2 participants