From 57bd9e318b7073692627bd72f067633609e26883 Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Wed, 25 May 2022 11:16:13 +0200 Subject: [PATCH] terminal: fix deadlock with finer-grained locking Commit 1bac87e80414 ("terminal: fix race when closing a terminal") fixed a race in Terminal.Draw by using a mutex. The current locking of the entire Draw function could create a deadlock, however, since this function itself might call Terminal.Close which is protected by the same mutex. A finer-grained locking solves both the race and deadlock problem. Signed-off-by: Koni Marti Acked-by: Robin Jarry --- widgets/terminal.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/widgets/terminal.go b/widgets/terminal.go index 1d4c1df..e499e3e 100644 --- a/widgets/terminal.go +++ b/widgets/terminal.go @@ -232,9 +232,6 @@ func (term *Terminal) invalidate() { } func (term *Terminal) Draw(ctx *ui.Context) { - term.closeMutex.Lock() - defer term.closeMutex.Unlock() - if term.destroyed { return } @@ -252,7 +249,15 @@ func (term *Terminal) Draw(ctx *ui.Context) { if term.pty == nil { term.vterm.SetSize(ctx.Height(), ctx.Width()) + + term.closeMutex.Lock() + if term.cmd == nil { + term.closeMutex.Unlock() + return + } tty, err := pty.StartWithAttrs(term.cmd, &winsize, &syscall.SysProcAttr{Setsid: true, Setctty: true, Ctty: 1}) + term.closeMutex.Unlock() + term.pty = tty if err != nil { term.Close(err)