KyleJune
KyleJune2y ago

Killing subprocess not working

Anyone know what I'm doing wrong when trying to kill a process? I have a process start another process like this:
runProcess = Deno.run({
cmd: ["deno", "task", "run-dev"],
});
runProcess = Deno.run({
cmd: ["deno", "task", "run-dev"],
});
My child process uses deno task and the run-dev task starts an oak server. Then if a file changes, I kill it with this.
runProcess.kill();
runProcess.close();
runProcess.kill();
runProcess.close();
But I found that the server oak server keeps listening after I sent those. I tried awaiting runProcess.status() afterwards and it does resolve to { success: false, code: 143, signal: 15 } indicating it was killed, but the server is able to keep handling requests and if I try starting a new run-dev task, it will fail with an error saying the address is already in use. So it would appear the oak server isn't getting killed correctly. I even tried adding a 30 second delay between killing and restarting but it still fails to kill it. The following output shows that I killed the process, but the oak server still was able to handle requests afterwards.
Restarting app
killing process
{ status: { success: false, code: 143, signal: 15 } }
{ status: 200, method: "GET", href: "http://localhost:9000/", responseTime: 4 }
{ status: 200, method: "GET", href: "http://localhost:9000/", responseTime: 2 }
{ status: 200, method: "GET", href: "http://localhost:9000/", responseTime: 3 }
Task run-dev export APP_ENV=development && deno run -A ./main.ts
error: Uncaught AddrInUse: Address already in use (os error 98)
: Deno.listen(this.#options)) as Listener;
Restarting app
killing process
{ status: { success: false, code: 143, signal: 15 } }
{ status: 200, method: "GET", href: "http://localhost:9000/", responseTime: 4 }
{ status: 200, method: "GET", href: "http://localhost:9000/", responseTime: 2 }
{ status: 200, method: "GET", href: "http://localhost:9000/", responseTime: 3 }
Task run-dev export APP_ENV=development && deno run -A ./main.ts
error: Uncaught AddrInUse: Address already in use (os error 98)
: Deno.listen(this.#options)) as Listener;
1 Reply
KyleJune
KyleJuneOP2y ago
I'm using Ubuntu 20.04.5 LTS with deno 1.29.2 I also tried setting stdin to "piped" but it made no difference. I tried using "SIGKILL" too but that doesn't work either. If I use ctrl + c to kill the parent process in my terminal, it does successfully kill both my script and the child process. I'm trying to implement a file watcher that will restart my oak server on file changes. I would just use the --watch flag but I'm trying to have more control over when restarts occur so that it waits until my build script finishes before restarting. Issue persists after restarting my machine too. Could the issue be related to me using deno task instead of just starting the server directly? I'll test that theory tomorrow but it would be disappointing if I can't use my configured tasks. I feel like it's probably that but don't understand why. Had a similar issue the other day where I tried setting stdout to null. The output of the task command was suppressed correctly but the script started by the task command still had output go to stdout. Oh i found an old issue for it from April. https://github.com/denoland/deno_task_shell/issues/33