Skip to content

Commit 1c2f5f8

Browse files
SAY-5dharmab
authored andcommitted
fix(simpleradio): use atomic.Bool for secureCoalitionRadios
The TCP receiver goroutine writes c.secureCoalitionRadios from updateServerSettings while the UDP receiver goroutine reads it from the voice-packet handling path, with no synchronization — a data race the Go race detector flags and an unlucky interleaving could let a voice packet skip the coalition filter while the flag is flipping. Switch the field to sync/atomic.Bool and route the write/read through Store/Load. The tri-state log message ('enabling secure coalition radios') still fires only on transitions, via a Load() check before Store(true). Refs dharmab/skyeye issue 661. Signed-off-by: SAY-5 <say.apm35@gmail.com>
1 parent 9967138 commit 1c2f5f8

3 files changed

Lines changed: 8 additions & 5 deletions

File tree

pkg/simpleradio/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net"
99
"sync"
10+
"sync/atomic"
1011
"time"
1112

1213
"github.com/dharmab/skyeye/pkg/simpleradio/types"
@@ -49,7 +50,9 @@ type Client struct {
4950
clientsLock sync.RWMutex
5051

5152
// secureCoalitionRadios indicates if the client should only receive transmissions from the same coalition.
52-
secureCoalitionRadios bool
53+
// Written by the TCP receiver goroutine (updateServerSettings) and read by the UDP receiver
54+
// goroutine (receive path); use atomic.Bool to avoid a data race.
55+
secureCoalitionRadios atomic.Bool
5356

5457
// rxChan is a channel where received transmission are published. A read-only version is available publicly.
5558
rxChan chan Transmission

pkg/simpleradio/message.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ func (c *Client) updateServerSettings(message types.Message) {
7878
log.Debug().Any("serverSettings", message.ServerSettings).Msg("received server settings")
7979
if enabled, ok := message.ServerSettings[string(types.CoalitionAudioSecurity)]; ok {
8080
if strings.ToLower(enabled) == "true" {
81-
if !c.secureCoalitionRadios {
81+
if !c.secureCoalitionRadios.Load() {
8282
log.Info().Msg("enabling secure coalition radios")
8383
}
84-
c.secureCoalitionRadios = true
84+
c.secureCoalitionRadios.Store(true)
8585
} else {
8686
log.Info().Msg("disabling secure coalition radios")
87-
c.secureCoalitionRadios = false
87+
c.secureCoalitionRadios.Store(false)
8888
}
8989
}
9090
if enabled, ok := message.ServerSettings[string(types.ExternalAWACSMode)]; ok {

pkg/simpleradio/receive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (c *Client) receiveVoice(ctx context.Context, in <-chan []byte, out chan<-
104104

105105
logger := log.With().Str("GUID", string(packet.OriginGUID)).Logger()
106106

107-
if c.secureCoalitionRadios {
107+
if c.secureCoalitionRadios.Load() {
108108
client, ok := c.clients[types.GUID(packet.OriginGUID)]
109109
if !ok {
110110
logger.Warn().Msg("ignoring voice packet from unknown client")

0 commit comments

Comments
 (0)