denoland / deno_task_shell

Cross-platform shell for deno task.
https://crates.io/crates/deno_task_shell
MIT License
104 stars 16 forks source link

Deno processes spawned using `deno task` do not exit when the parent process exits #33

Open dhardtke opened 2 years ago

dhardtke commented 2 years ago

Consider this example:

deno.jsonc:

{
  "tasks": {
    "script": "deno run script.ts",
  }
}

script.ts:

console.log(Deno.pid);
while (true);

reproduction.ts:

const BROKEN = ["deno", "task", "script"];
const WORKING = ["deno", "run", "script.ts"];

const parent = Deno.run({ cmd: BROKEN });
console.log(`Spawned ${parent.pid}`);
await sleep();

parent.close();

function sleep(): Promise<void> { return new Promise(resolve => { setTimeout(() => { resolve(); }, 1000); }); }

Here's the behavior using the BROKEN cmd in reproduction.ts:

> deno run --allow-all reproduction.ts 
Check file:///reproduction.ts
Spawned 15852
Warning deno task is unstable and may drastically change in the future
Task script deno run script.ts
2036

> taskkill /F /IM deno.exe             
SUCCESS: The process "deno.exe" with PID 15040 has been terminated.
SUCCESS: The process "deno.exe" with PID 2036 has been terminated.

Here's the same execution but this time using the WORKING cmd:

> deno run --allow-all reproduction.ts 
Check file:///reproduction.ts
Spawned 17712
17712

> taskkill /F /IM deno.exe             
ERROR: The process "deno.exe" not found.
deno 1.20.5 (release, x86_64-pc-windows-msvc)
v8 10.0.139.6
typescript 4.6.2
dhardtke commented 2 years ago

Maybe this issue belongs in https://github.com/denoland/deno_task_shell?

DrSensor commented 1 year ago

Another case when spawned process not closed when deno task exit

{
  "importMap": "./import_map.json",
  "tasks": {
    "lume": "echo \"import 'lume/task.ts'\" | deno run --unstable --allow-read --allow-run --lock=lock.json -",
    "serve": "deno task lume -s",
    "test": "deno task serve --quiet & PUPPETEER_PRODUCT=chrome deno test --no-check --parallel --shuffle=7 --unstable --allow-env --allow-write --allow-read --allow-net --allow-run ; exit"
  }
}

This is my deno.json config for running e2e test. In this example when I run deno task test, the server is still running although the deno test is done. (notice there is ; exit at the end of line)

Hexagon commented 1 year ago

Following! Whats the status on this?

StephenHaney commented 1 month ago

I think I'm also running into this – It's possible this is why Deno.serve sometimes and says the address is already in use when in --watch mode (https://github.com/denoland/deno/issues/16699)

For instance, right now my Deno.serve is still running even though I've exited out of the terminal that ran the deno task to start it.