Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

child_process.spawn leaves spawned process hanging #7951

Closed
dignifiedquire opened this issue Aug 2, 2016 · 7 comments
Closed

child_process.spawn leaves spawned process hanging #7951

dignifiedquire opened this issue Aug 2, 2016 · 7 comments
Labels
child_process Issues and PRs related to the child_process subsystem.

Comments

@dignifiedquire
Copy link

dignifiedquire commented Aug 2, 2016

  • Version: 6.3.0
  • Platform: Darwin dignified-station-3.local 15.6.0 Darwin Kernel Version 15.6.0: Thu Jun 23 18:25:34 PDT 2016; root:xnu-3248.60.10~1/RELEASE_X86_64 x86_64
  • Subsystem: child_process

Given the following code

'use strict'

const spawn = require('child_process').spawn

const cmd = 'tail'
const args = ['-f', '/dev/null'/*, '&'*/]
const p = spawn(cmd, args)

console.log('starting')
setTimeout(() => {
  console.log('exiting')
  process.exit()
}, 2 * 1000)

I would expect the spawned process to be terminated when the execution of it terminates, but this does not seem the case. Checking after this code is executed I find

$ ps auxw |grep tail
dignifiedquire  21100   0.0  0.0  2434832    664 s004  S     9:44PM   0:00.00 tail -f /dev/null &
@cjihrig
Copy link
Contributor

cjihrig commented Aug 2, 2016

Can you try removing the '&' arg?

@dignifiedquire
Copy link
Author

dignifiedquire commented Aug 2, 2016

Can you try removing the '&' arg?

Yes, same behaviour, updated the script above.

@dignifiedquire
Copy link
Author

I should probably note that this is not specific to tail -f, it was just the most minimal example I could find to reproduce this without introducing external binaries.

@bnoordhuis
Copy link
Member

bnoordhuis commented Aug 2, 2016

I would expect the spawned process to be terminated when the execution of it terminates

'it' refers to the node process, not the child process? That's the expected behavior, it's how UNIX generally works.

EDIT: To be clear, it's expected that the child stays alive when the parent terminates.

@dignifiedquire
Copy link
Author

'it' refers to the node process, not the child process?

Yes.

That's the expected behavior, it's how UNIX generally works.

Thanks for the clarification. While I understand that UNIX works this way, I feel it would be very beneficial for API to expose a way of saying kill this process whenever I die to ensure this cleanup happens.
To do this in userland as far as what I have found currently is that I need to listen to multiple events on process, .i.e. exit and uncaughtException and SIGINT to ensure that this cleanup happens, which seems to be prone to errors.

@addaleax
Copy link
Member

addaleax commented Aug 2, 2016

@dignifiedquire Does using https://www.npmjs.com/package/signal-exit present a solution for that problem to you?

@dignifiedquire
Copy link
Author

@addaleax thank you 😊 I did not know about that module, this certainly helps with ensuring on listening to the right signal.

Thank you everyone for the super quick turn around and help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
child_process Issues and PRs related to the child_process subsystem.
Projects
None yet
Development

No branches or pull requests

5 participants