Skip to content

Commit 593b988

Browse files
committed
fix: channel panic
1 parent 85f58fd commit 593b988

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

internal/core/services/process_manager.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,35 +74,39 @@ 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+
7780
combinedChannel := make(chan string, 20)
7881
go func() {
82+
defer close(combinedChannel)
7983
for {
80-
//io.Reader to channel chunks
81-
tmpBuffer := make([]byte, 1024)
82-
n, err := out.Read(tmpBuffer)
83-
84-
if err != nil {
84+
select {
85+
case <-ctx.Done():
8586
return
87+
default:
88+
tmpBuffer := make([]byte, 1024)
89+
n, err := out.Read(tmpBuffer)
90+
if err != nil {
91+
return
92+
}
93+
combinedChannel <- string(tmpBuffer[:n])
8694
}
87-
88-
combinedChannel <- string(tmpBuffer[:n])
8995
}
9096
}()
9197

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

94100
//reset periodically
95101
process.Cmd.Wait()
102+
cancel() // Signal the goroutine to stop
96103

97104
po.processMonitor.RemoveProcess(commandName)
98105
po.RemoveProcess(commandName)
99106
// Wait for goroutine to print everything (watchdog closes stdin)
100107
exitCode = process.Cmd.ProcessState.ExitCode()
101108
console.MarkExited(exitCode)
102109

103-
close(combinedChannel)
104-
105-
//we wait, sothat we are sure all data is written to the console
106110
<-doneChan
107111

108112
process.Cmd = nil

0 commit comments

Comments
 (0)