Skip to content

Proposal: child_process: Send socket but keep it open in parent #4271

Closed
@adrienverge

Description

@adrienverge

The current behavior when sending a net.Socket object to a child process using child_process.ChildProcess.send() is to close the socket in the parent. This way, only the socket receiver can read and write to it.

Problem: in some use-cases, a user may want to have BOTH parent and child to write to the TCP socket. For instance, I'm developing an app where a process writes data to the socket, whereas another one writes metadata. Sharing the socket in C, not in Node.js (currently).

The enhancement I propose is to add an optional keepOpen parameter to specify whether the parent should keep the socket open or not (it would default to false to keep existing behavior).

const child = require('child_process').fork('child.js');
const server = require('net').createServer();

server.listen(1234).on('connection', function (socket) {
    child.send('socket', socket, { keepOpen: true });

    // Currently this crashes with: "Error: This socket is closed."
    socket.write('-- hello from parent --');
});

What I'm asking here is whether this is an acceptable enhancement or not. If it is, I'll start working on a clean pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions