From 9a06a2e0b66723da0f9d08a6584ba829e4b329b7 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Wed, 11 Oct 2023 15:21:22 +0000 Subject: [PATCH] chore(pty/ptytest): add sync.Once to close --- pty/ptytest/ptytest.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/pty/ptytest/ptytest.go b/pty/ptytest/ptytest.go index 544adc242990e..6ee737563d2d4 100644 --- a/pty/ptytest/ptytest.go +++ b/pty/ptytest/ptytest.go @@ -350,22 +350,28 @@ func (e *outExpecter) fatalf(reason string, format string, args ...interface{}) type PTY struct { outExpecter pty.PTY + closeOnce sync.Once + closeErr error } func (p *PTY) Close() error { p.t.Helper() - pErr := p.PTY.Close() - if pErr != nil { - p.logf("PTY: Close failed: %v", pErr) - } - eErr := p.outExpecter.close("PTY close") - if eErr != nil { - p.logf("PTY: close expecter failed: %v", eErr) - } - if pErr != nil { - return pErr - } - return eErr + p.closeOnce.Do(func() { + pErr := p.PTY.Close() + if pErr != nil { + p.logf("PTY: Close failed: %v", pErr) + } + eErr := p.outExpecter.close("PTY close") + if eErr != nil { + p.logf("PTY: close expecter failed: %v", eErr) + } + if pErr != nil { + p.closeErr = pErr + return + } + p.closeErr = eErr + }) + return p.closeErr } func (p *PTY) Attach(inv *clibase.Invocation) *PTY {