Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): support process.stdin.unref() #22865

Merged
merged 4 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 30 additions & 2 deletions ext/io/12_io.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { op_set_raw } from "ext:core/ops";
const {
Uint8Array,
ArrayPrototypePush,
Symbol,
TypedArrayPrototypeSubarray,
TypedArrayPrototypeSet,
TypedArrayPrototypeGetByteLength,
Expand Down Expand Up @@ -181,9 +182,14 @@ const STDIN_RID = 0;
const STDOUT_RID = 1;
const STDERR_RID = 2;

const REF = Symbol("REF");
const UNREF = Symbol("UNREF");

class Stdin {
#rid = STDIN_RID;
#ref = true;
#readable;
#opPromise;

constructor() {
}
Expand All @@ -197,8 +203,14 @@ class Stdin {
return this.#rid;
}

read(p) {
return read(this.#rid, p);
async read(p) {
if (p.length === 0) return 0;
this.#opPromise = core.read(this.#rid, p);
if (!this.#ref) {
core.unrefOpPromise(this.#opPromise);
}
const nread = await this.#opPromise;
return nread === 0 ? null : nread;
}

readSync(p) {
Expand All @@ -224,6 +236,20 @@ class Stdin {
isTerminal() {
return core.isTerminal(this.#rid);
}

[REF]() {
this.#ref = true;
if (this.#opPromise) {
core.refOpPromise(this.#opPromise);
}
}

[UNREF]() {
this.#ref = false;
if (this.#opPromise) {
core.unrefOpPromise(this.#opPromise);
}
}
}

class Stdout {
Expand Down Expand Up @@ -318,6 +344,7 @@ export {
readAll,
readAllSync,
readSync,
REF,
SeekMode,
Stderr,
stderr,
Expand All @@ -327,6 +354,7 @@ export {
Stdout,
stdout,
STDOUT_RID,
UNREF,
write,
writeSync,
};
13 changes: 12 additions & 1 deletion ext/node/polyfills/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const {
} = core;

import { ERR_INVALID_FD } from "ext:deno_node/internal/errors.ts";
import { LibuvStreamWrap } from "ext:deno_node/internal_binding/stream_wrap.ts";
import {
kStreamBaseField,
LibuvStreamWrap,
} from "ext:deno_node/internal_binding/stream_wrap.ts";
import { providerType } from "ext:deno_node/internal_binding/async_wrap.ts";
import { Socket } from "node:net";
import { setReadStream } from "ext:deno_node/_process/streams.mjs";
Expand All @@ -36,6 +39,14 @@ class TTY extends LibuvStreamWrap {
constructor(handle) {
super(providerType.TTYWRAP, handle);
}

ref() {
this[kStreamBaseField][io.REF]();
}

unref() {
this[kStreamBaseField][io.UNREF]();
}
}

export class ReadStream extends Socket {
Expand Down
29 changes: 29 additions & 0 deletions tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5359,3 +5359,32 @@ fn code_cache_npm_with_require_test() {
assert!(!output.stderr().contains("Updating V8 code cache"));
}
}

#[test]
fn node_process_stdin_unref_with_pty() {
TestContext::default()
.new_command()
.args_vec(["run", "--quiet", "run/node_process_stdin_unref_with_pty.js"])
.with_pty(|mut console| {
console.expect("START\r\n");
console.write_line("foo");
console.expect("foo\r\n");
console.write_line("bar");
console.expect("bar\r\n");
console.write_line("baz");
console.expect("baz\r\n");
});

TestContext::default()
.new_command()
.args_vec([
"run",
"--quiet",
"run/node_process_stdin_unref_with_pty.js",
"--unref",
])
.with_pty(|mut console| {
// if process.stdin.unref is called, the program immediately ends by skipping reading from stdin.
console.expect("START\r\nEND\r\n");
});
}
14 changes: 14 additions & 0 deletions tests/testdata/run/node_process_stdin_unref_with_pty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import process from "node:process";
import util from "node:util";

console.log("START");
globalThis.addEventListener("unload", () => console.log("END"));

const args = util.parseArgs({ options: { unref: { type: "boolean" } } });

// call stdin.unref if --unref is passed
if (args.values.unref) {
process.stdin.unref();
}

process.stdin.pipe(process.stdout);