mirror of
https://github.com/wader/fq.git
synced 2024-12-24 22:05:31 +03:00
interp: More sure stdOS stops the signal forward gorutine
This commit is contained in:
parent
7b7faaf02b
commit
6034ad7d67
@ -35,27 +35,42 @@ func (a autoCompleterFn) Do(line []rune, pos int) (newLine [][]rune, length int)
|
||||
|
||||
type stdOS struct {
|
||||
rl *readline.Instance
|
||||
closeChan chan struct{}
|
||||
interruptChan chan struct{}
|
||||
}
|
||||
|
||||
func newStandardOS() *stdOS {
|
||||
closeChan := make(chan struct{})
|
||||
interruptChan := make(chan struct{}, 1)
|
||||
interruptSignalChan := make(chan os.Signal, 1)
|
||||
signal.Notify(interruptSignalChan, os.Interrupt)
|
||||
|
||||
// this more or less converts a os signal chan to just a struct{} chan that
|
||||
// ignores signals if forwarding it would block, also this makes sure interp
|
||||
// does not know about os.
|
||||
go func() {
|
||||
interruptSignalChan := make(chan os.Signal, 1)
|
||||
signal.Notify(interruptSignalChan, os.Interrupt)
|
||||
defer func() {
|
||||
signal.Stop(interruptSignalChan)
|
||||
close(interruptSignalChan)
|
||||
close(interruptChan)
|
||||
}()
|
||||
for range interruptSignalChan {
|
||||
|
||||
for {
|
||||
select {
|
||||
case interruptChan <- struct{}{}:
|
||||
default:
|
||||
case <-interruptSignalChan:
|
||||
// ignore if interruptChan is full
|
||||
select {
|
||||
case interruptChan <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
case <-closeChan:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return &stdOS{
|
||||
closeChan: closeChan,
|
||||
interruptChan: interruptChan,
|
||||
}
|
||||
}
|
||||
@ -205,7 +220,7 @@ func (o *stdOS) Close() error {
|
||||
if o.rl != nil {
|
||||
o.rl.Close()
|
||||
}
|
||||
close(o.interruptChan)
|
||||
close(o.closeChan)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user