You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When running processes with e.g. child_process.exec it is possible to write into the stdin stream via write.
var child_process = require('child_process')
var proc = child_process.exec('grep')
proc.stdin.write('abc')
I noticed that if the program (here grep) stops "fast enough" the write will throw an exception:
events.js:160
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at exports._errnoException (util.js:1026:11)
at WriteWrap.afterWrite (net.js:799:14)
This exception is not thrown by stdin.write directly. It seems that it is thrown in another event-loop iteration. It is not possible to catch this exception.
Will not work. I tried to check the property stdin.destroyed but it seems that the value of destroyed is updated in the next event loop iteration. So it is not possible to prevent the problem with
if (!proc.stdin.destroyed) {
proc.stdin.write('abc')
}
The only solution I found is using setTimeout to make sure that the event loop runs at least once before checking the .destroyed value, e.g.:
setTimeout(() => {
if (!proc.stdin.destroyed) {
proc.stdin.write('abc')
}
}, 10)
I looked into the documentation of exec and spawn and I found that it is possible to explicitly specify the input stream for the spawned process via options.stdio and with this I could probably pipe the input into the stream before starting the process.
But it seems to me, that this problem can arise whenever write is called at the wrong time. Is this behavior intended and if so, how could I prevent the exception or at least catch it?
If you want to test it you can simply use the following C program that fails very fast
int main () { return 1; }
I had the problem with grep initially on a new fast PC. With the above C program the problem was reproducible on all machines.
The text was updated successfully, but these errors were encountered:
When running processes with e.g.
child_process.exec
it is possible to write into the stdin stream viawrite
.I noticed that if the program (here
grep
) stops "fast enough" thewrite
will throw an exception:This exception is not thrown by stdin.write directly. It seems that it is thrown in another event-loop iteration. It is not possible to catch this exception.
Will not work. I tried to check the property
stdin.destroyed
but it seems that the value ofdestroyed
is updated in the next event loop iteration. So it is not possible to prevent the problem withThe only solution I found is using
setTimeout
to make sure that the event loop runs at least once before checking the.destroyed
value, e.g.:I looked into the documentation of
exec
andspawn
and I found that it is possible to explicitly specify the input stream for the spawned process viaoptions.stdio
and with this I could probably pipe the input into the stream before starting the process.But it seems to me, that this problem can arise whenever write is called at the wrong time. Is this behavior intended and if so, how could I prevent the exception or at least catch it?
If you want to test it you can simply use the following C program that fails very fast
I had the problem with grep initially on a new fast PC. With the above C program the problem was reproducible on all machines.
The text was updated successfully, but these errors were encountered: