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

fix(shim-deno/std): Fix std not to use FsFile #98

Merged
merged 5 commits into from
Mar 11, 2022

Conversation

hyp3rflow
Copy link
Contributor

@hyp3rflow hyp3rflow commented Mar 7, 2022

If node's "process" module is imported, accessing stdin by fs.read throws EAGAIN error: resource temporarily unavailable.
So I re-implemented std interface using process.stdin, process.stdout, process.stderr.

resolves #97

@hyp3rflow hyp3rflow marked this pull request as ready for review March 7, 2022 10:31
Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @hyp3rflow! That's annoying that occurs in Node and I'm able to reproduce it. I think we need to do a few more changes to get this merged though.

}
});
return deferred.then((result) => {
process.stdin.pause();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone does multiple read calls at once before the first is complete, would this pause the second call?

Copy link
Contributor Author

@hyp3rflow hyp3rflow Mar 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that code, yes. So I changed the logic to handle multiple calls correctly.

p.fill(0);
process.stdin.resume();
process.stdin.once("readable", () => {
const data = process.stdin.read(p.length) ?? process.stdin.read();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could falling back to read() cause more data to be read than what's in the buffer and then for p.set(data) to fail below with a RangeError?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think no.

The optional size argument specifies a specific number of bytes to read. If size bytes are not available to be read, null will be returned unless the stream has ended, in which case, all of the data remaining in the internal buffer will be returned.
If the size argument is not specified, all of the data contained in the internal buffer will be returned.

According to the documentation, if buffer has data more than p.length bytes, it doesn't fall back to stdin.read().
but buffer has data less than p.length bytes or is empty, stdin.read() will return all of the data contained in the buffer which is always less than p.length bytes.

@hyp3rflow hyp3rflow requested a review from dsherret March 10, 2022 07:06
@hyp3rflow
Copy link
Contributor Author

Thanks for the kind review, @dsherret!
I noticed that there are a few errors in multiple call cases, So I changed to delay stream logic to fix wrong behavior.
748e399 (#98)

Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @hyp3rflow!

});
return (prev = curr);
} as T;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

process.stdin.on("error", onerror);
process.stdin.once("readable", () => {
process.stdin.off("error", onerror);
const data = process.stdin.read(p.length) ?? process.stdin.read();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think actually it's possible that between the process.stdin.read(p.length) and process.stdin.read() calls that stdin could receive more bytes than are in the buffer. Let's resolve it in a separate PR. I'll open an issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #99

@dsherret dsherret merged commit bd4a83e into denoland:main Mar 11, 2022
@hyp3rflow hyp3rflow deleted the fix/stdio branch March 14, 2022 01:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

read from stdin throws EAGAIN: resource temporarily unavailable
2 participants