-
-
Notifications
You must be signed in to change notification settings - Fork 158
fix(child_process): unblock OpenCode writable pipelines #8531
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| ### Fixed | ||
|
|
||
| - Child-process stdin writables now invoke completion callbacks for Node's | ||
| `write` and `end` overloads on a later event-loop turn, preventing OpenCode | ||
| LSP and formatter pipelines from stalling while they await a completed write. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
crates/perry/tests/issue_8512_opencode_process_lifecycle.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| //! Regression coverage for OpenCode's child-process writable path (#8512). | ||
| //! | ||
| //! OpenCode adapts `child.stdin` through Effect's `NodeSink.fromWritable` for | ||
| //! LSP framing and process pipelines. That adapter waits for Node's optional | ||
| //! `write` / `end` completion callbacks. Perry used to perform the pipe write | ||
| //! but drop those callbacks, leaving the sink (and therefore the LSP or | ||
| //! formatter lifecycle) pending forever. | ||
|
|
||
| #![cfg(unix)] | ||
|
|
||
| use std::io::Read; | ||
| use std::path::PathBuf; | ||
| use std::process::{Command, Stdio}; | ||
| use std::time::{Duration, Instant}; | ||
|
|
||
| fn perry_bin() -> PathBuf { | ||
| PathBuf::from(env!("CARGO_BIN_EXE_perry")) | ||
| } | ||
|
|
||
| const FIXTURE: &str = r#" | ||
| import { spawn } from "node:child_process"; | ||
|
|
||
| async function completion(register: (done: () => void) => void): Promise<boolean> { | ||
| let synchronous = true; | ||
| const asynchronous = await new Promise<boolean>((resolve) => { | ||
| register(() => resolve(!synchronous)); | ||
| synchronous = false; | ||
| }); | ||
| return asynchronous; | ||
| } | ||
|
|
||
| async function main() { | ||
| const childCwd = process.env.OPENCODE_CHILD_CWD as string; | ||
| const child = spawn("sh", ["-c", 'printf "%s\\n%s\\n" "$PWD" "$OPENCODE_LSP"; cat'], { | ||
| cwd: childCwd, | ||
| env: { PATH: process.env.PATH, OPENCODE_LSP: "ready" }, | ||
| stdio: ["pipe", "pipe", "pipe"], | ||
| }); | ||
|
|
||
| let stdout = ""; | ||
| child.stdout!.on("data", (chunk: Buffer) => { | ||
| stdout += chunk.toString("utf8"); | ||
| }); | ||
| const closed = new Promise<{ code: number | null; signal: string | null }>((resolve) => { | ||
| child.once("close", (code: number | null, signal: string | null) => resolve({ code, signal })); | ||
| }); | ||
|
|
||
| const shortWriteAsync = await completion((done) => child.stdin!.write("frame-one\n", done)); | ||
| const encodedWriteAsync = await completion((done) => child.stdin!.write("frame-two\n", "utf8", done)); | ||
| const endAsync = await completion((done) => child.stdin!.end(done)); | ||
| const result = await closed; | ||
|
|
||
| console.log("WRITE_CB_ASYNC:" + shortWriteAsync); | ||
| console.log("ENCODED_WRITE_CB_ASYNC:" + encodedWriteAsync); | ||
| console.log("END_CB_ASYNC:" + endAsync); | ||
| console.log("EXIT:" + result.code + ":" + result.signal); | ||
| console.log("CWD:" + stdout.startsWith(childCwd + "\n")); | ||
| console.log("ENV:" + stdout.includes("\nready\n")); | ||
| console.log("FRAMES:" + stdout.includes("frame-one\nframe-two\n")); | ||
| } | ||
|
|
||
| main(); | ||
| "#; | ||
|
|
||
| #[test] | ||
| fn opencode_lsp_writable_callbacks_complete() { | ||
| let dir = tempfile::tempdir().expect("tempdir"); | ||
| let entry = dir.path().join("main.ts"); | ||
| let output = dir.path().join("main_bin"); | ||
| let child_cwd = dir.path().join("child-cwd"); | ||
| std::fs::create_dir(&child_cwd).expect("create distinct child cwd"); | ||
| let child_cwd = std::fs::canonicalize(child_cwd).expect("canonicalize child cwd"); | ||
| std::fs::write(&entry, FIXTURE).expect("write fixture"); | ||
|
|
||
| let compile = Command::new(perry_bin()) | ||
| .current_dir(dir.path()) | ||
| .arg("compile") | ||
| .arg(&entry) | ||
| .arg("-o") | ||
| .arg(&output) | ||
| .output() | ||
| .expect("run perry compile"); | ||
| assert!( | ||
| compile.status.success(), | ||
| "perry compile failed:\nstdout: {}\nstderr: {}", | ||
| String::from_utf8_lossy(&compile.stdout), | ||
| String::from_utf8_lossy(&compile.stderr) | ||
| ); | ||
|
|
||
| // A dropped completion callback leaves `cat` alive with stdin open, so use | ||
| // a hard timeout to turn that historical hang into a deterministic test | ||
| // failure. | ||
| let mut child = Command::new(&output) | ||
| .current_dir(dir.path()) | ||
| .env("OPENCODE_CHILD_CWD", &child_cwd) | ||
| .stdout(Stdio::piped()) | ||
| .stderr(Stdio::piped()) | ||
| .spawn() | ||
| .expect("run compiled fixture"); | ||
| let mut stdout_pipe = child.stdout.take().expect("stdout pipe"); | ||
| let mut stderr_pipe = child.stderr.take().expect("stderr pipe"); | ||
| let stdout_reader = std::thread::spawn(move || { | ||
| let mut buf = String::new(); | ||
| let _ = stdout_pipe.read_to_string(&mut buf); | ||
| buf | ||
| }); | ||
| let stderr_reader = std::thread::spawn(move || { | ||
| let mut buf = String::new(); | ||
| let _ = stderr_pipe.read_to_string(&mut buf); | ||
| buf | ||
| }); | ||
|
|
||
| let start = Instant::now(); | ||
| let status = loop { | ||
| if let Some(status) = child.try_wait().expect("poll compiled fixture") { | ||
| break status; | ||
| } | ||
| if start.elapsed() > Duration::from_secs(15) { | ||
| let _ = child.kill(); | ||
| let _ = child.wait(); | ||
| let stdout = stdout_reader.join().expect("stdout reader"); | ||
| let stderr = stderr_reader.join().expect("stderr reader"); | ||
| panic!( | ||
| "compiled fixture hung waiting for a child stdin callback:\nstdout: {stdout}\nstderr: {stderr}" | ||
| ); | ||
| } | ||
| std::thread::sleep(Duration::from_millis(25)); | ||
| }; | ||
|
|
||
| let stdout = stdout_reader.join().expect("stdout reader"); | ||
| let stderr = stderr_reader.join().expect("stderr reader"); | ||
| assert!( | ||
| status.success(), | ||
| "compiled fixture failed ({status:?}):\nstdout: {stdout}\nstderr: {stderr}" | ||
| ); | ||
| for expected in [ | ||
| "WRITE_CB_ASYNC:true", | ||
| "ENCODED_WRITE_CB_ASYNC:true", | ||
| "END_CB_ASYNC:true", | ||
| "EXIT:0:null", | ||
| "CWD:true", | ||
| "ENV:true", | ||
| "FRAMES:true", | ||
| ] { | ||
| assert!( | ||
| stdout.contains(expected), | ||
| "missing {expected:?} in {stdout:?}" | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 10893
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 50370
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 18525
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 50372
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 50369
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 50372
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 2348
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 42320
Root all GC-managed values before
cp_handle_of.cp_handle_ofallocates the__cpHandlekey throughstring_storage_alloc, which can move objects. Both methods use barethisand select the callback after this call.cp_method_stdin_endalso reuseschunk. Create oneRuntimeHandleScopeat method entry and use refreshed handles throughcp_defer_stream_callback.🤖 Prompt for AI Agents
Sources: Coding guidelines, Learnings