Skip to content

Capturing stderr hangs Nushell #5156

Closed

Description

@kubouch

Describe the bug

Capturing stderr causes Nushell to hang indefinitely. It might be triggered by interleaving prints to stdout and stderr.

How to reproduce

  1. Have a C program that uses a random mix of printf("...\n") and fprintf(stderr, "...\n")
  2. do -i { random-program | complete } # hangs forever, cannot cancel with Ctrl-C
  3. run-external 'random-program' # same
  4. Outside of a git repo: do -i { git branch | complete } # this one works fine

Expected behavior

Collect both stdout and stderr without hanging. The do -i { git branch | complete } works fine so it might be the alternating stdout / stderr prints mess it up.

Screenshots

No response

Configuration

key value
version 0.60.1
branch main
short_commit e934062
commit_hash e934062
commit_date 2022-03-28 07:43:27 +00:00
build_os linux-x86_64
rust_version rustc 1.59.0 (9d1b2106e 2022-02-23)
rust_channel stable-x86_64-unknown-linux-gnu
cargo_version cargo 1.59.0 (49d8809dc 2022-02-10)
pkg_version 0.60.1
build_time 2022-03-28 11:02:19 +03:00
build_rust_channel release
features dataframe, default, trash, which, zip
installed_plugins gstat

Additional context

It might be that my C program is broken, I would need to make a MWE example.

