Two go routines reading from the same channel. The first go routine never prints its shutdown message after the done
channel is closed, while the second go routine always does.
Why is the message from the first go routine not printing and is the method even returning
?
main.go
func main() {
done := make(chan bool)
c := make(chan os.Signal, 1)
cameras := client.CameraConfig()
client.DrawUserControls(cameras)
operator := client.NewOperator(cameras)
go operator.UserInputListener(done)
go operator.ParseAndExecuteUserCommand(done)
signal.Notify(c, os.Interrupt)
for range c {
close(done)
break
}
log.Println("Interrupt signal received. Shutting client down....")
time.Sleep(5 * time.Second)
}
client.go
func (o *Operator) UserInputListener(done <-chan bool) {
reader := bufio.NewReader(os.Stdin)
for {
select {
case <-done:
log.Println("Keyboard listener shutting down.") // <-- this never prints
return
default:
line, _, err := reader.ReadLine()
if err != nil {
log.Println(err)
}
data := strings.Split(string(line), "")
id, err := strconv.Atoi(data[1])
if err != nil {
log.Println(err)
continue
}
switch data[0] {
case "b":
o.Controls <- Ctrl{
Identifier: id,
Ctrl: "run",
}
case "t":
o.Controls <- Ctrl{
Identifier: id,
Ctrl: "terminate",
}
case "r":
o.Controls <- Ctrl{
Identifier: id,
Ctrl: "record",
}
case "s":
o.Controls <- Ctrl{
Identifier: id,
Ctrl: "stop",
}
}
}
}
}
func (o *Operator) ParseAndExecuteUserCommand(done <-chan bool) {
for {
select {
case <-done:
log.Println("Command operator shutting down.")
return
case ctrl := <-o.Controls:
switch ctrl.Ctrl {
case "run":
o.Room[ctrl.Identifier].Run()
case "terminate":
o.Room[ctrl.Identifier].Close()
case "record":
o.Room[ctrl.Identifier].Write()
case "stop":
o.Room[ctrl.Identifier].Stop()
}
}
}
}