Skip to content

Remove IO buffering when piping out of function #5635

Closed

Description

@mqudsi

Now that @ridiculousfish updated the io buffer code for fish 3.1.0 and we've embraced background threads, I think we can finally do something about our broken function output buffering:

function foo
    echo hello
    sleep 10
end

foo | command grep hello # no output appears until timeout has elapsed

The easy way out is to use the background thread to read from the buffer and write to grep's stdin. The correct way would be to drop io buffering entirely since there's absolutely nothing stopping us from making the output pipe for foo be the input pipe to grep. The only place IO buffering is actually useful (and someone correct me if I'm wrong here) is command substitution.

Activity

  1. added this to the fish-future milestone on Feb 10, 2019
  2. ridiculousfish commented on Feb 10, 2019

    @ridiculousfish
    Member

    The main reason for my changes is to improve the architecture around process waiting, not responsiveness - though improved architecture can make other changes tractable.

    I wonder if a background thread that services the pipe makes sense, or if io_streams_t should be enhanced to know how to write directly to an fd. I'm thinking about questions like what if the child process exits, how do we implement back pressure, etc. (We don't handle these today at all but it would be good to know where we want to go).

    Note that buffering is useful when piping between two fish blocks or functions. The reason is that only one function in fish can execute at a time so the output must be fully buffered.

  3. mqudsi commented on Feb 12, 2019

    @mqudsi
    ContributorAuthor

    I wonder if a background thread that services the pipe makes sense, or if io_streams_t should be enhanced to know how to write directly to an fd. I'm thinking about questions like what if the child process exits, how do we implement back pressure, etc. (We don't handle these today at all but it would be good to know where we want to go).

    It was my thinking that we'd deal with issues along these lines by asking ourselves "how would it behave if this entire block were an external process invoked via a single fork()", but that is contingent on the main thread being able to just launch functions/builtins/processes and move on, so that the shell itself wouldn't have to take any of that into account (i.e. if the shell were replaced with a manually constructed chain of pipes hand-routed between the individual processes start from the very first input fd to the very last output fd), but that ties back directly to the next point:

    Note that buffering is useful when piping between two fish blocks or functions. The reason is that only one function in fish can execute at a time so the output must be fully buffered.

    Thanks for reminding me of that. I remember trying to see how much manipulation of global state takes place in executing a block by attempting to start a new thread to execute functions, but I couldn't figure out how to fork the parser state (this was a long time ago). The hypothetical behavior of simultaneous execution of blocks can much more easily be achieved by forking to execute blocks, but personally I would hope we never, ever go in that direction).

    (An alternate approach here is to do what I'm pretty sure bash does and basically expand functions to a sequence of builtins and external processes.)

  4. ridiculousfish commented on Feb 16, 2019

    @ridiculousfish
    Member

    Yeah supporting background execution of fish functions is a long term goal. IMO forking is out - it's incompatible with threads and threads are too convenient, plus it's too heavyweight and makes IPC too complicated.

    An attractive possibility is libuv-style async for everything. If C++ has async/await this might be tractable, but it's too difficult with our current design.

    I am convinced this will eventually work via multiple kernel threads and a Python GIL-style scheduler. Some variables will be "thread local" (but still managed by fish), and this will also enable multiplexing kernel state (for example, the working directory). A lot of the ground work of this has been laid already, such as instantizing the environment stack. I've done some prototyping (example.

    There's still a long tail of global state, e.g. the caching around set_color, the idea of the "current reader"; all that has to be removed and instanced. But removing this stuff is worthwhile anyways.

  5. faho commented on Mar 25, 2019

    @faho
    Member

    Fixed via 5eade35, at least in this limited case.

    The broader issue is #1396.

  6. modified the milestones: fish-future, fish 3.1.0 on Mar 25, 2019
  7. locked as resolved and limited conversation to collaborators on Apr 17, 2020
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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions