Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions src/runtime/webcore/FileReader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,35 @@ impl Lazy {

let fd: Fd = match &file.pathlike {
PathOrFileDescriptor::Fd(pl_fd) => {
if pl_fd.stdio_tag().is_some() {
'brk: {
#[cfg(unix)]
{
let rc = open_as_nonblocking_tty(pl_fd.native(), sys::O::RDONLY);
if rc > -1 {
is_nonblocking = true;
file.is_atty = Some(true);
break 'brk Fd::from_native(rc);
}
}
break 'brk *pl_fd;
// A tty is read through a private O_NONBLOCK description of the
// same terminal, so the reader can poll it and stop at any time
// without touching the flags of the caller's fd. stdio falls
// back to polling the shared fd; any other fd falls back to a
// dup the reader owns.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[cfg(not(unix))]
let reopened_tty: Option<Fd> = None;
#[cfg(unix)]
let reopened_tty: Option<Fd> = {
let is_tty = pl_fd.stdio_tag().is_some()
|| *file.is_atty.get_or_insert_with(|| sys::isatty(*pl_fd));
let rc = if is_tty {
open_as_nonblocking_tty(pl_fd.native(), sys::O::RDONLY)
} else {
-1
};
if rc > -1 {
is_nonblocking = true;
file.is_atty = Some(true);
Some(Fd::from_native(rc))
} else {
None
}
};

if let Some(fd) = reopened_tty {
fd
} else if pl_fd.stdio_tag().is_some() {
*pl_fd
} else {
let duped = sys::dup_with_flags(*pl_fd, 0);

Expand Down
57 changes: 56 additions & 1 deletion test/js/bun/util/bun-file-fd-read.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from "bun:test";
import { closeSync, openSync } from "fs";
import { isWindows, tempDir } from "harness";
import { bunEnv, bunExe, isWindows, tempDir } from "harness";
import { join } from "path";

// Reading a Bun.file() backed by a file descriptor goes through
Expand Down Expand Up @@ -49,4 +49,59 @@ describe.skipIf(isWindows)("Bun.file(fd) read", () => {
expect(await withFd(path, fd => Bun.file(fd).text())).toBe("");
expect((await withFd(path, fd => Bun.file(fd).arrayBuffer())).byteLength).toBe(0);
});

// A tty fd other than stdin used to be read like a regular file: the stream
// returned nothing and held nothing, so the process exited before any input.
// It now gets the same polled, privately reopened terminal as Bun.stdin.
test("stream() on a /dev/tty fd polls the terminal and cancel() releases it", async () => {
let output = "";
let wrote = false;
const decoder = new TextDecoder();
const proc = Bun.spawn({
Comment thread
robobun marked this conversation as resolved.
Resolved
cmd: [
bunExe(),
"-e",
`
const fs = require("fs");
const fd = fs.openSync("/dev/tty", "r");
const reader = Bun.file(fd).stream().getReader();
const first = reader.read();
console.log("ready");
const { value } = await first;
console.log("chunk", JSON.stringify(Buffer.from(value).toString()));
// A second read is pending on the terminal while cancel() runs.
const second = reader.read();
await reader.cancel();
console.log("cancelled", JSON.stringify(await second));
// The caller still owns its fd.
console.log("fd", fs.fstatSync(fd).isCharacterDevice() ? "open" : "closed");
`,
],
env: bunEnv,
terminal: {
cols: 200,
rows: 24,
data(terminal, chunk: Uint8Array) {
output += decoder.decode(chunk, { stream: true });
if (!wrote && output.includes("ready")) {
wrote = true;
terminal.write("hello\n");
}
},
},
});
const exitCode = await proc.exited;
proc.terminal?.close();
output += decoder.decode();

// The pty echoes the typed line before the child reads it.
expect(Bun.stripANSI(output).split(/\r?\n/).filter(Boolean)).toEqual([
"ready",
"hello",
'chunk "hello\\n"',
'cancelled {"done":true}',
"fd open",
]);
expect(exitCode).toBe(0);
});
});
Loading