From 09693ba1bf1b62645d6ebc16ffb879cb49ea8995 Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Sun, 31 Mar 2024 16:55:45 +0200 Subject: [PATCH] Further improvements to make race conditions less likely. --- CHANGELOG.md | 3 +++ main.go | 4 ++++ stanzahandling.go | 3 +++ 3 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42eae30..2052f1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog ## UNRELEASED +### Changed +- Fixed a race condition in receiving stanzas. + ### Added - Add a warning when run by the user *root*. diff --git a/main.go b/main.go index e26a142..73f26c9 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ import ( osUser "os/user" "runtime" "strings" + "sync" "time" "github.com/ProtonMail/gopenpgp/v2/crypto" // MIT License @@ -33,8 +34,11 @@ type configuration struct { alias string } +var mutex sync.Mutex + func closeAndExit(client *xmpp.Client, cancel context.CancelFunc, err error) { cancel() + mutex.Lock() client.Close() if err != nil { log.Fatal(err) diff --git a/stanzahandling.go b/stanzahandling.go index 386724c..8167e63 100644 --- a/stanzahandling.go +++ b/stanzahandling.go @@ -54,13 +54,16 @@ func rcvStanzas(client *xmpp.Client, iqc chan xmpp.IQ, msgc chan xmpp.Chat, ctx return default: go func() { + mutex.Lock() received, err = client.Recv() r <- received }() select { case <-ctx.Done(): + mutex.Unlock() return case <-r: + mutex.Unlock() } } // Don't print errors if the program is getting shut down,