Skip to content

Commit

Permalink
fix: channel panic
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcStdt committed Nov 17, 2024
1 parent 85f58fd commit 593b988
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions internal/core/services/process_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,35 +74,39 @@ func (po *ProcessManager) RunTty(commandName string, command []string, cwd strin

var exitCode int

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

combinedChannel := make(chan string, 20)
go func() {
defer close(combinedChannel)
for {
//io.Reader to channel chunks
tmpBuffer := make([]byte, 1024)
n, err := out.Read(tmpBuffer)

if err != nil {
select {
case <-ctx.Done():
return
default:
tmpBuffer := make([]byte, 1024)
n, err := out.Read(tmpBuffer)
if err != nil {
return
}
combinedChannel <- string(tmpBuffer[:n])
}

combinedChannel <- string(tmpBuffer[:n])
}
}()

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

//reset periodically
process.Cmd.Wait()
cancel() // Signal the goroutine to stop

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

close(combinedChannel)

//we wait, sothat we are sure all data is written to the console
<-doneChan

process.Cmd = nil
Expand Down

0 comments on commit 593b988

Please sign in to comment.