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

Exit on first EOF from stdin #492

Merged
merged 1 commit into from
Feb 7, 2019
Merged

Conversation

reidwagner
Copy link
Contributor

Addresses #477.

@mziter
Copy link

mziter commented Feb 3, 2019

per docs, process::exit:

Note that because this function never returns, and that it terminates the process, no destructors on the current stack or any other thread's stack will be run.

Not sure if this is an issue or not?

@reidwagner
Copy link
Contributor Author

reidwagner commented Feb 4, 2019

Thanks for bringing this up! I was hoping process::exit was sufficient for this simple case, but I can see the argument for a stricter exit sequence as well.

This is also how a BrokenPipe error is handled, so there is a precedent.

I'd like to know the maintainer's thoughts as well :).

@sharkdp
Copy link
Owner

sharkdp commented Feb 4, 2019

Thank you for working on this.

Yes, returning an Err(..) with a custom error would definitely be the preferred option here, instead of calling process::exit directly.

Using process::exit is probably even wrong here if there are multiple input files...

Speaking of wrong behavior: it would be great if we could add integration/regression tests for this (see tests/integration_tests.rs)

@reidwagner
Copy link
Contributor Author

Okay, thanks for the input. I'll look into implementing this and include integration tests as well.

@willcassella
Copy link

I agree with sharkdp that Err is preferable to exit. But an interesting note is that when using exit you don't actually need a Result, since exit returns never which is implicitly convertible to anything (since the conversion never actually happens since exit... exits).

@reidwagner
Copy link
Contributor Author

@willcassella I added the Result here to handle read_until()?. Do you think I should have left it as is, discarding the error?

@sharkdp
Copy link
Owner

sharkdp commented Feb 5, 2019

@willcassella I added the Result here to handle read_until()?. Do you think I should have left it as is, discarding the error?

I think it should be fine to handle the error with ? here - I originally thought it would cause problems with broken pipes (when the input file only has one line without line ending), but it returns Some(..) in this case.

@reidwagner
Copy link
Contributor Author

Okay, I created a new ErrorKind variant and essentially just ignore it - would any extra actions need to be taken? The behavior is now correct with multiple input files, e.g. bat foo.txt - foo.txt.

I haven't figured out how to emulate a CTRL-D for an integration test yet.

@@ -45,7 +45,10 @@ impl<'b> Controller<'b> {
match input_file.get_reader(&stdin) {
Err(error) => {
handle_error(&error);
no_errors = false;
match error {
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of matching on ErrorKind::ImmediateEOF here and in handle_error, could we also move the handle_error call into the _ => branch?

match error {
    Error(ErrorKind::ImmediateEOF, _) => (),
    _ => {
        handle_error(&error);
        no_errors = false
    },
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure - that did seem redundant. I backed it up even further and just matched the Result against the Err(Error(ErrorKind::ImmediateEOF, _)), if that works for you.

@sharkdp
Copy link
Owner

sharkdp commented Feb 6, 2019

I haven't figured out how to emulate a CTRL-D for an integration test yet.

You are right, that's probably very tricky. I'd like to have some tests where we check that bat empty test == test, bat test empty == test, bat empty empty == empty, bat test empty test == test + test etc., but that's not really part of this issue (they would have caught the problem with process::exit, though).

@reidwagner
Copy link
Contributor Author

Makes sense. I can write that up.

@sharkdp
Copy link
Owner

sharkdp commented Feb 7, 2019

Thank you for the update!

@willcassella
Copy link

I added the Result here to handle read_until()?

Sorry I didn't notice that, I got distracted by how cool never is :)

Thanks for fixing this!

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.

4 participants