Skip to content

Fix early task termination and lost task end after terminal reuse - #351260

Merged
Megan Rogge (meganrogge) merged 2 commits into
mainfrom
agents/fix-task-lifecycle-bugs-vscode
Nov 30, 2026
Merged

Megan Rogge (meganrogge) merged 2 commits into
mainfrom
agents/fix-task-lifecycle-bugs-vscode

Conversation

@meganrogge

Copy link
Copy Markdown
Collaborator

Fixes #350588
Fixes #350590

Changes

#350588: TaskExecution.terminate() before the task starts is dropped

executeTask in the extension host does not wait for the main thread. A terminate() called right after executeTask can therefore reach the main thread before the task is resolved, run, or attached to its terminal. The main thread only looked through active tasks, which need a terminal. It rejected the call with "Task to terminate not found" and the task kept running.

  • MainThreadTask now remembers executions that were requested but haven't started yet. A terminate for one of them is recorded. It is applied as soon as the task is resolved and again when the task system reports CustomExecutionAccepted/Start for it, using the task instance the task system actually runs.
  • TerminalTaskSystem.terminate no longer returns success: false when a task is known but its terminal isn't attached yet. It keeps a pending termination and stops the task as soon as the terminal is attached. If the task ends first, the pending termination resolves.

#350590: reusing a dedicated task terminal after a nonzero exit loses the next end event

When a task exits with a nonzero code, the terminal stays open for reuse, but _isExiting stayed true. reuseTerminal cleared it only for waitOnExit terminals. The next process's exit was then ignored, so onExit never fired and the task never ended. reuseTerminal now always clears the exiting state.

How to test

New unit tests reproduce each bug; each one fails without the fix:

  • TerminalTaskSystem terminate: stops a task whose terminate was requested before its terminal was attached
  • MainThreadTask terminate: terminating an execution before the task has started stops it once the task system accepts it
  • TerminalInstance: reusing a task terminal after a failed exit reports the next exit

To check manually: in an extension, call const e = await vscode.tasks.executeTask(task); e.terminate();. The task should stop instead of running on. Then run a dedicated-panel task that fails, rerun it in the same terminal, and check that onDidEndTaskProcess fires for the second run.

Validation

  • ./scripts/test.sh --run src/vs/workbench/contrib/tasks/test/browser/terminalTaskSystem.test.ts --run src/vs/workbench/api/test/browser/mainThreadTask.test.ts --run src/vs/workbench/contrib/terminal/test/browser/terminalInstance.test.ts: passed (142 passing). With the source changes reverted, the 3 new tests fail.
  • pnpm run typecheck-client: passed
  • pnpm run oxlint <changed files>: passed

- Apply TaskExecution.terminate() requested before a task has started once the task system accepts it, and stop the task as soon as its terminal is attached instead of dropping the request.
- Reset the terminal's exiting state when reusing a task terminal, so the exit of the next process is reported after a previous nonzero exit.

Fixes #350588
Fixes #350590

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 30, 2026 17:25
@meganrogge Megan Rogge (meganrogge) added this to the 1.150.0 milestone Nov 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Task execution tracking conflates concurrent instances, so terminating an API execution can target an unrelated UI instance or silently fail when the instance limit is reached.

2 open issues
What changed in this PR

This PR addresses early task termination through the extension API (#350588) and missing task-end events after reusing a failed task’s terminal (#350590).

Changes:

  • Tracks pending executions and retries termination when the task system accepts them.
  • Defers termination until a task’s terminal is attached.
  • Resets terminal exit state on reuse and adds regression tests for all three paths.
File Description
src/​vs/​workbench/​contrib/​terminal/​test/​browser/​terminalInstance.test.ts Tests exit events after reusing a failed task terminal.
src/​vs/​workbench/​contrib/​terminal/​browser/​terminalInstance.ts Clears exit state unconditionally on terminal reuse.
src/​vs/​workbench/​contrib/​tasks/​test/​browser/​terminalTaskSystem.test.ts Tests termination requested before terminal attachment.
src/​vs/​workbench/​contrib/​tasks/​browser/​terminalTaskSystem.ts Retains pending termination until attachment or task completion.
src/​vs/​workbench/​api/​test/​browser/​mainThreadTask.test.ts Tests early termination before task acceptance.
src/​vs/​workbench/​api/​browser/​mainThreadTask.ts Tracks requested executions and retries early termination.

Review effort: Balanced

Report the instance a run executes (newly started, or the active one when the
instance limit is reached) through a run option, and apply early termination
to that instance only instead of retargeting on any start event with the same
task id.

Co-authored-by: Copilot <[email protected]>
@meganrogge
Megan Rogge (meganrogge) added this pull request to the merge queue Nov 30, 2026
Merged via the queue into main with commit 4bff655 Nov 30, 2026
53 checks passed
@meganrogge
Megan Rogge (meganrogge) deleted the agents/fix-task-lifecycle-bugs-vscode branch November 30, 2026 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

5 participants