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

Parent-child pipes not relayed #613

Open
5 of 6 tasks
antoinep92 opened this issue Jul 15, 2024 · 2 comments
Open
5 of 6 tasks

Parent-child pipes not relayed #613

antoinep92 opened this issue Jul 15, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@antoinep92
Copy link

Acknowledgements

  • I read the documentation and searched existing issues to avoid duplicates
  • I understand this is a bug tracker and anything other than a proven bug will be closed
  • I understand this is a free project and relies on community contributions
  • I read and understood the Contribution guide

Minimal reproduction URL

https://stackblitz.com/edit/node-qzmjn4?file=main.js

Problem & expected behavior (under 200 words)

I'm not aware of how tsx works internally, but apparently it breaks at least some forms of inter-process communication. I setup a child process to have a stream (fd 3) to talk with it's parent, but that becomes unusable when using tsx. Spent quite a bit of time to track the issue to tsx actually ^_^

I don't know if this is easily fixable, but at least it should be documented somewhere.

Bugs are expected to be fixed by those affected by it

  • I'm interested in working on this issue

Compensating engineering work will speed up resolution and support the project

  • I'm willing to offer $10 for financial support
@antoinep92 antoinep92 added bug Something isn't working pending triage labels Jul 15, 2024
@privatenumber privatenumber changed the title Swawn + communication through additional FD broken by tsx Parent-child pipes not relayed Jul 16, 2024
@privatenumber
Copy link
Owner

This happens because tsx also spawns a node process and isn't relaying the pipes it receives from the parent process:

tsx/src/run.ts

Lines 15 to 19 in 2688300

const stdio: StdioOptions = [
'inherit', // stdin
'inherit', // stdout
'inherit', // stderr
];

Do you know if there's a good way to detect all the pipes attached to the current process by the parent? Not a lot of info in the docs: https://nodejs.org/api/child_process.html#optionsstdio

As far as I understand, the only way is to do something like:

import fs from 'fs';

const stdio = [];

for (let fd = 0; fd < 100; fd++) { // Arbitrary upper limit for file descriptors
  try {
    fs.fstatSync(fd); // If this doesn't throw, fd is valid
    stdio.push('inherit');
  } catch (err) {
    if (err.code !== 'EBADF') { // EBADF means fd is not valid
      throw err;
    }
  }
}

But I prefer we're not doing an arbitrary number of stat and try-catches on startup.

@antoinep92
Copy link
Author

Hi, thanks for the quick feedback !

There are platform specific ways, e.g. Linux (or unix like MacOS) have /proc/fd which lists open files and I believe it would be quite efficient to scan that on startup. There must be a windows API for that also, but not necessarily available from node. But at this point it's probably more trouble than it's worth. I don't see a standard, cross-platform way of doing it, apart from what you propose, which does not seem very nice.

Also, just adding a bunch of "inherit" to stdio options just in case is not a good option either because it changes the error code when trying to open a file descriptor which could also break some edge cases.

I guess one option would be to have a way to configure this through command line parameters or otherwise, but I understand one of the point of this project is to "just work" with no configuration, so I don't know if that fits.

Also if you look at the broader picture, there are many IPC mechanisms ; detecting and forwarding them all seems unrealistic. So maybe the easier resolution is to accept that this use-case is not handled by tsx, but it should be documented.

In the meantime I'm gonna try my luck with ts-imp (ts-node also breaks on our code, but for different reasons 😭 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants