zsh-workers
 help / color / mirror / code / Atom feed
* use of "less" in function and interrupt
@ 2016-07-20 11:36 Vincent Lefevre
  2016-07-20 12:21 ` Peter Stephenson
  0 siblings, 1 reply; 6+ messages in thread
From: Vincent Lefevre @ 2016-07-20 11:36 UTC (permalink / raw)
  To: zsh-workers

With zsh 5.2, if I type:

zira% sleep 5 | less

and Ctrl-C immediately after, this interrupts the "sleep 5" and
everything is fine ("less" ignores the SIGINT).

But if I use a function:

zira% tless() { less "$@"; }
zira% sleep 5 | tless

then "less" is left stopped in background after I type a key.

bash and mksh do not have this problem (Debian's ksh93 93u+20120801-2
is completely broken as "less" is immediately put in background).

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: use of "less" in function and interrupt
  2016-07-20 11:36 use of "less" in function and interrupt Vincent Lefevre
@ 2016-07-20 12:21 ` Peter Stephenson
  2016-07-21  3:40   ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2016-07-20 12:21 UTC (permalink / raw)
  To: zsh-workers

On Wed, 20 Jul 2016 13:36:33 +0200
Vincent Lefevre <vincent@vinc17.net> wrote:
> With zsh 5.2, if I type:
> 
> zira% sleep 5 | less
> 
> and Ctrl-C immediately after, this interrupts the "sleep 5" and
> everything is fine ("less" ignores the SIGINT).
> 
> But if I use a function:
> 
> zira% tless() { less "$@"; }
> zira% sleep 5 | tless
> 
> then "less" is left stopped in background after I type a key.

Interestingly, I can get this to happen with

sleep 5 | { less }

but not

sleep 5 | ( less )

I'm not sure what that says about job control, which I've never really
understood, but the two cases should be very close as in the second the
shell knows it's about to exit and should exec less without an extra
fork.  I'm guessing that in the failing cases less is somehow being
detached from the terminal while in the other cases it isn't and, when
you quit less, normal service is resumed.

pws


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: use of "less" in function and interrupt
  2016-07-20 12:21 ` Peter Stephenson
@ 2016-07-21  3:40   ` Bart Schaefer
  2016-07-21 18:51     ` Vincent Lefevre
  2016-07-23 17:02     ` Bart Schaefer
  0 siblings, 2 replies; 6+ messages in thread
From: Bart Schaefer @ 2016-07-21  3:40 UTC (permalink / raw)
  To: zsh-workers

On Jul 20,  1:21pm, Peter Stephenson wrote:
} Subject: Re: use of "less" in function and interrupt
}
} On Wed, 20 Jul 2016 13:36:33 +0200
} Vincent Lefevre <vincent@vinc17.net> wrote:
} > With zsh 5.2, if I type:
} > 
} > zira% tless() { less "$@"; }
} > zira% sleep 5 | tless
} > 
} > then "less" is left stopped in background after I type a key.

After you type ^C, you mean?

} Interestingly, I can get this to happen with
} 
} sleep 5 | { less }

In both cases it works as expected provided that "sleep 5" has exited,
so you have to interrupt the sleep.

} but not
} 
} sleep 5 | ( less )
} 
} I'm not sure what that says about job control, which I've never really
} understood, but the two cases should be very close as in the second the
} shell knows it's about to exit and should exec less without an extra
} fork.

I believe the difference is that in both the functin case and the curly
brackets case you have a "current shell job" at the tail of the pipeline;
"less" is a subjob of that current shell job.  In the other cases you
have a true external job at the end of the pipeline; you're correct that
the subshell exec's less, so it's equivalent to piping directly to less.

} I'm guessing that in the failing cases less is somehow being
} detached from the terminal while in the other cases it isn't and, when
} you quit less, normal service is resumed.

Pretty much, yes.  I think what's happening is that the "current shell
job" is noticing that a ^C was delivered and therefore stops waiting
for its subjobs, not noticing that that "less" has ignored the signal
and continued on anyway.

