From 665d42fc513db281fcc2e8bd538267315f60722e Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 3 Jan 2021 16:19:49 -0500 Subject: [PATCH] close channels so that everything dies properly --- bridge.go | 19 +++++++++++++++++-- discord.go | 10 ++++++++-- main.go | 8 ++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/bridge.go b/bridge.go index 6efd2ca..c3de13b 100644 --- a/bridge.go +++ b/bridge.go @@ -68,8 +68,20 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin <-ticker.C if mumble.State() != 2 { log.Println("Lost mumble connection " + strconv.Itoa(int(mumble.State()))) - die <- true - m.Close <- true + select { + default: + close(die) + case <-die: + //die is already closed + } + + select { + default: + close(m.Close) + case <-m.Close: + //m.Close is already closed + } + return } } }() @@ -81,6 +93,9 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin log.Println("\nGot internal die request. Terminating Mumble-Bridge") dgv.Disconnect() det.Detach() + close(die) + close(m.Close) + close(toMumble) Bridge.Connected = false } } diff --git a/discord.go b/discord.go index fb08488..77e2233 100644 --- a/discord.go +++ b/discord.go @@ -137,8 +137,14 @@ func discordReceivePCM(v *discordgo.VoiceConnection, die chan bool) { lastReady = true readyTimeout.Stop() } - - p, ok := <-v.OpusRecv + var ok bool + var p *discordgo.Packet + select { + case p, ok = <-v.OpusRecv: + case <-die: + log.Println("killing discord ReceivePCM") + return + } if !ok { log.Println("Opus not ok") continue diff --git a/main.go b/main.go index 8c85a40..c708750 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "log" "os" "os/signal" + "runtime" "strconv" "syscall" "time" @@ -101,6 +102,13 @@ func main() { userCount := make(chan int) go pingMumble(*mumbleAddr, strconv.Itoa(*mumblePort), userCount) go discordStatusUpdate(discord, userCount) + go func() { + for { + time.Sleep(3 * time.Second) + log.Println(runtime.NumGoroutine()) + //pprof.Lookup("goroutine").WriteTo(os.Stdout, 1) + } + }() if *autoMode { Bridge.AutoChan = make(chan bool) go AutoBridge(discord)