diff options
author | xengineering <me@xengineering.eu> | 2023-07-03 13:58:58 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-07-03 13:58:58 +0200 |
commit | 938e92387ae1aa3f771ed219ee65b2dbc34b6738 (patch) | |
tree | b4e1661006ef4acc1c289da4b279dc3f79b2cff0 /limox.go | |
parent | 4bca452afe2b91dd6933738a87247eab960c2959 (diff) | |
download | limox-938e92387ae1aa3f771ed219ee65b2dbc34b6738.tar limox-938e92387ae1aa3f771ed219ee65b2dbc34b6738.tar.zst limox-938e92387ae1aa3f771ed219ee65b2dbc34b6738.zip |
Fix race condition on struct Limox
To make message passing to the XMPP session channel non-blocking short
living Goroutines are used. Before this commit they were executing
closures which capture l as xengineering.eu/limox.Limox pointer and used
it to get the channel where the message should be passed to.
While channels are safe with respect to race conditions, structs are
not. Thus the member access of the Limox struct was a race condition in
certain side cases like bad network connection where those Goroutines
lived for a longer time.
The solution is to make a copy of the l.sessionIn channel and use this
copy in the closures. This is valid since channels do not need pointers
to them and furthermore are thread safe.
Diffstat (limited to 'limox.go')
-rw-r--r-- | limox.go | 5 |
1 files changed, 3 insertions, 2 deletions
@@ -80,6 +80,7 @@ func (l *Limox) run() error { } func (l *Limox) buttonCallback() { + c := l.sessionIn switch l.State { case Disconnected: jid := l.JidEditor.Text() @@ -89,10 +90,10 @@ func (l *Limox) buttonCallback() { l.sessionIn = xmpp.StartSession(l.sessionOut, jid, pwd) l.State = Connecting case Connecting: - go func() { l.sessionIn <- xmpp.SessionShouldDisconnect{} }() + go func() { c <- xmpp.SessionShouldDisconnect{} }() l.State = Disconnected case Connected: - go func() { l.sessionIn <- xmpp.SessionShouldDisconnect{} }() + go func() { c <- xmpp.SessionShouldDisconnect{} }() l.State = Disconnected } } |