You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Redirections and pipes involving blocks (like for-loops) buffer output #1396
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
Reacted by Jannik Vieten, Marian Steinbach, lispmachine, Adam Žingor, David D Lowe, oxalica, Bas van den Wollenberg, Nikita Bilous and Jean Mertz
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.
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 $iend'| cat
It would be really nice to fix it, but nobody made a fix yet.
@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_promptif 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).
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.
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#ifcommand-sq ip; andcommand ip -color=auto l >/dev/null 2>/dev/null
function ip
command ip -color=auto $argvendend
ip monitor | grep dev does not work. (I.e. piping slowly out of a fish function into a fish function)
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)?
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 |whileread-l line
set lines $lines$lineend
If you run the while read in a subshell, the variable won't be shared.
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.
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)'|commandgrep 35 |grep 989
python -c'for i in range(100000000): print(i)'|grep 35 |commandgrep 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.
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
Activity
ridiculousfish commentedon Apr 3, 2014
Yes, redirections and pipes involving blocks are run serially, not in parallel.
CGamesPlay commentedon Apr 3, 2014
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.
ridiculousfish commentedon Apr 6, 2014
It's a longstanding and annoying issue.
sugar700 commentedon Apr 7, 2014
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).
It would be really nice to fix it, but nobody made a fix yet.
tannhuber commentedon Sep 4, 2014
It's annoying, indeed. Does anybody have an idea how to fix it?
ridiculousfish commentedon Sep 4, 2014
The long-term plan is to support full multithreaded execution. See #563
tannhuber commentedon Sep 4, 2014
Would be great. Is this also the reason, why
shows the prompt with 3 seconds delay?
lilyball commentedon Sep 4, 2014
@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_promptas being executed in a manner similar toIn this pseudocode it should be obvious that nothing is painted until the entire prompt command has finished (and the right prompt command, if provided).
tannhuber commentedon Sep 5, 2014
Makes sense. I know how I could do it without this delay. Thank you for clarifying.
sugar700 commentedon Sep 6, 2014
It's actually more tricky, even ignoring right prompts. Fish shell automatically shortens too long prompts.
For this to work, fish has to wait.
[-]For loop buffers output[/-][+]Redirections and pipes involving blocks (like for-loops) buffer output[/+]enricotagliavini commentedon Mar 8, 2016
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.
Do not indent the runVerbose output. Better for log output and preven…
4 remaining items
paddor commentedon Nov 2, 2018
mqudsi commentedon Mar 28, 2019
Just a ping for those subscribed: @ridiculousfish committed some improvements to this as discussed in #5635
zanchey commentedon Sep 3, 2019
Actually all the examples given seem to work now - is there still work to be done here?
ridiculousfish commentedon Sep 8, 2019
The way this was done was through a hack which covered the majority of cases, but not all, for example:
exploide commentedon Sep 24, 2019
I tried to fix #5340 again and this case also doesn't work, yet.
When using the following function for
ip:ip monitor | grep devdoes not work. (I.e. piping slowly out of a fish function into a fish function)yalon commentedon Jan 3, 2023
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:
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)?
faho commentedon Jan 3, 2023
This is a non-starter.
Fish currently has no concept of subshells, and that's because it massively simplifies things.
If you run the
while readin a subshell, the variable won't be shared.This isn't a theoretical issue either, see http://mywiki.wooledge.org/BashFAQ/024.
yalon commentedon Jan 3, 2023
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:
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.
faho commentedon Jan 3, 2023
(that is what bash calls a "subshell")
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 readblock and then tried towaitfor them after. Instead thewaitimmediately succeeded because thewhile readran in a separate process and so the background processes weren't known to the main bash process.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:
Most users do not hit this regularly.
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.
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.
WesselAtWork commentedon Apr 2, 2025
Heads up.
IDK when this got fixed, but I am observing the requested functionality
faho commentedon Apr 2, 2025
@WesselAtWork In some simple cases, fish now elides the buffer.
But if you do
| begin; cat; endit still buffers.oxalica commentedon Apr 8, 2025
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
tgrushka commentedon Jul 7, 2026