terminal: fix deadlock with finer-grained locking

Commit 1bac87e804 ("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 <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
Koni Marti 2022-05-25 11:16:13 +02:00 committed by Robin Jarry
parent 0cc992b4e3
commit 57bd9e318b
1 changed files with 8 additions and 3 deletions

View File

@ -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)