Skip to content

Data race in config-parser: unprotected DefaultSectionName global variable #393

@phihos

Description

@phihos

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions