-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(runtime): "Error: EOF: end of file, read" on Windows
NodeJS makes it unsafe to perform syncrhonous (aka blocking) operations on file descriptors 0 through 2 (STDIN, STDOUT, STDERR), as those are set to `O_NONBLOCK` by `libuv`. This is particularly problematic on Windows where there is no easy way to re-open those in blocking mode as can be done on most UNIX platforms today using `/dev/std{in,out,err}` pseudo-files. Consequently, we must handle some quirks in cases where blocking attempts result in conditions typical of non-blocking access: `EAGAIN` errors must be caught and retried, and `EOF` errors must be "ignored". This PR adds the necessary code to correctly handle the `EOF` error, and adds a 50ms sleep before re-trying a `EAGAIN` error that is achieved thanks to a neat trick suggested by the [`sleep` npm package][sleep]: using `Atomics.wait` enables syncrhonous sleep to be done in `node` without having to ship a native module. Additionally, this PRs re-opens `STDIN`, `STDOUT` and `STDERR` when the `/dev/std{in,out,err}` files are present, reducing the incidence of this problem on most UNIX platfoms. [sleep]: https://www.npmjs.com/package/sleep
- Loading branch information
1 parent
32774ca
commit 221edff
Showing
4 changed files
with
95 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// We need a shared buffer array for the purpose of this exercise. | ||
const array = new Int32Array(new SharedArrayBuffer(4)); | ||
|
||
/** | ||
* Sleeps for a set amount of time, syncrhonously. | ||
* | ||
* @param time the amount of time to wait, in milliseconds. | ||
*/ | ||
export function sleep(time: number): void { | ||
// `Atomics.wait` will block for `time` milliseconds if `array[0]` still has | ||
// value `0` (which it will, since we just initialized it to that). The return | ||
// value is irrelevant for our business here. | ||
Atomics.wait(array, 0, 0, time); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters