Skip to content

Commit 4c579b1

Browse files
authored
fix: race condition i tty exec (#74)
1 parent 0360a8b commit 4c579b1

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

internal/core/services/process_manager.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,28 @@ func (po *ProcessManager) RunTty(commandName string, command []string, cwd strin
7474

7575
var exitCode int
7676

77-
ctx, cancel := context.WithCancel(context.Background())
78-
defer cancel()
79-
8077
combinedChannel := make(chan string, 20)
78+
var readWG sync.WaitGroup
79+
readWG.Add(1)
8180
go func() {
81+
defer readWG.Done()
8282
defer close(combinedChannel)
83+
tmpBuffer := make([]byte, 1024)
8384
for {
84-
select {
85-
case <-ctx.Done():
86-
return
87-
default:
88-
tmpBuffer := make([]byte, 1024)
89-
n, err := out.Read(tmpBuffer)
90-
if err != nil {
91-
return
92-
}
85+
n, err := out.Read(tmpBuffer)
86+
if n > 0 {
9387
combinedChannel <- string(tmpBuffer[:n])
9488
}
89+
if err != nil {
90+
return
91+
}
9592
}
9693
}()
9794

9895
console, doneChan := po.consoleManager.AddConsoleWithChannel(commandName, domain.ConsoleTypeTTY, "stdin", combinedChannel)
9996

100-
//reset periodically
10197
process.Cmd.Wait()
102-
cancel() // Signal the goroutine to stop
98+
readWG.Wait() // drain PTY until EOF; early cancel dropped output vs. Wait()
10399

104100
po.processMonitor.RemoveProcess(commandName)
105101
po.RemoveProcess(commandName)

0 commit comments

Comments
 (0)