Skip to content

Lack of 'wait' command? #1422

Closed
Closed

Description

@bzaborow

I've recently moved to fish and I'm missing the 'wait' command. Is there any particular reason why it is not implemented? Or maybe it exists, but i've somehow overlooked it?

It seems for me it's not implementable outside of the shell since it must have acces to the jobs list. Perhaps it should be a function...

Activity

  1. added this to the fish-future milestone on Apr 22, 2014
  2. zanchey commented on Apr 22, 2014

    @zanchey
    Member

    It's not implemented, but it might be a useful addtion; a naïve implementation should be easy to do as a function (with sleep and ps or similar).

  3. ibqn commented on Jun 1, 2014

    @ibqn

    indeed, I am also missing it.

  4. maxnordlund commented on Sep 24, 2014

    @maxnordlund
    Contributor

    I wrote an implementation using fg, it almost the same but slightly different output.

    function wait -d "Wait for the specified process and report its termination status.
      If N is not given, all currently active child processes are waited for,
      and the return code is zero.  N may be a process ID or a job
      specification; if a job spec is given, all processes in the job's
      pipeline are waited for."
      # The above help text is taken from `help wait` in a bash shell on Mac OS X.
    
      if test -n "$argv"
        fg $argv
        return $status
      end
      for pid in (jobs -p | tail +1)
        if kill -0 $pid 2>/dev/null
          fg $pid
        end
      end
      return $status
    end
  5. zanchey commented on Sep 29, 2014

    @zanchey
    Member

    The problem with using fg is that Ctrl-C will kill the target program, not the wait utility.

  6. roselma commented on Jan 5, 2016

    @roselma

    It seems to me that a built-in is a reasonable offering here. The absence of the wait command makes fish less expressive than other shells. maxnordlund's solution is clever, but it is inelegant to ask everyone to use that.

    (Just a +1.)

  7. ridiculousfish commented on Jan 6, 2016

    @ridiculousfish
    Member

    Yes, wait seems natural to me. Do you (or anyone) want to propose what its interface should be?

  8. krader1961 commented on Jan 6, 2016

    @krader1961
    Contributor

    I too think this should be implemented. Copying the interface from traditional shells like bash seems like an obvious starting point. It's not obvious to me as a relatively new user of the fish shell that there is any need to or merit in deviating from that interface.

  9. maxnordlund commented on Jan 6, 2016

    @maxnordlund
    Contributor

    Yeah, I would need/want just waiting for all jobs without argument and waiting for specific jobs when given job ids and/or pids of those jobs.

    It should probably do nothing if there are no jobs, maybe print an error message if it's run interactively.

  10. roselma commented on Jan 6, 2016

    @roselma

    Well, I thought that perhaps I'd just write the built-in myself and offer a patch (or a pull request or whatever the protocol is) but I looked into bash's implementation and realized that there is a non-trivial aspect to a good implementation of wait: You need to hold onto job exit status until either the jobs have been waited for (or, as an optimization, until it can be determined that the job identity is no longer knowable to the shell user). POSIX only requires that the last CHILD_MAX such job statuses are retained.

    I would strongly encourage not providing a wait command until/unless job status for already-exited but un-waited-for jobs can be supplied by the command; to do otherwise will enable people to unknowingly write buggy scripts, as is explained under "Application Usage" in the POSIX spec:
    http://pubs.opengroup.org/onlinepubs/009695399/utilities/wait.html

    I believe that it's worth considering an altered or extended interface that is capable of unambiguously supplying exit status information. See wait4(2). (In POSIX shells, an exit status greater than 127 is indistinguishable from an exit due to receipt of a signal.) It's also worth considering supplying a means to request exit status information for all the processes in a pipeline. That said, a POSIX-esque implementation would be entirely adequate for my needs.

  11. ridiculousfish commented on Jan 7, 2016

    @ridiculousfish
    Member

    Really great find @roselma . I think that section makes a compelling case for redesigning the wait interface.

  12. roselma commented on Jan 8, 2016

    @roselma

    @ridiculousfish A proposal:

    • retain-exit-status name
      Retain under the name name a handle to exit status/waiting for the last job started or placed in the background.
      (Equivalently, as far as I am concerned:
      retain-exit-status
      Retain a handle to exit status/waiting for the last job started or placed in the background. Return a name for this handle.)
    • wait-on-jobs name...
      Arguments are names of handles created by retain-exit-status. wait-on-jobs waits until at least one of the jobs associated with the supplied names has exited and returns the names for such jobs. If at least one of the named jobs had already exited, wait-on-jobs does not block; otherwise, it blocks only until one job has exited.
    • extract-exit-status name
      Return exit status information for the job associated with name, which must have previously been returned by wait-on-jobs.

    The retain-exit-status command makes the contract for when the shell must retain exit status information explicit. The wait-on-jobs command makes it easy to wait on an arbitrary set of jobs. The extract-exit-status command provides an interface for returning exit information, divorced of the complications surrounding when to block, and gives you a clean point at which to end the shell's responsibility for the exit status information.

    I omitted as many details as I at first thought I could but actually...

    tl;dr Let the user be explicit about when he may subsequently ask to wait on a job or know its exit status, and make him able to wait on arbitrary sets of jobs.

  13. faho commented on Jan 8, 2016

    @faho
    Member

    @roselma: I think at first we should try to figure out what functionality is needed.

    I've never really used wait so I can only comment from the outside, but for me it seems that you'll want to:

    • Suspend execution until a certain process or job finishes
    • Read the exit status for that process

    The main question I've had is: How do we deal with multiple jobs, especially regarding return code? wait 42 666 should obviously (tm) only exit when both 42 and 666 have exited, but when 42 fails (and then returns "54") but 666 succeeds, should we communicate that fact, and how?

    There's a couple solutions I can come up with:

    • Save status to a different (perhaps user-chosen) variable, probably as a list - this is basically what you've proposed with retain-exit-status.
    • Only return 0 when all jobs have been successful and 1 if at least one failed (or the number of failed jobs), and perhaps additionally output return codes to stdout
    • Make $status a list - this would break test $status -eq 0, though if it's only used in a new function... (still, inconsistency is bad)

    And now for some healthy self-criticism: I said above that wait 42 666 should "obviously" only exit if both exited. Should it? Am I stupid? Evil? (Yes I am 🎸). Of course that matches POSIX wait, but there might be a case where you'd wait for one. There might be a case for a "--any" option.

  14. ridiculousfish commented on Jan 8, 2016

    @ridiculousfish
    Member

    The current two-step:

    some command &
    pid=$!
    

    is a kind of boilerplate, and it's not explicit about whether you may want to wait on the command or not. The retain-exit-status idea is nice and explicit. We could roll it into the background syntax, taking a cue from redirections:

    sleep 3 &handle
    

    Now handle would be the name of the job for use in other commands. Or if we don't like changing this syntax, there's #1381 which suggests having a command decoration (like exec) to mean background.

    One advantage of this is that even retain-exit-status is in a sense annoyingly late, since the backgrounded job may finish even before the retain-exit-status job is executed. So we still have to speculatively hold onto job return values for at least a period.

    Regarding return codes, assuming wait 42 666 returns only after both have exited, it's not a big deal to return the exit code of the first failed process, or 0 if everyone succeeded. If you want to get individual exit codes, you can issue two separate wait commands.

  15. faho commented on Jan 8, 2016

    @faho
    Member

    If you want to get individual exit codes, you can issue two separate wait commands.

    Going by the same logic as above, you can't - the second job could have finished in the meantime. Unless of course we save the status and job id for at least a while - though if someone ever exceeds that while, they'd be in for a nasty surprise, and holding them indefinitely means using memory for potentially a long time (I've heard there are people who go months without rebooting or restarting the shell - never quite got that myself). That is, of course, unless we save it in a scoped variable that can be garbage collected.

  16. ridiculousfish commented on Jan 8, 2016

    @ridiculousfish
    Member

    Unless of course we save the status and job id for at least a while

    Any wait will put us on the hook for doing this at least some of the time, since a backgrounded process may finish even before the next command executes.

    Other shells have a problem: they don't know if you're going to wait or not, so they have to speculatively hold onto exit statuses, potentially forever. The idea for fixing this is to indicate at job creation time whether it will be waited for in the future. If you don't indicate that you're going to wait, we can discard the exit status immediately.

    A script that promises to wait but does not would create a (puny) leak, but then that would be a script bug that we could fix. It's much better than relying on heuristics and introspection like POSIX suggests.

  17. faho commented on Jan 8, 2016

    @faho
    Member

    The idea for fixing this is to indicate at job creation time whether it will be waited for in the future

    What would wait SOMEID then do if you didn't specify that? Always return 0? Return 0 if the job still existed?

    Or maybe the "command decoration" approach is the right one, with scoped handles, i.e.:

    function extractbg
      background -l extract tar -xf $argv
      wait extract # returns status of tar
    end
    wait extract # exits with status 127 since it was unknown
  18. faho commented on Jan 8, 2016

    @faho
    Member

    Also, I love that label!

  19. ridiculousfish commented on Jan 8, 2016

    @ridiculousfish
    Member

    wait SOMEID could be either an error or a no-op if the handle isn't valid.

  20. roselma commented on Jan 9, 2016

    @roselma

    If this wait command is to be non-POSIX, please do not make the semantics of it be such that it does not return until all jobs specified have exited.

    If it returns after at least one job has exited, it would be trivial to implement the wait-for-all semantics in a shell function. Conversely, it is impossible to implement wait-for-any semantics.

  21. maxnordlund commented on Jan 9, 2016

    @maxnordlund
    Contributor

    That is not a good default behaviour, hide the wait-for-any behind a flag (or just use a spinner like @bucaran did). Most of the time, if not all of the time, I use wait is to create a synchronization barrier for set of background jobs. Kinda like xargs except the jobs can heterogeneous, e.g. update brew/vim/pip at the same time, then wait for all them to complete.

  22. maxnordlund commented on Jan 26, 2016

    @maxnordlund
    Contributor

    I realized this simply synchronization command can be written like this:

    # Wait for the last job, unless there's no one left.
    while fg ^| grep -qv "There are no suitable jobs"; end
  23. roselma commented on Jan 27, 2016

    @roselma

    That would spin, not block. I don't know about your application, but in my case, expending one core worth of CPU cycles to wait for process termination is not a viable alternative.

    It's interesting that fish's fg returns zero when there are no background jobs. I would call that a bug of its own.

    On Jan 26, 2016, at 17:25, Max Nordlund [email protected] wrote:

    I realized this simply synchronization command can be written like this:

    Wait for the last job, unless there's no one left.

    while fg ^| grep -qv "There are no suitable jobs"; end
    —
    Reply to this email directly or view it on GitHub #1422 (comment).

  24. modified the milestones: fish-future, on Mar 13, 2018
  25. 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