fix(lldp): use mutexes to protect state

This commit is contained in:
Siyuan 2025-11-10 16:55:26 +00:00
parent 15484f889e
commit 826e5155b2
3 changed files with 9 additions and 2 deletions

View File

@ -102,10 +102,10 @@ func (l *LLDP) Start() error {
// StartRx starts the LLDP receiver if not already running // StartRx starts the LLDP receiver if not already running
func (l *LLDP) startRx() error { func (l *LLDP) startRx() error {
l.mu.Lock() l.mu.RLock()
running := l.rxRunning running := l.rxRunning
enabled := l.enableRx enabled := l.enableRx
l.mu.Unlock() l.mu.RUnlock()
if running || !enabled { if running || !enabled {
return nil return nil

View File

@ -88,6 +88,11 @@ func (l *LLDP) setUpCapture() error {
} }
func (l *LLDP) doCapture(logger *zerolog.Logger) { func (l *LLDP) doCapture(logger *zerolog.Logger) {
if l.pktSourceRx == nil || l.rxCtx == nil {
logger.Error().Msg("packet source or RX context not initialized")
return
}
l.rxWaitGroup.Add(1) l.rxWaitGroup.Add(1)
defer l.rxWaitGroup.Done() defer l.rxWaitGroup.Done()

View File

@ -194,7 +194,9 @@ func (l *LLDP) startTx() error {
cancel() cancel()
} }
l.mu.Lock()
l.txCtx, l.txCancel = context.WithCancel(context.Background()) l.txCtx, l.txCancel = context.WithCancel(context.Background())
l.mu.Unlock()
if err := l.setUpTx(); err != nil { if err := l.setUpTx(); err != nil {
return fmt.Errorf("failed to set up TX: %w", err) return fmt.Errorf("failed to set up TX: %w", err)