Skip to content

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

Closed
divybot wants to merge 1 commit into
mainfrom
orch/divybot-288
Closed

divybot wants to merge 1 commit 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:

import { spawn } from "node:child_process";
// 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 commented May 29, 2026

Copy link
Copy Markdown
Contributor Author

Closing as duplicate — orchid pre-created #34544 from the same branch; consolidating discussion there.

@divybot divybot closed this May 29, 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.

FileResource-backed streams cannot be cancelled

1 participant