Skip to content

Commit 2bacf9f

Browse files
author
maks2134
committed
Fix deadlock in ttyWriter.Done()
Resolves race condition between main thread calling Done() and UI thread calling printWithDimensions(). The issue was that Done() held the mutex while sending to the done channel, but the UI thread needed the same mutex to process the done signal. Fixed by sending the done signal before acquiring the mutex, allowing the UI thread to receive the signal and release any held locks. Fixes #13639 Signed-off-by: maks2134 <maks210306@yandex.by>
1 parent e8c2143 commit 2bacf9f

8 files changed

Lines changed: 70 additions & 1 deletion

File tree

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compose.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/golinter.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/material_theme_project_new.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/display/tty.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,13 @@ func (w *ttyWriter) Start(ctx context.Context, operation string) {
176176

177177
func (w *ttyWriter) Done(operation string, success bool) {
178178
w.print()
179+
w.done <- true
179180
w.mtx.Lock()
180181
defer w.mtx.Unlock()
181182
if w.ticker != nil {
182183
w.ticker.Stop()
183184
}
184185
w.operation = ""
185-
w.done <- true
186186
}
187187

188188
func (w *ttyWriter) On(events ...api.Resource) {

cmd/display/tty_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package display
1818

1919
import (
2020
"bytes"
21+
"context"
2122
"strings"
2223
"sync"
2324
"testing"
@@ -422,3 +423,23 @@ func TestLenAnsi(t *testing.T) {
422423
})
423424
}
424425
}
426+
427+
func TestDoneDeadlockFix(t *testing.T) {
428+
w, _ := newTestWriter()
429+
addTask(w, "test-task", "Working", "details", api.Working)
430+
ctx, cancel := context.WithCancel(context.Background())
431+
defer cancel()
432+
433+
w.Start(ctx, "test")
434+
done := make(chan bool)
435+
go func() {
436+
w.Done("test", true)
437+
done <- true
438+
}()
439+
440+
select {
441+
case <-done:
442+
case <-time.After(5 * time.Second):
443+
t.Fatal("Deadlock detected: Done() did not complete within 5 seconds")
444+
}
445+
}

0 commit comments

Comments
 (0)