Probably there's some point where the one last wait is needed, within
the cleanup of jobs in the current shell, to make sure that all the
subjobs really did go away.  I suspect this to be related to the
breakup of errflag into ERRFLAG_ERROR and ERRFLAG_INT.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: use of "less" in function and interrupt
  2016-07-21  3:40   ` Bart Schaefer
@ 2016-07-21 18:51     ` Vincent Lefevre
  2016-07-23 17:02     ` Bart Schaefer
  1 sibling, 0 replies; 6+ messages in thread
From: Vincent Lefevre @ 2016-07-21 18:51 UTC (permalink / raw)
  To: zsh-workers

On 2016-07-20 20:40:47 -0700, Bart Schaefer wrote:
> On Jul 20,  1:21pm, Peter Stephenson wrote:
> } Subject: Re: use of "less" in function and interrupt
> }
> } On Wed, 20 Jul 2016 13:36:33 +0200
> } Vincent Lefevre <vincent@vinc17.net> wrote:
> } > With zsh 5.2, if I type:
> } > 
> } > zira% tless() { less "$@"; }
> } > zira% sleep 5 | tless
> } > 
> } > then "less" is left stopped in background after I type a key.
> 
> After you type ^C, you mean?

After I type ^C, I get the zsh prompt back, but "less" is not stopped
yet. And if I type one or several other keys, "less" gets stopped.
Something like that.

> } Interestingly, I can get this to happen with
> } 
> } sleep 5 | { less }
> 
> In both cases it works as expected provided that "sleep 5" has exited,
> so you have to interrupt the sleep.

Yes, that's the goal of the sleep: to leave some time for the user
to interrupt the command.

In the reality for me, the sleep is actually some command that does
some output (often not immediately) and the function is a wrapper
to less.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: use of "less" in function and interrupt
  2016-07-21  3:40   ` Bart Schaefer
  2016-07-21 18:51     ` Vincent Lefevre
@ 2016-07-23 17:02     ` Bart Schaefer
  2016-07-25 13:40       ` Vincent Lefevre
  1 sibling, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2016-07-23 17:02 UTC (permalink / raw)
  To: zsh-workers

On Jul 20,  8:40pm, Bart Schaefer wrote:
}
} Probably there's some point where the one last wait is needed, within
} the cleanup of jobs in the current shell, to make sure that all the
} subjobs really did go away.  I suspect this to be related to the
} breakup of errflag into ERRFLAG_ERROR and ERRFLAG_INT.

Indeed the following seems to fix it.  All tests still pass and the
several interactive cases I tried appear to do the right thing, but
I'm not sure there isn't some edge case I've missed.  For example,
perhaps we should be saving and restoring errflag around the call
to signal_suspend() so that it can't be cleared by trap handlers?

I call attention to the comments from Oberon and PWS just a few lines
below the context of this diff.

diff --git a/Src/jobs.c b/Src/jobs.c
index 3db2ed0..ce8535d 100644
--- a/Src/jobs.c
+++ b/Src/jobs.c
@@ -1472,7 +1472,7 @@ zwaitjob(int job, int wait_cmd)
 	     */
 	    pipecleanfilelist(jn->filelist, 0);
 	}
-	while (!errflag && jn->stat &&
+	while (!(errflag & ERRFLAG_ERROR) && jn->stat &&
 	       !(jn->stat & STAT_DONE) &&
 	       !(interact && (jn->stat & STAT_STOPPED))) {
 	    signal_suspend(SIGCHLD, wait_cmd);


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: use of "less" in function and interrupt
  2016-07-23 17:02     ` Bart Schaefer
@ 2016-07-25 13:40       ` Vincent Lefevre
  0 siblings, 0 replies; 6+ messages in thread
From: Vincent Lefevre @ 2016-07-25 13:40 UTC (permalink / raw)
  To: zsh-workers

On 2016-07-23 10:02:35 -0700, Bart Schaefer wrote:
> Indeed the following seems to fix it.

Thanks. I confirm that this fixes the problem on my side.
After a few tests, I haven't noticed any drawback.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-07-25 13:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-20 11:36 use of "less" in function and interrupt Vincent Lefevre
2016-07-20 12:21 ` Peter Stephenson
2016-07-21  3:40   ` Bart Schaefer
2016-07-21 18:51     ` Vincent Lefevre
2016-07-23 17:02     ` Bart Schaefer
2016-07-25 13:40       ` Vincent Lefevre

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).