Commit 161b09e
fix(ext/io): cancel pending FileResource reads on close (#34544)
## 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:
```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>1 parent ff4275a commit 161b09e
3 files changed
Lines changed: 97 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
15 | 18 | | |
| 19 | + | |
16 | 20 | | |
17 | 21 | | |
18 | 22 | | |
| |||
98 | 102 | | |
99 | 103 | | |
100 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
101 | 111 | | |
102 | 112 | | |
103 | 113 | | |
| |||
345 | 355 | | |
346 | 356 | | |
347 | 357 | | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
348 | 363 | | |
349 | 364 | | |
350 | 365 | | |
351 | 366 | | |
352 | | - | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
353 | 376 | | |
354 | 377 | | |
355 | 378 | | |
| |||
397 | 420 | | |
398 | 421 | | |
399 | 422 | | |
| 423 | + | |
400 | 424 | | |
401 | 425 | | |
402 | 426 | | |
403 | 427 | | |
404 | 428 | | |
| 429 | + | |
405 | 430 | | |
406 | 431 | | |
407 | 432 | | |
| |||
411 | 436 | | |
412 | 437 | | |
413 | 438 | | |
| 439 | + | |
414 | 440 | | |
415 | 441 | | |
416 | 442 | | |
417 | 443 | | |
418 | 444 | | |
| 445 | + | |
419 | 446 | | |
420 | 447 | | |
421 | 448 | | |
| |||
468 | 495 | | |
469 | 496 | | |
470 | 497 | | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
471 | 506 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
640 | 640 | | |
641 | 641 | | |
642 | 642 | | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
643 | 698 | | |
644 | 699 | | |
645 | 700 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
179 | 179 | | |
180 | 180 | | |
181 | 181 | | |
182 | | - | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
183 | 188 | | |
184 | 189 | | |
185 | 190 | | |
| |||
0 commit comments