Skip to content

There is a race in task startup in terminalTaskService.ts #180541

@markw65

Description

@markw65

Type: Bug

The race was reported in #159640 and partially fixed by #165560.

The fix doesn't fully cover the issue though. The problem is that the task identifier is added to _inProgressTasks just before terminalTaskSystem._executeTask() is called, and removed just after. But _executeTask will return (via "await") before updating _activeTasks. So _activeTasks won't be updated until the next tick (at the earliest, and possibly much much later).

This is a problem because although terminalTaskSystem updates _instances immediately (so it knows how many instances of a task are running), it ignores that unless there's an entry in _activeTasks.

As a result, a task can be re-submitted after its name has been removed from _inProgressTasks, but before the first task has been added to _activeTasks, bypass the instanceLimit check, and start a second, invalid instance. And this is the root cause of #159640.

Typically the race can be hard to hit - especially after #165560 - but if the task has a "dependsOn" that runs for more than a few milliseconds, it's easy to repro. Use these commands to create and open a new project with a suitable task.

mkdir -p test/.vscode
cd test
cat <<"EOF" > .vscode/tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "sleep",
      "type": "shell",
      "command": "sleep 2"
    },
    {
      "label": "build",
      "type": "shell",
      "command": "echo 'Running build' && sleep 5 && echo 'Finished build'",
      "dependsOn": ["sleep"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}
EOF
code .

Trust the workspace, then hit Cmd-Shift-B three times in less than 2 seconds (but not too fast, or _inProgressTasks will stop the second and third attempts). You will get three instances of the task running.

VS Code version: Code 1.77.3 (704ed70, 2023-04-12T09:19:37.325Z)
OS version: Darwin x64 22.4.0
Modes:
Sandboxed: No

System Info
Item Value
CPUs Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz (16 x 2300)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
metal: disabled_off
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_renderer: enabled_on
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off
Load (avg) 4, 4, 4
Memory (System) 32.00GB (0.66GB free)
Process Argv --crash-reporter-id e8eaf96d-f4d4-4874-a9d9-08e2c6d99409
Screen Reader no
VM 0%
Extensions (22)
Extension Author (truncated) Version
gltf-vscode ces 2.3.16
vscode-eslint dba 2.4.0
githistory don 0.6.20
gitlens eam 13.5.0
prettier-vscode esb 9.12.0
php-intellisense fel 2.3.14
monkey-c Gar 1.0.8
jq-syntax-highlighting jq- 0.0.2
prettier-extension-monkeyc mar 2.0.61
cpptools ms- 1.14.5
js-debug-nightly ms- 2023.4.1317
peggy-language Peg 2.4.0
java red 1.17.0
vscode-xml red 0.25.0
shader sle 1.1.5
rewrap stk 1.16.3
intellicode-api-usage-examples Vis 0.2.7
vscodeintellicode Vis 1.2.30
vscode-java-debug vsc 0.49.1
vscode-java-pack vsc 0.25.10
vscode-java-test vsc 0.38.2
vscode-maven vsc 0.41.0
A/B Experiments
vsliv368cf:30146710
vsreu685:30147344
python383:30185418
vspor879:30202332
vspor708:30202333
vspor363:30204092
vslsvsres303:30308271
vserr242:30382549
pythontb:30283811
vsjup518:30340749
pythonptprofiler:30281270
vshan820:30294714
vstes263:30335439
vscoreces:30445986
pythondataviewer:30285071
vscod805cf:30301675
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
cmake_vspar411:30581797
vsaa593:30376534
pythonvs932:30410667
cppdebug:30492333
vsclangdf:30486550
c4g48928:30535728
dsvsc012cf:30540253
pynewext54:30695312
azure-dev_surveyone:30548225
nodejswelcome1:30587005
2e4cg342:30602488
pyind779:30671433
f6dab269:30613381
pythonsymbol12:30671437
vsccsb:30705552
functionswalk:30687959
pythonms35:30701012
pythonfmttextcf:30716743

Metadata

Metadata

Assignees

Labels

bugIssue identified by VS Code Team member as probable buginsiders-releasedPatch has been released in VS Code InsiderstasksTask system issuesverifiedVerification succeeded

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions