Skip to content

The process never ends when piping the stdin to a child process #2276

@IonicaBizau

Description

@IonicaBizau

Initially I asked this on StackOverflow, but looks like a bug (nodejs/node-v0.x-archive#9190). Post follows:


I'm using spawn to create a child process and pipe data:

child process  |  parent process (main)
---------------------------------------
stdout      ----->       process.stdout
stderr      ----->       process.stderr
stdin       <-----        process.stdin

The problem is that when piping the process.stdin to the child process stdin, the main process is not ended when the child process is finished.

The code looks like this (not a really good example because ps does not use stdin data, I guess):

var Spawn = require("child_process").spawn;

var ps = Spawn("ps");
process.stdin.pipe(ps.stdin);
ps.stdout.pipe(process.stdout);
ps.stderr.pipe(process.stderr);

If I remove the process.stdin.pipe(ps.stdin) line, the main process is ended, but the stdin data is not piped anymore.

Why isn't the main process ended when the ps child process is ended? How can I solve this problem?

An ugly solution would be:

ps.on("close", process.exit.bind(process));

I don't like this, because I don't really want to force the main process to be closed, but I want to be closed naturally (e.g. having setTimeout(function(){}, 1000) you wait 1000ms and then the process ends).


I tried to ps.stdin.close() in ps.on("close", cb). It didn't work... 😢

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.processIssues and PRs related to the process subsystem.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions