@@ -54,8 +54,7 @@ func (o *outputProducer) listen(r io.Reader, w io.Writer, appendBuffer func([]by
5454 for {
5555 o .opts .Logger .Println ("listen: loop" )
5656 if err := o .processNextRead (br , w , appendBuffer , size ); err != nil {
57- if errors .Is (err , ptyEOF ) {
58- o .opts .Logger .Println ("listen: reached EOF" )
57+ if errors .Is (err , PtyEOF ) {
5958 return nil
6059 } else {
6160 return fmt .Errorf ("could not poll reader: %w" , err )
@@ -64,7 +63,7 @@ func (o *outputProducer) listen(r io.Reader, w io.Writer, appendBuffer func([]by
6463 }
6564}
6665
67- var ptyEOF = errors .New ("pty closed" )
66+ var PtyEOF = errors .New ("pty closed" )
6867
6968func (o * outputProducer ) processNextRead (r io.Reader , w io.Writer , appendBuffer func ([]byte , bool ) error , size int ) error {
7069 o .opts .Logger .Printf ("processNextRead started with size: %d\n " , size )
@@ -78,6 +77,7 @@ func (o *outputProducer) processNextRead(r io.Reader, w io.Writer, appendBuffer
7877 pathError := & fs.PathError {}
7978 if errors .Is (errRead , fs .ErrClosed ) || errors .Is (errRead , io .EOF ) || (runtime .GOOS == "linux" && errors .As (errRead , & pathError )) {
8079 isEOF = true
80+ o .opts .Logger .Println ("reached EOF" )
8181 }
8282 }
8383
@@ -96,7 +96,8 @@ func (o *outputProducer) processNextRead(r io.Reader, w io.Writer, appendBuffer
9696
9797 if errRead != nil {
9898 if isEOF {
99- return errors .Join (errRead , ptyEOF )
99+ o .closeConsumers (PtyEOF )
100+ return errors .Join (errRead , PtyEOF )
100101 }
101102 return fmt .Errorf ("could not read pty output: %w" , errRead )
102103 }
@@ -194,6 +195,19 @@ func (o *outputProducer) processDirtyOutput(output []byte, cursorPos int, cleanU
194195 return append (append (alreadyCleanedOutput , processedOutput ... ), unprocessedOutput ... ), processedCursorPos , newCleanUptoPos , nil
195196}
196197
198+ func (o * outputProducer ) closeConsumers (reason error ) {
199+ o .opts .Logger .Println ("closing consumers" )
200+ defer o .opts .Logger .Println ("closed consumers" )
201+
202+ o .mutex .Lock ()
203+ defer o .mutex .Unlock ()
204+
205+ for n := 0 ; n < len (o .consumers ); n ++ {
206+ o .consumers [n ].Stop (reason )
207+ o .consumers = append (o .consumers [:n ], o .consumers [n + 1 :]... )
208+ }
209+ }
210+
197211func (o * outputProducer ) flushConsumers () error {
198212 o .opts .Logger .Println ("flushing consumers" )
199213 defer o .opts .Logger .Println ("flushed consumers" )
@@ -238,12 +252,12 @@ func (o *outputProducer) flushConsumers() error {
238252 return nil
239253}
240254
241- func (o * outputProducer ) addConsumer (tt * TermTest , consume consumer , opts ... SetConsOpt ) (* outputConsumer , error ) {
255+ func (o * outputProducer ) addConsumer (consume consumer , opts ... SetConsOpt ) (* outputConsumer , error ) {
242256 o .opts .Logger .Printf ("adding consumer" )
243257 defer o .opts .Logger .Printf ("added consumer" )
244258
245259 opts = append (opts , OptConsInherit (o .opts ))
246- listener := newOutputConsumer (tt , consume , opts ... )
260+ listener := newOutputConsumer (consume , opts ... )
247261 o .consumers = append (o .consumers , listener )
248262
249263 if err := o .flushConsumers (); err != nil {
0 commit comments