-
Notifications
You must be signed in to change notification settings - Fork 4.2k
fix: add default timeout for terminal command tool execution #10550
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
amabito
wants to merge
1
commit into
continuedev:main
Choose a base branch
from
amabito:fix/runTerminalCommand-default-timeout
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.
Open
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
186 changes: 186 additions & 0 deletions
186
core/tools/implementations/runTerminalCommand.timeout.vitest.ts
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,186 @@ | ||
| import { EventEmitter } from "node:events"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { IDE, ToolExtras } from "../.."; | ||
| import { runTerminalCommandImpl } from "./runTerminalCommand"; | ||
|
|
||
| // Hoist mock function so it can be referenced in vi.mock factory | ||
| const { mockSpawn } = vi.hoisted(() => ({ | ||
| mockSpawn: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("node:child_process", () => ({ | ||
| default: { | ||
| spawn: mockSpawn, | ||
| }, | ||
| spawn: mockSpawn, | ||
| })); | ||
|
|
||
| describe("runTerminalCommand timeout functionality", () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| let mockChildProc: any; | ||
| let mockGetIdeInfo: ReturnType<typeof vi.fn>; | ||
| let mockGetWorkspaceDirs: ReturnType<typeof vi.fn>; | ||
| let mockOnPartialOutput: ReturnType<typeof vi.fn>; | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| let setTimeoutSpy: any; | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| let clearTimeoutSpy: any; | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetAllMocks(); | ||
| vi.clearAllTimers(); | ||
| vi.useFakeTimers(); | ||
|
|
||
| // Spy on setTimeout and clearTimeout | ||
| setTimeoutSpy = vi.spyOn(global, "setTimeout"); | ||
| clearTimeoutSpy = vi.spyOn(global, "clearTimeout"); | ||
|
|
||
| // Create mock child process with EventEmitter behavior | ||
| mockChildProc = new EventEmitter(); | ||
| mockChildProc.stdout = new EventEmitter(); | ||
| mockChildProc.stderr = new EventEmitter(); | ||
| mockChildProc.killed = false; | ||
| mockChildProc.kill = vi.fn((signal?: NodeJS.Signals) => { | ||
| mockChildProc.killed = true; | ||
| setTimeout(() => { | ||
| mockChildProc.emit("close", signal === "SIGKILL" ? 137 : 143); | ||
| }, 100); | ||
| return true; | ||
| }); | ||
|
|
||
| mockSpawn.mockReturnValue(mockChildProc); | ||
|
|
||
| mockGetIdeInfo = vi.fn().mockResolvedValue({ remoteName: "local" }); | ||
| mockGetWorkspaceDirs = vi | ||
| .fn() | ||
| .mockResolvedValue(["file:///tmp/test-workspace"]); | ||
| mockOnPartialOutput = vi.fn(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| const createMockExtras = ( | ||
| overrides: Partial<ToolExtras> = {}, | ||
| ): ToolExtras => { | ||
| const mockIde = { | ||
| getIdeInfo: mockGetIdeInfo, | ||
| getWorkspaceDirs: mockGetWorkspaceDirs, | ||
| getIdeSettings: vi.fn(), | ||
| getDiff: vi.fn(), | ||
| readFile: vi.fn(), | ||
| readRangeInFile: vi.fn(), | ||
| isTelemetryEnabled: vi.fn(), | ||
| getProblems: vi.fn(), | ||
| subprocess: vi.fn(), | ||
| getWorkspaceConfigs: vi.fn(), | ||
| showToast: vi.fn(), | ||
| listWorkspaceContents: vi.fn(), | ||
| getTerminalContents: vi.fn(), | ||
| listFolders: vi.fn(), | ||
| getSessionId: vi.fn(), | ||
| runCommand: vi.fn(), | ||
| showLines: vi.fn(), | ||
| saveFile: vi.fn(), | ||
| getBranch: vi.fn(), | ||
| showDiff: vi.fn(), | ||
| getOpenFiles: vi.fn(), | ||
| showVirtualFile: vi.fn(), | ||
| openFile: vi.fn(), | ||
| getRepo: vi.fn(), | ||
| pathSep: vi.fn(), | ||
| fileExists: vi.fn(), | ||
| } as unknown as IDE; | ||
|
|
||
| return { | ||
| ide: mockIde, | ||
| llm: {} as any, | ||
| fetch: vi.fn() as any, | ||
| tool: {} as any, | ||
| config: {} as any, | ||
| onPartialOutput: mockOnPartialOutput, | ||
| toolCallId: "test-tool-call-id", | ||
| ...overrides, | ||
| }; | ||
| }; | ||
|
|
||
| it("should set up timeout when waitForCompletion is true", async () => { | ||
| const extras = createMockExtras(); | ||
| const args = { command: "echo test", waitForCompletion: true }; | ||
| const resultPromise = runTerminalCommandImpl(args, extras); | ||
| await vi.runOnlyPendingTimersAsync(); | ||
| expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), 120_000); | ||
| mockChildProc.emit("close", 0); | ||
| await resultPromise; | ||
| expect(clearTimeoutSpy).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should NOT set up timeout when waitForCompletion is false", async () => { | ||
| const extras = createMockExtras(); | ||
| const args = { command: "sleep 10", waitForCompletion: false }; | ||
| const result = await runTerminalCommandImpl(args, extras); | ||
| const timeoutCalls = setTimeoutSpy.mock.calls.filter( | ||
| (call: any[]) => call[1] === 120_000, | ||
| ); | ||
| expect(timeoutCalls.length).toBe(0); | ||
| expect(result[0].status).toContain("background"); | ||
| }); | ||
|
|
||
| it("should kill process when timeout fires (streaming)", async () => { | ||
| const extras = createMockExtras(); | ||
| const args = { command: "sleep 300", waitForCompletion: true }; | ||
| const resultPromise = runTerminalCommandImpl(args, extras); | ||
| await vi.runOnlyPendingTimersAsync(); | ||
| await vi.advanceTimersByTimeAsync(120_000); | ||
| expect(mockChildProc.kill).toHaveBeenCalledWith("SIGTERM"); | ||
| await vi.advanceTimersByTimeAsync(5_000); | ||
| await vi.runAllTimersAsync(); | ||
| const result = await resultPromise; | ||
| expect(result[0].content).toContain( | ||
| "[Timeout: process killed after 2 minutes]", | ||
| ); | ||
| }); | ||
|
|
||
| it("should clear timeout on normal process exit", async () => { | ||
| const extras = createMockExtras(); | ||
| const args = { command: "echo quick", waitForCompletion: true }; | ||
| const resultPromise = runTerminalCommandImpl(args, extras); | ||
| // Flush async setup (getIdeInfo, getWorkspaceDirs) without advancing timer time | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| mockChildProc.stdout.emit("data", Buffer.from("quick\n")); | ||
| mockChildProc.emit("close", 0); | ||
| await resultPromise; | ||
| expect(clearTimeoutSpy).toHaveBeenCalled(); | ||
| expect(mockChildProc.kill).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should clear SIGKILL timeout when process exits between SIGTERM and SIGKILL grace period", async () => { | ||
| const extras = createMockExtras(); | ||
| const args = { command: "sleep 300", waitForCompletion: true }; | ||
| const resultPromise = runTerminalCommandImpl(args, extras); | ||
|
|
||
| // Let initial setup complete | ||
| await vi.runOnlyPendingTimersAsync(); | ||
|
|
||
| // Advance to trigger main timeout (120s) — SIGTERM sent, SIGKILL timer started | ||
| await vi.advanceTimersByTimeAsync(120_000); | ||
| expect(mockChildProc.kill).toHaveBeenCalledWith("SIGTERM"); | ||
| expect(mockChildProc.kill).toHaveBeenCalledTimes(1); | ||
|
|
||
| // Process exits gracefully after 2 seconds (before 5s grace period) | ||
| await vi.advanceTimersByTimeAsync(2_000); | ||
| mockChildProc.emit("close", 143); // SIGTERM exit code | ||
|
|
||
| // Wait for promise to resolve | ||
| await resultPromise; | ||
|
|
||
| // Advance past the SIGKILL grace period (3 more seconds) | ||
| await vi.advanceTimersByTimeAsync(3_000); | ||
|
|
||
| // Verify SIGKILL was NOT called (timer was cleared) | ||
| expect(mockChildProc.kill).toHaveBeenCalledTimes(1); // Only SIGTERM | ||
| expect(mockChildProc.kill).not.toHaveBeenCalledWith("SIGKILL"); | ||
| }); | ||
| }); | ||
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 |
|---|---|---|
|
|
@@ -2,6 +2,10 @@ import iconv from "iconv-lite"; | |
| import childProcess from "node:child_process"; | ||
| import os from "node:os"; | ||
| import { ContinueError, ContinueErrorReason } from "../../util/errors"; | ||
|
|
||
| // Default timeout for terminal commands (2 minutes) | ||
| const DEFAULT_TOOL_TIMEOUT_MS = 120_000; | ||
|
|
||
| // Automatically decode the buffer according to the platform to avoid garbled Chinese | ||
| function getDecodedOutput(data: Buffer): string { | ||
| if (process.platform === "win32") { | ||
|
|
@@ -133,6 +137,8 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { | |
|
|
||
| return new Promise((resolve, reject) => { | ||
| let terminalOutput = ""; | ||
| let timeoutId: ReturnType<typeof setTimeout> | undefined; | ||
| let sigkillTimeoutId: ReturnType<typeof setTimeout> | undefined; | ||
|
|
||
| if (!waitForCompletion) { | ||
| const status = "Command is running in the background..."; | ||
|
|
@@ -168,6 +174,41 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { | |
| ); | ||
| } | ||
|
|
||
| // Set up timeout for waitForCompletion mode | ||
| if (waitForCompletion) { | ||
| timeoutId = setTimeout(() => { | ||
cubic-dev-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!childProc.killed) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: SIGKILL fallback never runs because childProc.killed becomes true immediately after SIGTERM; it does not indicate process exit. Use exitCode/signalCode to detect whether the process is still running. Prompt for AI agents |
||
| terminalOutput += | ||
| "\n[Timeout: process killed after 2 minutes]\n"; | ||
|
|
||
| // Update UI with timeout message | ||
| if (extras.onPartialOutput) { | ||
| extras.onPartialOutput({ | ||
| toolCallId, | ||
| contextItems: [ | ||
| { | ||
| name: "Terminal", | ||
| description: "Terminal command output", | ||
| content: terminalOutput, | ||
| status: "Command timed out", | ||
| }, | ||
| ], | ||
| }); | ||
| } | ||
|
|
||
| // Try graceful termination first | ||
| childProc.kill("SIGTERM"); | ||
|
|
||
| // Force kill after 5 seconds if still running | ||
| sigkillTimeoutId = setTimeout(() => { | ||
| if (!childProc.killed) { | ||
| childProc.kill("SIGKILL"); | ||
| } | ||
| }, 5_000); | ||
| } | ||
| }, DEFAULT_TOOL_TIMEOUT_MS); | ||
| } | ||
|
|
||
| childProc.stdout?.on("data", (data) => { | ||
| // Skip if this process has been backgrounded | ||
| if (isProcessBackgrounded(toolCallId)) return; | ||
|
|
@@ -240,6 +281,16 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { | |
| } | ||
|
|
||
| childProc.on("close", (code) => { | ||
| // Clear timeout on normal completion | ||
| if (timeoutId) { | ||
| clearTimeout(timeoutId); | ||
| } | ||
|
|
||
| // Clear inner SIGKILL timeout if process exits before grace period | ||
| if (sigkillTimeoutId) { | ||
| clearTimeout(sigkillTimeoutId); | ||
| } | ||
|
|
||
| // Clean up process tracking | ||
| if (toolCallId) { | ||
| if (isProcessBackgrounded(toolCallId)) { | ||
|
|
@@ -296,6 +347,11 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { | |
| }); | ||
|
|
||
| childProc.on("error", (error) => { | ||
| // Clear timeout on error | ||
| if (timeoutId) { | ||
| clearTimeout(timeoutId); | ||
| } | ||
|
|
||
| // Clean up process tracking | ||
| if (toolCallId) { | ||
| if (isProcessBackgrounded(toolCallId)) { | ||
|
|
@@ -325,6 +381,9 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { | |
| getShellCommand(command); | ||
| const output = await new Promise<{ stdout: string; stderr: string }>( | ||
| (resolve, reject) => { | ||
| let timeoutId: ReturnType<typeof setTimeout> | undefined; | ||
| let sigkillTimeoutId: ReturnType<typeof setTimeout> | undefined; | ||
|
|
||
| const childProc = childProcess.spawn( | ||
| nonStreamingShell, | ||
| nonStreamingArgs, | ||
|
|
@@ -342,6 +401,23 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { | |
| let stdout = ""; | ||
| let stderr = ""; | ||
|
|
||
| // Set up timeout | ||
| timeoutId = setTimeout(() => { | ||
| if (!childProc.killed) { | ||
| stderr += "\n[Timeout: process killed after 2 minutes]\n"; | ||
|
|
||
| // Try graceful termination first | ||
| childProc.kill("SIGTERM"); | ||
|
|
||
| // Force kill after 5 seconds if still running | ||
| sigkillTimeoutId = setTimeout(() => { | ||
| if (!childProc.killed) { | ||
| childProc.kill("SIGKILL"); | ||
| } | ||
| }, 5_000); | ||
| } | ||
| }, DEFAULT_TOOL_TIMEOUT_MS); | ||
|
|
||
| childProc.stdout?.on("data", (data) => { | ||
| stdout += getDecodedOutput(data); | ||
| }); | ||
|
|
@@ -351,6 +427,16 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { | |
| }); | ||
|
|
||
| childProc.on("close", (code) => { | ||
| // Clear outer timeout | ||
| if (timeoutId) { | ||
| clearTimeout(timeoutId); | ||
| } | ||
|
|
||
| // Clear inner SIGKILL timeout if process exits before grace period | ||
| if (sigkillTimeoutId) { | ||
| clearTimeout(sigkillTimeoutId); | ||
| } | ||
|
|
||
| // Clean up process tracking | ||
| if (toolCallId) { | ||
| removeRunningProcess(toolCallId); | ||
|
|
||
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.