-
Notifications
You must be signed in to change notification settings - Fork 253
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
Feature request: callback to know when the sub-process is started, of why it failed to exec #265
Comments
@simark to track a process I normally launch |
Hmm not really. It's not related to the process exit. It is that we want to be able to know whether the process was started successfully (meaning, the exec system call succeeded) or not. In practice, in our UI, we want to show either "Process X started" or "Couldn't start process X: no such file or directory", for example. It could be a long running process. Checking the exit event doesn't help. First, we are not notified when the process has been successfully started (to show the success message). Then, on failure (if the program does not exist), we just receive an exit event, which we can't differentiate from a normal process exit. For example, with node's
with node-pty, we just get this exit event:
|
@simark I see, definitely seems useful. Can you get approval to sign the CLA to contribute this specific change? |
We have already tried to get the approval for another Microsoft project (language-server stuff). IIUC, the problem our legal department has with the Microsoft CLA is that it applies to all open source Microsoft projects, which would be too wide (again, IIUC, I am not a lawyer). I doubt it will be possible to get an agreement signed just for one change... If the situation changes, I will definitely update you. |
@simark You trigger the success event on |
My patch does something to avoid this (exactly the same technique as libuv). The parent synchronously waits for the child to exec, using a pipe: theia-ide@f58e6ee#diff-32a8618c58cb849e9b5535ba19ffacc5R359 When the native function in the parent returns, the exec has either already succeeded or failed. |
Here's where it's done in libuv: https://github.com/nodejs/node/blob/3fe9267592cfef01b50f4f94eaa50b2dc495eb1a/deps/uv/src/unix/process.c#L462 |
Woops have overlooked this, yeah the CLOEXEC trick works for this. |
Could we document this behavior on I just spent hours banging my head against a wall, and it turned out the exec had silently failed because the file path was wrong 😄 I suppose I expected an exception or some sort of feedback of the failure. Maybe we can save the next person some debugging pain with a small note? |
Even better, it should be fixed. @marcdumais-work, is the situation still the same at Ericsson (still not allowed to contribute to this project)? |
Yeah, the fact that a failed exec* gets silently swallowed by the module, is less than ideal. @simark You already proposed a working patch for posix systems, would be nice to see this being applied. But also note, that the module is likely to see a major overhaul soon due to #405. Not sure about the windows side here - maybe @Tyriar has some insights, whether it already promotes a failed process binary lookup? |
Not sure what you mean by failed process binary lookup, but afaik on Windows it acts the same. VS Code uses a workaround for this by validating cwd and executable before invoking node-pty https://github.com/microsoft/vscode/blob/cf73ba7aac021ee448a258f597064dcd0f023b87/src/vs/workbench/contrib/terminal/node/terminalProcess.ts#L74-L110 It leads to some obscure errors though which could be better if we had this. |
Yes, it's still the case, as far as I know, that we are not allowed to sign the Microsoft CLA. However we kept our Looking quickly I think these are the pieces of the patch and follow-ups: |
Sorry for sloppy wording - I mean whether it gives a meaningful error message, if the process binary lookup failed. (you already answered it) Imho
I think both should be exposed with proper error messages, so I am also not sure, if the pty fd / conpty handle is correctly teared down atm, if 2. fails (they are two different steps on OS levels prolly wasting resources if they are not teared down). |
Summary: I think we do throw when conpty fails to create processes now, but may not in other cases. To action this we should review what happens when processes fail to launch and ensure they throw |
Environment details
Issue description
When using the
spawn
function, I didn't find any way to know when the process has been successfully started, or if the exec failed for some reason. My concrete use case is that I need to start a process on a pty. On success, I need to report to the caller that the process has been started*. On failure (e.g. if the executable doesn't exist), I should return the error code. I haven't found a way to do this using the current API.I have implemented something in our fork of node-pty here, but unfortunately my employer doesn't want me to sign the Microsoft CLA, and thus I can't contribute it. But I thought I would share the idea, to get some feedback. And if somebody wants to implement something similar in the upstream repo, I guess you can't copy the code but you can get some good inspiration from it.
In my patch, I add an
exec
event to theIPty
/ITerminal
interface, with an optionalerror
parameter. If theexec
goes well, theexec
callback is invoked without parameter. If there is an error, it is invoked with an errno string (e.g.ENOENT
).On Linux/macOS, since we use fork + exec to spawn a new process, it's a bit difficult to get some feedback on the exec (since it happens in a new process). I looked at how libuv does (what node's
child_process.spawn
relies on) and did something similar, which is to use a pipe between the child process and our process to report success or failure.For Windows, it looks like we just need to catch an exception on failure, here's what I did.
Any feedback is appreciated, and if anybody is willing to work on an upstream version of this, I would be happy to help.
The text was updated successfully, but these errors were encountered: