Files
bun.sh/test/regression/issue/tty-readstream-ref-unref.test.ts
robobun 9b97dd11e2 Fix TTY reopening after stdin EOF (#22591)
## Summary
- Fixes ENXIO error when reopening `/dev/tty` after stdin reaches EOF
- Fixes ESPIPE error when reading from reopened TTY streams  
- Adds ref/unref methods to tty.ReadStream for socket-like behavior
- Enables TUI applications that read piped input then switch to
interactive TTY mode

## The Problem
TUI applications and interactive CLI tools have a pattern where they:
1. Read piped input as initial data: `echo "data" | tui-app`
2. After stdin ends, reopen `/dev/tty` for interactive session
3. Use the TTY for interactive input/output

This didn't work in Bun due to missing functionality:
- **ESPIPE error**: TTY ReadStreams incorrectly had `pos=0` causing
`pread()` syscall usage which fails on character devices
- **Missing methods**: tty.ReadStream lacked ref/unref methods that TUI
apps expect for socket-like behavior
- **Hardcoded isTTY**: tty.ReadStream always set `isTTY = true` even for
non-TTY file descriptors

## The Solution
1. **Fix ReadStream position**: For fd-based streams (like TTY), don't
default `start` to 0. This keeps `pos` undefined, ensuring `read()`
syscall is used instead of `pread()`.

2. **Add ref/unref methods**: Implement ref/unref on tty.ReadStream
prototype to match Node.js socket-like behavior, allowing TUI apps to
control event loop behavior.

3. **Dynamic isTTY check**: Use `isatty(fd)` to properly detect if the
file descriptor is actually a TTY.

## Test Results
```bash
$ bun test test/regression/issue/tty-reopen-after-stdin-eof.test.ts
✓ can reopen /dev/tty after stdin EOF for interactive session
✓ TTY ReadStream should not set position for character devices

$ bun test test/regression/issue/tty-readstream-ref-unref.test.ts
✓ tty.ReadStream should have ref/unref methods when opened on /dev/tty
✓ tty.ReadStream ref/unref should behave like Node.js

$ bun test test/regression/issue/tui-app-tty-pattern.test.ts
✓ TUI app pattern: read piped stdin then reopen /dev/tty
✓ tty.ReadStream handles non-TTY file descriptors correctly
```

## Compatibility
Tested against Node.js v24.3.0 - our behavior now matches:
-  Can reopen `/dev/tty` after stdin EOF
-  TTY ReadStream has `pos: undefined` and `start: undefined`
-  tty.ReadStream has ref/unref methods for socket-like behavior
-  `isTTY` is properly determined using `isatty(fd)`

---------

Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
2025-09-13 01:00:57 -07:00

110 lines
3.0 KiB
TypeScript

import { expect, test } from "bun:test";
import { openSync } from "fs";
import { bunEnv, bunExe, normalizeBunSnapshot } from "harness";
import tty from "tty";
test("tty.ReadStream should have ref/unref methods when opened on /dev/tty", () => {
// Skip this test if /dev/tty is not available (e.g., in CI without TTY)
let ttyFd: number;
try {
ttyFd = openSync("/dev/tty", "r");
} catch (err: any) {
if (err.code === "ENXIO" || err.code === "ENOENT") {
// No TTY available, skip the test
return;
}
throw err;
}
try {
// Create a tty.ReadStream with the /dev/tty file descriptor
const stream = new tty.ReadStream(ttyFd);
// Verify the stream is recognized as a TTY
expect(stream.isTTY).toBe(true);
// Verify ref/unref methods exist
expect(typeof stream.ref).toBe("function");
expect(typeof stream.unref).toBe("function");
// Verify ref/unref return the stream for chaining
expect(stream.ref()).toBe(stream);
expect(stream.unref()).toBe(stream);
// Clean up - destroy will close the fd
stream.destroy();
} finally {
// Don't double-close the fd - stream.destroy() already closed it
}
});
test("tty.ReadStream ref/unref should behave like Node.js", async () => {
// Skip on Windows - no /dev/tty
if (process.platform === "win32") {
return;
}
// Create a test script that uses tty.ReadStream with ref/unref
const script = `
const fs = require('fs');
const tty = require('tty');
let ttyFd;
try {
ttyFd = fs.openSync('/dev/tty', 'r');
} catch (err) {
// No TTY available
console.log('NO_TTY');
process.exit(0);
}
const stream = new tty.ReadStream(ttyFd);
// Test that ref/unref methods exist and work
if (typeof stream.ref !== 'function' || typeof stream.unref !== 'function') {
console.error('ref/unref methods missing');
process.exit(1);
}
// Unref should allow process to exit
stream.unref();
// Set a timer that would keep process alive if ref() was called
const timer = setTimeout(() => {
console.log('TIMEOUT');
}, 100);
timer.unref();
// Process should exit immediately since both stream and timer are unref'd
console.log('SUCCESS');
// Clean up properly
stream.destroy();
`;
// Write the test script to a temporary file
const path = require("path");
const os = require("os");
const tempFile = path.join(os.tmpdir(), "test-tty-ref-unref-" + Date.now() + ".js");
await Bun.write(tempFile, script);
// Run the script with bun
const proc = Bun.spawn({
cmd: [bunExe(), tempFile],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [exitCode, stdout, stderr] = await Promise.all([proc.exited, proc.stdout.text(), proc.stderr.text()]);
if (stdout.includes("NO_TTY")) {
// No TTY available in test environment, skip
return;
}
expect(stderr).toBe("");
expect(exitCode).toBe(0);
expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(`"SUCCESS"`);
});