Activity

  1. added
    category:bug
    status:investigate
    on Apr 11, 2022
  2. fennewald commented on Apr 14, 2022

    @fennewald
    Contributor

    Got a MWE:

    #include <stdio.h>
    
    static int line = 0;
    
    static void
    stderr_print (void)
    {
        fprintf(stderr, "Line %d: stderr\n", line++);
    }
    
    static void
    stdout_print (void)
    {
        printf("Line %d: stdout\n", line++);
    }
    
    int main ()
    {
        int i;
    
        for (i=0; i<100; ++i) {
            stdout_print();
            stderr_print();
        }
    }
    

    Interestingly, doesn't fail for small is

  3. fennewald commented on Apr 14, 2022

    @fennewald
    Contributor

    Even more interesting, I wrote a modified version that accepts a command line argument:

    #include <stdio.h>
    #include <stdlib.h>
    
    static int line = 0;
    
    static void
    stderr_print (void)
    {
        fprintf(stderr, "Line %d: stderr\n", line++);
    }
    
    static void
    stdout_print (void)
    {
        printf("Line %d: stdout\n", line++);
    }
    
    int main (int argc, char ** argv)
    {
        int i, max = atoi(argv[1]);
    
        for (i=0; i<max; ++i) {
            stdout_print();
            stderr_print();
        }
    }
    

    On my system, do -i { ./test 56 | complete } works, but do -i { ./test 57 | complete } hangs. Shell becomes unresponsive to ^C or any other signals, and I have to kill it externally.

    Version info:

    ╭────────────────────┬─────────────────────────────────────────────╮
    │ version            │ 0.61.0                                      │
    │ branch             │                                             │
    │ tag                │                                             │
    │ build_os           │ linux-x86_64                                │
    │ build_os           │ x86_64-unknown-linux-gnu                    │
    │ rust_version       │ rustc 1.62.0-nightly (90ca44752 2022-04-11) │
    │ rust_channel       │ nightly-x86_64-unknown-linux-gnu            │
    │ cargo_version      │ cargo 1.62.0-nightly (e2e2ddd 2022-04-05)   │
    │ pkg_version        │ 0.61.0                                      │
    │ build_time         │ 2022-04-13 16:37:17 +00:00                  │
    │ build_rust_channel │ release                                     │
    │ features           │ default, trash, which, zip                  │
    │ installed_plugins  │                                             │
    ╰────────────────────┴─────────────────────────────────────────────╯
    
  4. fennewald commented on Apr 14, 2022

    @fennewald
    Contributor

    This actually seems unrelated to the stdout switching, and instead seems to be related to the size of stderr output. Consider the following snippet:

    #include <stdio.h>
    #include <stdlib.h>
    
    int
    main (int argc, char ** argv)
    {
        int i, max = atoi(argv[1]);
    
        for (i=0; i<max; ++i) {
            fprintf(stderr, "Line %d\n", i);
        }
    }
    

    I wrote a python script that ranges the inputs from 1 to 100 and counts how many time out (I used a 100ms threshold).

    i=1 100 workd, 0 failed
    i=2 100 workd, 0 failed
    i=3 100 workd, 0 failed
    i=4 100 workd, 0 failed
    i=5 100 workd, 0 failed
    i=6 100 workd, 0 failed
    i=7 100 workd, 0 failed
    i=8 100 workd, 0 failed
    i=9 100 workd, 0 failed
    i=10 100 workd, 0 failed
    i=11 100 workd, 0 failed
    i=12 100 workd, 0 failed
    i=13 100 workd, 0 failed
    i=14 100 workd, 0 failed
    i=15 100 workd, 0 failed
    i=16 100 workd, 0 failed
    i=17 99 workd, 1 failed
    i=18 100 workd, 0 failed
    i=19 100 workd, 0 failed
    i=20 100 workd, 0 failed
    i=21 99 workd, 1 failed
    i=22 100 workd, 0 failed
    i=23 100 workd, 0 failed
    i=24 100 workd, 0 failed
    i=25 99 workd, 1 failed
    i=26 99 workd, 1 failed
    i=27 100 workd, 0 failed
    i=28 99 workd, 1 failed
    i=29 99 workd, 1 failed
    i=30 97 workd, 3 failed
    i=31 93 workd, 7 failed
    i=32 93 workd, 7 failed
    i=33 94 workd, 6 failed
    i=34 91 workd, 9 failed
    i=35 94 workd, 6 failed
    i=36 92 workd, 8 failed
    i=37 94 workd, 6 failed
    i=38 90 workd, 10 failed
    i=39 93 workd, 7 failed
    i=40 93 workd, 7 failed
    i=41 90 workd, 10 failed
    i=42 91 workd, 9 failed
    i=43 94 workd, 6 failed
    i=44 91 workd, 9 failed
    i=45 92 workd, 8 failed
    i=46 92 workd, 8 failed
    i=47 91 workd, 9 failed
    i=48 92 workd, 8 failed
    i=49 93 workd, 7 failed
    i=50 94 workd, 6 failed
    i=51 88 workd, 12 failed
    i=52 93 workd, 7 failed
    i=53 94 workd, 6 failed
    i=54 90 workd, 10 failed
    i=55 92 workd, 8 failed
    i=56 91 workd, 9 failed
    i=57 92 workd, 8 failed
    i=58 91 workd, 9 failed
    i=59 93 workd, 7 failed
    i=60 94 workd, 6 failed
    i=61 91 workd, 9 failed
    i=62 91 workd, 9 failed
    i=63 90 workd, 10 failed
    i=64 92 workd, 8 failed
    i=65 94 workd, 6 failed
    i=66 91 workd, 9 failed
    i=67 95 workd, 5 failed
    i=68 89 workd, 11 failed
    i=69 89 workd, 11 failed
    i=70 89 workd, 11 failed
    i=71 92 workd, 8 failed
    i=72 89 workd, 11 failed
    i=73 87 workd, 13 failed
    i=74 90 workd, 10 failed
    i=75 89 workd, 11 failed
    i=76 82 workd, 18 failed
    i=77 85 workd, 15 failed
    i=78 80 workd, 20 failed
    i=79 79 workd, 21 failed
    i=80 74 workd, 26 failed
    i=81 72 workd, 28 failed
    i=82 63 workd, 37 failed
    i=83 52 workd, 48 failed
    i=84 49 workd, 51 failed
    i=85 39 workd, 61 failed
    i=86 24 workd, 76 failed
    i=87 20 workd, 80 failed
    i=88 12 workd, 88 failed
    i=89 10 workd, 90 failed
    i=90 9 workd, 91 failed
    i=91 4 workd, 96 failed
    i=92 4 workd, 96 failed
    i=93 3 workd, 97 failed
    i=94 4 workd, 96 failed
    i=95 2 workd, 98 failed
    i=96 1 workd, 99 failed
    i=97 0 workd, 100 failed
    i=98 4 workd, 96 failed
    i=99 3 workd, 97 failed
    

    Ignoring my spelling mistakes, there's an interesting jump in failure rates from 75 to 89. Unsure what is causing this, but it appears non-deterministic.

  5. lukexor commented on Aug 17, 2022

    @lukexor

    Can confirm that this issue happens when there is no stderr output.

    do -i and do -c both hang when passed to complete if there is no stderr output (only stdout). It's like nushell is waiting for some stderr before completing instead of waiting on the external to return with a status code.

    This is particularly problematic when a given command may or may not fail and produce stderr or not making it hard to predict whether this will hang the shell. No known work-arounds as far as I can tell short of dropping into another shell inside the do block and putting at least something into the stderr stream before returning.

  6. added this to the v0.70.0 milestone on Oct 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    category:bugstatus:investigate

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions