@@ -23,6 +23,7 @@ type TermTest struct {
2323 ptmx pty.Pty
2424 outputProducer * outputProducer
2525 listenError chan error
26+ waitError chan error
2627 opts * Opts
2728}
2829
@@ -78,6 +79,7 @@ func New(cmd *exec.Cmd, opts ...SetOpt) (*TermTest, error) {
7879 cmd : cmd ,
7980 outputProducer : newOutputProducer (optv ),
8081 listenError : make (chan error , 1 ),
82+ waitError : make (chan error , 1 ),
8183 opts : optv ,
8284 }
8385
@@ -227,6 +229,7 @@ func (tt *TermTest) start() (rerr error) {
227229 tt .term = vt10x .New (vt10x .WithWriter (ptmx ), vt10x .WithSize (tt .opts .Cols , tt .opts .Rows ))
228230
229231 // Start listening for output
232+ // We use a waitgroup here to ensure the listener is active before consumers are attached.
230233 wg := & sync.WaitGroup {}
231234 wg .Add (1 )
232235 go func () {
@@ -235,6 +238,16 @@ func (tt *TermTest) start() (rerr error) {
235238 err := tt .outputProducer .Listen (tt .ptmx , tt .term )
236239 tt .listenError <- err
237240 }()
241+
242+ go func () {
243+ // We start waiting right away, because on Windows the PTY isn't closed until the process exits, which in turn
244+ // can't happen unless we've told the pty we're ready for it to close.
245+ // This of course isn't ideal, but until the pty library fixes the cross-platform inconsistencies we have to
246+ // work around these limitations.
247+ defer tt .opts .Logger .Printf ("waitIndefinitely finished" )
248+ tt .waitError <- tt .waitIndefinitely ()
249+ }()
250+
238251 wg .Wait ()
239252
240253 return nil
@@ -247,13 +260,8 @@ func (tt *TermTest) Wait(timeout time.Duration) (rerr error) {
247260 tt .opts .Logger .Println ("wait called" )
248261 defer tt .opts .Logger .Println ("wait closed" )
249262
250- errc := make (chan error , 1 )
251- go func () {
252- errc <- tt .WaitIndefinitely ()
253- }()
254-
255263 select {
256- case err := <- errc :
264+ case err := <- tt . waitError :
257265 // WaitIndefinitely already invokes the expect error handler
258266 return err
259267 case <- time .After (timeout ):
0 commit comments