Skip to content

avoid freeze for table print - #6688

Merged
fdncred merged 4 commits into
nushell:mainfrom
WindSoilder:avoid_freeze_for_table_print
Oct 10, 2022
Merged

fdncred merged 4 commits into
nushell:mainfrom
WindSoilder:avoid_freeze_for_table_print

Conversation

@WindSoilder

@WindSoilder WindSoilder commented Oct 9, 2022 •

Copy link
Copy Markdown
Contributor

Description

Fixes: #6594 #6679

As @nibon7 point out, nushell is freeze because blocking channel sender is full, and exit code doesn't send through channel yet.

And while printing out pipeline, it tried to catch exit_code first, so everything hangged on.

This pr is going to solve this by removing exit_code consume logic from eval_block function, and so make pipeline_data.print method always consume stdout, then exit_code

For stderr relative example on #5625 (comment), I'm trying to apply the same consuming logic, but it doesn't work, I need to think how to handle it properly.

Tests

Make sure you've done the following:

  • Add tests that cover your changes, either in the command examples, the crate/tests folder, or in the /tests folder.
  • Try to think about corner cases and various ways how your changes could break. Cover them with tests.
  • If adding tests is not possible, please document in the PR body a minimal example with steps on how to reproduce so one can verify your change works.

Make sure you've run and fixed any issues with these commands:

  • cargo fmt --all -- --check to check standard code formatting (cargo fmt --all applies these changes)
  • cargo clippy --workspace --features=extra -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo test --workspace --features=extra to check that all the tests pass

@fdncred

fdncred commented Oct 9, 2022

Copy link
Copy Markdown
Contributor

Will this still capture all exit codes like it did before? I like your idea but I don't want to misreport exit codes either. I haven't test yet because I'm on my phone.

@WindSoilder

Copy link
Copy Markdown
Contributor Author

Will this still capture all exit codes like it did before?

I think so :-), it just move the logic from eval_block:

            if let PipelineData::ExternalStream { exit_code, .. } = &mut pipeline_data {
                if let Some(exit_code) = exit_code.take().and_then(|it| it.last()) {
                    stack.add_env_var("LAST_EXIT_CODE".to_string(), exit_code);
                } else {
                    set_last_exit_code(stack, 0);
                }
            } else {
                set_last_exit_code(stack, 0);
            }

to PipelineData.print method:

           // Make sure everything has finished
            if let Some(exit_code) = exit_code {
                let _: Vec<_> = exit_code.into_iter().collect();
                let mut exit_codes: Vec<_> = exit_code.into_iter().collect();
                if let Some(Value::Int { val, .. }) = exit_codes.pop() {
                    return Ok(val);
                }
            }

@fdncred

fdncred commented Oct 10, 2022

Copy link
Copy Markdown
Contributor

How can we write tests for this?

@WindSoilder

Copy link
Copy Markdown
Contributor Author

#[test]
#[ignore = "For now we have no way to check LAST_EXIT_CODE in tests, ignore it for now"]
fn failed_with_proper_exit_code() {
Playground::setup("external failed", |dirs, _sandbox| {
let actual = nu!(cwd: dirs.test(), r#"
nu -c "cargo build; print $env.LAST_EXIT_CODE"
"#);
// cargo for non rust project's exit code is 101.
assert_eq!(actual.out, "101")
})
}

Actually we have one test cover about this, but because of semicolon rule, the test is ignored for now. I'll try to figure out if we can make it works again

@WindSoilder

WindSoilder commented Oct 10, 2022 •

Copy link
Copy Markdown
Contributor Author

Ok, I've updated relative test to make sure that it doesn't break exit code handling; And add one more test case to cover the change.

@fdncred

fdncred commented Oct 10, 2022

Copy link
Copy Markdown
Contributor

thanks! let's see if this makes things a little better.

@fdncred
fdncred merged commit 1998bce into nushell:main Oct 10, 2022
@WindSoilder
WindSoilder deleted the avoid_freeze_for_table_print branch October 10, 2022 13:38
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.

Shell freezes after cat .zshrc | table

2 participants