Repository: vscode
Issue: Task command hangs and doesn't recognize completion of detached processes
Labels: bug,tasks


Type: <b>Bug</b>

## Issue

While trying to setup a debugging launch configuration in VS Code, I noticed an issue where the task system hangs indefinitely when using a shell task command that starts a detached process. My setup involved:
- A Docker Compose service with a custom entrypoint script that performs setup tasks (e.g., uv sync) before starting the main process.
- The task shell command I was using: docker compose up -d. This task was used as a preLaunchTask in a launch configuration meant to attach to a python debugger in the running service started by my task, but given the task never completes, the launch configuration never runs.

The issue seems to appear when there's a delay between when the process starts and when it fully detaches. The task system seems unable to recognize that the command has completed, even though the process has been successfully detached (as evidenced by the container running correctly). I'm not sure if the issue is related to vscode getting confused by the process hierarchy or not properly handling compose's detach signal, but either way it seems like Code's task system might be holding onto the process handle or waiting for some specific signal that never comes.

As a workaround, adding a longer delay after docker compose up -d in the task’s command seems to mitigate the issue:
`{ "command": "docker compose up -d && sleep 3" }`
_Note_: the delay in the task command has to be longer than any delay in the entrypoint script.

## Expected

The task system should handle the container’s initialization period gracefully, even with an entrypoint delay, without requiring explicit workarounds.

## Minimal Repro Steps:

Build the following image, then start a Python Debugger using launch.json. Ensure the service is not up and running before trying to reproduce the hang using the "Debug using launch.json" option and selecting "Python Debugger: Attach".

<details>
<summary>Code</summary>

`requirements.txt`
```
debugpy
pytest
```

`text_example.py`
```
def test_example():
    assert True
```

`entrypoint.sh`
```
#!/bin/bash
sleep 2 # simulate init delay
exec "$@"
```

`docker-compose.yml`
```
services:
  test_runner:
    build: .
    command: "python -m debugpy --listen 0.0.0.0:5679 --wait-for-client -m pytest"
    ports:
      - "5679:5679"
```

`Dockerfile`
```
FROM python:3.10-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY test_example.py .
COPY entrypoint.sh /usr/bin/entrypoint.sh
RUN chmod +x /usr/bin/entrypoint.sh

ENTRYPOINT ["entrypoint.sh"]
```

`launch.json`
```
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Attach",
            "type": "debugpy",
            "request": "attach",
            "preLaunchTask": "Docker Compose Up",
            "connect": {
                "host": "0.0.0.0",
                "port": 5679
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/app"
                }
            ]
        }
    ]
}
```

`tasks.json`
```
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Docker Compose Up",
      "type": "shell",
      "command": "docker compose up -d",
      "problemMatcher": []
    }
  ]
}
```

Version:
```
Version: 1.95.3
Commit: f1a4fb101478ce6ec82fe9627c43efbf9e98c813
Date: 2024-11-13T14:50:04.152Z
Electron: 32.2.1
ElectronBuildId: 10427718
Chromium: 128.0.6613.186
Node.js: 20.18.0
V8: 12.8.374.38-electron.0
OS: Darwin arm64 24.1.0
```

I also verified the issue happens in Visual Studio Code - Insider's Edition:
```
Version: 1.96.0-insider
Commit: a40fbb1ac10ad5c797d331eb75d1d99332ef9cbe
Date: 2024-12-03T10:12:24.933Z
Electron: 32.2.6
ElectronBuildId: 10629634
Chromium: 128.0.6613.186
Node.js: 20.18.1
V8: 12.8.374.38-electron.0
OS: Darwin arm64 24.1.0
```
</details>

VS Code version: Code 1.95.3 (f1a4fb101478ce6ec82fe9627c43efbf9e98c813, 2024-11-13T14:50:04.152Z)
OS version: Darwin arm64 24.1.0
Modes:

<details>
<summary>System Info</summary>

|Item|Value|
|---|---|
|CPUs|Apple M1 Max (10 x 2400)|
|GPU Status|2d_canvas: enabled<br>canvas_oop_rasterization: enabled_on<br>direct_rendering_display_compositor: disabled_off_ok<br>gpu_compositing: enabled<br>multiple_raster_threads: enabled_on<br>opengl: enabled_on<br>rasterization: enabled<br>raw_draw: disabled_off_ok<br>skia_graphite: disabled_off<br>video_decode: enabled<br>video_encode: enabled<br>webgl: enabled<br>webgl2: enabled<br>webgpu: enabled<br>webnn: disabled_off|
|Load (avg)|16, 19, 27|
|Memory (System)|32.00GB (0.40GB free)|
|Process Argv|--crash-reporter-id 63f58570-0bcd-43a0-b6c7-3b3c84beb9ea|
|Screen Reader|no|
|VM|0%|
</details><details><summary>Extensions (17)</summary>

Extension|Author (truncated)|Version
---|---|---
ruff|cha|2024.56.0
gitlens|eam|16.0.4
copilot|Git|1.246.0
copilot-chat|Git|0.22.4
vscode-docker|ms-|1.29.3
debugpy|ms-|2024.12.0
python|ms-|2024.20.0
vscode-pylance|ms-|2024.11.3
jupyter|ms-|2024.10.0
jupyter-keymap|ms-|1.1.2
jupyter-renderers|ms-|1.0.21
vscode-jupyter-cell-tags|ms-|0.1.9
vscode-jupyter-slideshow|ms-|0.1.6
remote-containers|ms-|0.388.0
vscode-typescript-next|ms-|5.8.20241202
one-dark-theme|msk|1.14.2
vim|vsc|1.28.1

(1 theme extensions excluded)

</details><details>
<summary>A/B Experiments</summary>

```
vsliv368cf:30146710
vspor879:30202332
vspor708:30202333
vspor363:30204092
vscod805cf:30301675
binariesv615:30325510
vsaa593cf:30376535
py29gd2263:31024239
c4g48928:30535728
azure-dev_surveyone:30548225
962ge761:30959799
pythonnoceb:30805159
asynctok:30898717
pythonmypyd1:30879173
h48ei257:31000450
pythontbext0:30879054
cppperfnew:31000557
dsvsc020:30976470
pythonait:31006305
dsvsc021:30996838
dvdeprecation:31068756
dwnewjupytercf:31046870
2f103344:31071589
nativerepl2:31139839
pythonrstrctxt:31112756
nativeloc2:31192216
cf971741:31144450
iacca1:31171482
notype1:31157159
5fd0e150:31155592
dwcopilot:31170013
stablechunks:31184530

```

</details>

<!-- generated by issue reporter -->