Node js spawn function

If you execute the code below with Node.js installed from snap and apply what I said in the comment, it doesn’t work. But it works if you install Node from other sources. It’s probably because snap is sandboxing the process, and somehow It can’t do IPC with the child process, maybe they can’t access the same file descriptors! I’m not sure about the reason.

As this is not the expected behavior I thought something else needs to be fixed.

const spawn = require('child_process').spawn;

if (process.argv[2] === 'child') {
  let net = require('net');
  let pipe = new net.Socket({ fd: 3 });
  pipe.write('killme');
} else {
  let child = spawn(process.execPath, [__filename, 'child'], {
    // changing process.execPath to 'node' breaks the ipc
    stdio: [null, null, null, 'pipe'],
  });
  child.stdio[3].on('data', (data) => {
    if (data.toString() === 'killme') {
      console.log('RIP');
      child.kill();
    }
  });
}

I think you’re being hit by: https://bugs.launchpad.net/ubuntu/+source/snapd/+bug/1849753 A workaround for is described in https://bugs.launchpad.net/ubuntu/+source/snapd/+bug/1849753/comments/22

A workaround has been sketched out here: https://github.com/snapcore/snapd/pull/10029 But there hasn’t been much progress so far.