-
Notifications
You must be signed in to change notification settings - Fork 22
🤖 feat: add local background bash process execution #749
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
Open
ethanndickson
wants to merge
13
commits into
main
Choose a base branch
from
bash-background-execution-param
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,125
−27
Open
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
78e19d3
🤖 feat: add background bash process execution
ethanndickson ef2bc7b
🤖 fix: block background execution for SSH/remote workspaces
ethanndickson e252065
🤖 fix: preserve killed status when onExit callback fires
ethanndickson d681ceb
🤖 fix: convert signal-terminated processes to non-zero exit codes
ethanndickson caf0c3e
🤖 refactor: extract BackgroundExecutor interface for runtime-agnostic…
ethanndickson 2dd5e75
🤖 fix: register background executors for existing workspaces after re…
ethanndickson fbb4a9d
🤖 refactor: lazy executor pattern for background processes
ethanndickson 8a7d8f7
🤖 refactor: move background execution to Runtime interface
ethanndickson 8bcee44
🤖 refactor: gate background tools and extract LocalBackgroundHandle
ethanndickson d39f405
🤖 fix: report correct exit codes for signal-terminated background pro…
ethanndickson 51dfcfd
🤖 fix: correctly detect signal-killed processes in isRunning()
ethanndickson c5571f9
fix: use separate TextDecoders per stream in background process
ethanndickson a57049c
fix: terminate background processes on app quit
ethanndickson 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
Some comments aren't visible on the classic Files Changed page.
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
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
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,124 @@ | ||
| import type { BackgroundHandle } from "./Runtime"; | ||
| import type { DisposableProcess } from "@/node/utils/disposableExec"; | ||
| import { log } from "@/node/services/log"; | ||
|
|
||
| /** | ||
| * Handle to a local background process. | ||
| * | ||
| * Buffers early events until callbacks are registered, since the manager | ||
| * registers callbacks after spawn() returns (but output may arrive before). | ||
| */ | ||
| export class LocalBackgroundHandle implements BackgroundHandle { | ||
| private stdoutCallback?: (line: string) => void; | ||
| private stderrCallback?: (line: string) => void; | ||
| private exitCallback?: (exitCode: number) => void; | ||
| private terminated = false; | ||
|
|
||
| // Buffers for events that arrive before callbacks are registered | ||
| private pendingStdout: string[] = []; | ||
| private pendingStderr: string[] = []; | ||
| private pendingExitCode?: number; | ||
|
|
||
| constructor(private readonly disposable: DisposableProcess) {} | ||
|
|
||
| onStdout(callback: (line: string) => void): void { | ||
| this.stdoutCallback = callback; | ||
| // Flush buffered events | ||
| for (const line of this.pendingStdout) { | ||
| callback(line); | ||
| } | ||
| this.pendingStdout = []; | ||
| } | ||
|
|
||
| onStderr(callback: (line: string) => void): void { | ||
| this.stderrCallback = callback; | ||
| // Flush buffered events | ||
| for (const line of this.pendingStderr) { | ||
| callback(line); | ||
| } | ||
| this.pendingStderr = []; | ||
| } | ||
|
|
||
| onExit(callback: (exitCode: number) => void): void { | ||
| this.exitCallback = callback; | ||
| // Flush buffered event | ||
| if (this.pendingExitCode !== undefined) { | ||
| callback(this.pendingExitCode); | ||
| this.pendingExitCode = undefined; | ||
| } | ||
| } | ||
|
|
||
| /** Internal: called when stdout line arrives */ | ||
| _emitStdout(line: string): void { | ||
| if (this.stdoutCallback) { | ||
| this.stdoutCallback(line); | ||
| } else { | ||
| this.pendingStdout.push(line); | ||
| } | ||
| } | ||
|
|
||
| /** Internal: called when stderr line arrives */ | ||
| _emitStderr(line: string): void { | ||
| if (this.stderrCallback) { | ||
| this.stderrCallback(line); | ||
| } else { | ||
| this.pendingStderr.push(line); | ||
| } | ||
| } | ||
|
|
||
| /** Internal: called when process exits */ | ||
| _emitExit(exitCode: number): void { | ||
| if (this.exitCallback) { | ||
| this.exitCallback(exitCode); | ||
| } else { | ||
| this.pendingExitCode = exitCode; | ||
| } | ||
| } | ||
|
|
||
| isRunning(): Promise<boolean> { | ||
| return Promise.resolve(this.disposable.underlying.exitCode === null); | ||
| } | ||
ethanndickson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| async terminate(): Promise<void> { | ||
| if (this.terminated) return; | ||
|
|
||
| const pid = this.disposable.underlying.pid; | ||
| if (pid === undefined) { | ||
| this.terminated = true; | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| // Send SIGTERM to the process group for graceful shutdown | ||
| // Use negative PID to kill the entire process group (detached processes are group leaders) | ||
| const pgid = -pid; | ||
| log.debug(`LocalBackgroundHandle: Sending SIGTERM to process group (PGID: ${pgid})`); | ||
| process.kill(pgid, "SIGTERM"); | ||
|
|
||
| // Wait 2 seconds for graceful shutdown | ||
| await new Promise((resolve) => setTimeout(resolve, 2000)); | ||
|
|
||
| // Check if process is still running | ||
| if (await this.isRunning()) { | ||
| log.debug(`LocalBackgroundHandle: Process still running, sending SIGKILL`); | ||
| process.kill(pgid, "SIGKILL"); | ||
| } | ||
| } catch (error) { | ||
| // Process may already be dead - that's fine | ||
| log.debug( | ||
| `LocalBackgroundHandle: Error during terminate: ${error instanceof Error ? error.message : String(error)}` | ||
| ); | ||
| } | ||
|
|
||
| this.terminated = true; | ||
| } | ||
|
|
||
| dispose(): Promise<void> { | ||
| return Promise.resolve(this.disposable[Symbol.dispose]()); | ||
| } | ||
|
|
||
| /** Get the underlying child process (for spawn event waiting) */ | ||
| get child() { | ||
| return this.disposable.underlying; | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.