Skip to content

Redirections and pipes involving blocks (like for-loops) buffer output #1396

Open

Description

@CGamesPlay

When I run this script in fish 2.1.0:

for i in (seq 1 5); sleep 1; echo $i; end | cat

I should except to see a slow count to 5, one line per second. Instead, I see no output, and then, five seconds later, all 5 lines appear. This appears to be the same issue as #1302.

This appears to happen regardless of the amount of output:

for i in (seq 1 5); sleep 1; cat /usr/share/dict/words; end | cat

Activity

  1. ridiculousfish commented on Apr 3, 2014

    @ridiculousfish
    Member

    Yes, redirections and pipes involving blocks are run serially, not in parallel.

  2. CGamesPlay commented on Apr 3, 2014

    @CGamesPlay
    ContributorAuthor

    Thanks for getting back to me so quickly. Is there a way to work around this? I'm running a series of long-running scripts and tee'ing the output to a log file. I'd like to monitor the log as it happens, but the shell just stores up all of the output until the very end.

  3. ridiculousfish commented on Apr 6, 2014

    @ridiculousfish
    Member

    It's a longstanding and annoying issue.

  4. sugar700 commented on Apr 7, 2014

    @sugar700
    Contributor

    There is a workaround for it, but it's really ugly, and probably not a good idea (besides, it ignores scopes, unless you are going to use universal variables removed after leaving the scope).

    fish -c 'for i in (seq 1 5)
        sleep 1
        echo $i
    end' | cat

    It would be really nice to fix it, but nobody made a fix yet.

  5. tannhuber commented on Sep 4, 2014

    @tannhuber
    Contributor

    It's annoying, indeed. Does anybody have an idea how to fix it?

  6. ridiculousfish commented on Sep 4, 2014

    @ridiculousfish
    Member

    The long-term plan is to support full multithreaded execution. See #563

  7. tannhuber commented on Sep 4, 2014

    @tannhuber
    Contributor

    Would be great. Is this also the reason, why

    function fish_prompt
            echo -n "prompt: "
            sleep 3 # in reality there's a time consuming command after printing the prompt
    end
    

    shows the prompt with 3 seconds delay?

  8. lilyball commented on Sep 4, 2014

    @lilyball
    Contributor

    @tannhuber No, that's a different issue. That's also correct behavior. Which is to say, the prompt isn't supposed to paint until all the data is available. In any case, you can think of fish_prompt as being executed in a manner similar to

    set -l left_prompt $default_prompt
    if functions -q fish_prompt
        set left_prompt (fish_prompt)
    end
    set -l right_prompt ''
    if functions -q fish_right_prompt
        set right_prompt (fish_right_prompt)
    end
    internal_read_line --left-prompt $left_prompt --right-prompt $right_prompt

    In this pseudocode it should be obvious that nothing is painted until the entire prompt command has finished (and the right prompt command, if provided).

  9. tannhuber commented on Sep 5, 2014

    @tannhuber
    Contributor

    Makes sense. I know how I could do it without this delay. Thank you for clarifying.

  10. sugar700 commented on Sep 6, 2014

    @sugar700
    Contributor

    It's actually more tricky, even ignoring right prompts. Fish shell automatically shortens too long prompts.

    xfix@damson ~> function fish_prompt
                       perl -e 'print "x" x 1e5'
                   end
    >

    For this to work, fish has to wait.

  11. added this to the fish-future milestone on Oct 12, 2014
  12. changed the title [-]For loop buffers output[/-] [+]Redirections and pipes involving blocks (like for-loops) buffer output[/+] on Jan 18, 2016
  13. enricotagliavini commented on Mar 8, 2016

    @enricotagliavini

    Just adding an explicit comment here in case somebody, like me, encounter the same problem and searches for already open bugs. When piping large amount of data via scripts and functions fish might exhaust available RAM memory, producing a system crash or triggering the OOM killer (on platforms having one).

    One notable case: grep. If grep is defined as a function, usually to automatically add --color=auto like in bash aliases, don't process large files (producing large outputs). I triggered this by processing apache httpd log files (many GBs) and boom, OOM.

    More details in issue #2803 .

    Limiting the buffer size and / or forcing a flush would be nice, if possible.

  14. added
    bug
    on Jun 3, 2016
  15. 4 remaining items

  16. paddor commented on Nov 2, 2018

    @paddor
  17. mqudsi commented on Mar 28, 2019

    @mqudsi
    Contributor

    Just a ping for those subscribed: @ridiculousfish committed some improvements to this as discussed in #5635

  18. zanchey commented on Sep 3, 2019

    @zanchey
    Member

    Actually all the examples given seem to work now - is there still work to be done here?

  19. ridiculousfish commented on Sep 8, 2019

    @ridiculousfish
    Member

    The way this was done was through a hack which covered the majority of cases, but not all, for example:

    for i in (seq 1 5); sleep 1; echo $i; end | begin ; cat ; end
    
  20. exploide commented on Sep 24, 2019

    @exploide
    Contributor

    I tried to fix #5340 again and this case also doesn't work, yet.

    When using the following function for ip:

    #
    # Enable colored output for ip when available
    #
    
    if command -sq ip; and command ip -color=auto l >/dev/null 2>/dev/null
        function ip
            command ip -color=auto $argv
        end
    end
    

    ip monitor | grep dev does not work. (I.e. piping slowly out of a fish function into a fish function)

  21. yalon commented on Jan 3, 2023

    @yalon

    I love using fish! This, however, is a very thorny issue, especially since aliasing commands like grep, which fish does by default to add coloring, cause them to behave radically different than what you'd expect coming from any other shell, and even if you do know about this issue, I think it's less convenient to start adding "command" before each part of a pipe - and even more cumbersome in case you really want to execute a function as part of a pipe.
    I read through this issue and #238, and I understand the appeal to add multi-threading/asyncio/ etc. concurrency models, but I also am painfully aware of how hard it is to add/change concurrency models to an existing code base.

    I have another idea and before I start looking into the code base I'd love to hear from the maintainers if this can be a valid approach.

    Basically, only when running a pipe line, run each pipe element as a sub-shell. It's also a feature that can be done opt-in so not to break any existing behavior.

    My reasoning for why this may be a reasonable approach is:

    • it will make the pipe behave as a pipe for every invocation, making it intuitive (to users coming from other shells) and even more importantly, consistent
    • performance overhead of a fork & exec is not an issue IMHO since I suspect the majority of the pipe use cases will run external commands anyhow (e.g. cat, grep, less, wc, ...)
    • I read somewhere about sharing state between jobs using fish variables - I couldn't think of a non-contrived use case for that when piping commands, but maybe I'm not thinking of the full picture

    To summarize, what about converting each pipe element to be the semi equivalent of "command fish -c " for each pipe element (guarded by an opt-in feature flag of course)?

  22. faho commented on Jan 3, 2023

    @faho
    Member

    Basically, only when running a pipe line, run each pipe element as a sub-shell. It's also a feature that can be done opt-in so not to break any existing behavior.

    This is a non-starter.

    Fish currently has no concept of subshells, and that's because it massively simplifies things.

    I read somewhere about sharing state between jobs using fish variables - I couldn't think of a non-contrived use case for that when piping commands, but maybe I'm not thinking of the full picture

    set -l lines
    
    somecommand | while read -l line
        set lines $lines $line
    end

    If you run the while read in a subshell, the variable won't be shared.

    This isn't a theoretical issue either, see http://mywiki.wooledge.org/BashFAQ/024.

  23. yalon commented on Jan 3, 2023

    @yalon

    Thanks @faho for the quick and detailed reply.

    I didn't mean sub-shell as in some kind of IPC happening between the parent and child shell, I should have written a sub-process, just like any external command running as a pipe element.

    And about the use case you wrote and the Bash FAQ link, thank you for showing a non-contrived use case :)

    In my mind at least, since people coming from other shells don't expect this behavior to work, I would guess there's less fish code out there sharing mutable variables between pipe commands. On the other hand, a streaming pipe is a very common use case IMHO - dunno if there's a way to validate my claim though.

    Adding to that, for input-reading use cases I think there are good alternatives like output capturing, etc.
    If we agree that streaming pipes happen more often than mutating variables in pipes, then IMHO it's a good compromise to ask the user to add a something explicit where they want to mutate a state.

    This has a benefit that it makes the shell's behavior clear because you're saying a statement: you want to share the state in this step, so this step will run sequentially.
    The current status is less favorable - you pipe some seemingly innocent commands but since a package/user/shell upgrade decided to turn that command into a function/alias, the streaming behavior changes silently. If you're piping an infinite stream for example you won't even know you have an issue.

    So, here's a revised idea, again, all subject to a feature flag so not to break existing behavior:

    • Each pipe element will run as a child process, same as command.
    • If sharing state is desired, a new keyword or an additional symbol next to the pipe will be added - there are better people than me to suggest great syntax, but the essence of that keyword/symbol would be "this step shares the variables state of the shell may buffer the entire output of the previous command or its own output". Basically, "shared/non-streamed".
    • Under this feature-flag, we can even warn/error the user if they're modifying a variable inside a pipe element without this setting this keyword/symbol first.

    Thanks again for discussing this - if there's interest I can see if I can get a PR for this, but it's not a small task IMHO, so I don't want to start working on it w/o maintainers blessing.

  24. faho commented on Jan 3, 2023

    @faho
    Member

    I didn't mean sub-shell as in some kind of IPC happening between the parent and child shell, I should have written a sub-process

    (that is what bash calls a "subshell")

    In my mind at least, since people coming from other shells don't expect this behavior to work

    They do, that's why it's a bash FAQ.

    And it's not just variables either. I once had a bash script that started background processes in a while read block and then tried to wait for them after. Instead the wait immediately succeeded because the while read ran in a separate process and so the background processes weren't known to the main bash process.

    On the other hand, a streaming pipe is a very common use case IMHO

    This affects "a streaming pipe between two blocks", which is a whole lot rarer, and in most cases will just have slightly degraded performance.

    In your specific case, both of these would not have buffered:

    python -c 'for i in range(100000000): print(i)' | command grep 35 | grep 989
    python -c 'for i in range(100000000): print(i)' | grep 35 | command grep 989

    Most users do not hit this regularly.

    If sharing state is desired, a new keyword or an additional symbol next to the pipe will be added

    This would break all code that relies on the current behavior, and there's no good way to find out which code that even is.

    It's not a workable solution. I'm not saying the buffering situation shouldn't be improved, I'm saying it's not a "stop the world" sort of thing where we break everything to fix it, especially not if it's awkward to even figure out if a given script would even be affected by the change.

    Thanks again for discussing this - if there's interest I can see if I can get a PR for this,

    Like I said, the multi-process thing is a non-starter, we're working on a multi-threading solution, but it's quite tricky and something that touches a lot of fish internals.

    I do not believe this is something for an external person as their first PR.

  25. WesselAtWork commented on Apr 2, 2025

    @WesselAtWork

    Heads up.
    IDK when this got fixed, but I am observing the requested functionality

    ><> fish -version
    fish, version 3.7.0
    
    ><> for i in (seq 1 5); sleep 1; echo $i; end | cat
    # 1 second of wait
    1
    # 1 second of wait
    2
    # 1 second of wait
    3
    # 1 second of wait
    4
    # 1 second of wait
    5
  26. faho commented on Apr 2, 2025

    @faho
    Member

    @WesselAtWork In some simple cases, fish now elides the buffer.

    But if you do | begin; cat; end it still buffers.

  27. oxalica commented on Apr 8, 2025

    @oxalica

    I did some experimental work on async script execution which fixes this issue without introducing sub-shells or fork: https://github.com/oxalica/ghoti-shell

  28. tgrushka commented on Jul 7, 2026

    @tgrushka
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions