dataplaneapi icon indicating copy to clipboard operation
dataplaneapi copied to clipboard

Data race in config-parser: unprotected DefaultSectionName global variable

Open phihos opened this issue 1 month ago • 0 comments

The config-parser package has a package-level global variable that causes data races when multiple parser instances are used concurrently:

File: config-parser/parser.go:65

var DefaultSectionName = "" //nolint:gochecknoglobals

Written at: config-parser/reader.go:198

DefaultSectionName = data.Name  // NO mutex protection!

This variable is written by (*configParser).ProcessLine() during parsing without any synchronization, causing race conditions when multiple goroutines parse configurations simultaneously.

This may cause issues like:

==================
WARNING: DATA RACE
Write at 0x000005472da0 by goroutine 1284:
  github.com/haproxytech/client-native/v6/config-parser.(*configParser).ProcessLine()
      /home/runner/go/pkg/mod/github.com/haproxytech/client-native/[email protected]/config-parser/reader.go:198 +0x5fa8
  github.com/haproxytech/client-native/v6/config-parser.(*configParser).Process()
      /home/runner/go/pkg/mod/github.com/haproxytech/client-native/[email protected]/config-parser/reader.go:90 +0x88f

Previous write at 0x000005472da0 by goroutine 1283:
  github.com/haproxytech/client-native/v6/config-parser.(*configParser).ProcessLine()
      /home/runner/go/pkg/mod/github.com/haproxytech/client-native/[email protected]/config-parser/reader.go:198 +0x5fa8

Reproducer:

package main

import (
	"strings"
	"sync"
	"testing"

	parser "github.com/haproxytech/client-native/v6/config-parser"
)

func TestParallelParsing(t *testing.T) {
	config := `
global
	daemon

defaults unnamed_defaults_1
	mode http

backend api
	balance roundrobin
	server srv1 192.168.1.10:80
`

	var wg sync.WaitGroup
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			p, _ := parser.New()
			_ = p.Process(strings.NewReader(config))
		}()
	}
	wg.Wait()
}

Run with: go test -race

phihos avatar Oct 22 '25 21:10 phihos