Skip to content

pkg/utils' DebugManager singleton makes its tests unparallelizable, and RecordEvent broadcasting is the reason #373

Closed

Description

@scttfrdmn

Split out of #179's paralleltest burn-down. 19 of the findings are in pkg/utils/debug_mode_test.go, and they are the only ones in that backlog that a t.Parallel() cannot fix. They are excluded in .golangci.yml with this reason recorded; this issue is what makes the exclusion removable.

Why the tests can't be parallelized as written

Two facts, and only the second is the real obstacle.

DebugManager is a package-level singleton behind a sync.Once, with no exported constructor:

type DebugManager struct {
	mu       sync.RWMutex
	sessions map[string]*DebugSession
	logger   *StructuredLogger
}

func GetDebugManager() *DebugManager {
	debugManagerOnce.Do(func() { ... })
	return globalDebugManager
}

Every test calls GetDebugManager() and gets the same instance. On its own that would be workable — each test already uses a distinct session ID (test-session-1 … test-session-14), so the sessions themselves don't collide.

The blocker is DebugManager.RecordEvent (pkg/utils/debug_mode.go:150), which fans every event out to all open sessions:

func (dm *DebugManager) RecordEvent(component, operation, message string, fields map[string]any) {
	dm.mu.RLock()
	sessions := make([]*DebugSession, 0, len(dm.sessions))
	for _, session := range dm.sessions {
		sessions = append(sessions, session)
	}
	dm.mu.RUnlock()

	goroutineID := getGoroutineID()

	for _, session := range sessions {
		session.RecordEvent(component, operation, message, fields, goroutineID)
	}
}

So a session's event count is a function of what every other concurrently-open session recorded. Unique IDs isolate the session map and nothing else.

Measured, not predicted

Adding t.Parallel() to all 19 and running -race -count=4:

TestRecordEvent            Expected 1 event, got 2
TestGetEventsByComponent   Expected 2 storage events, got 6
                           Expected 2 cache events, got 3
TestGetStats               Expected 3 events in stats, got 8
                           Expected 2 storage events in stats, got 4
                           Expected 1 cache event in stats, got 4
TestDebugTrace             Expected 1 event, got 17
TestDebugTrace_WithError   Expected 1 event, got 17

No data race — the locking is correct. The counts are wrong because the broadcast is doing exactly what it is written to do.

TestSetLogger has a second, smaller version of the same problem: it calls dm.SetLogger(logger) on the shared instance, so it mutates a field every other test's sessions log through.

The fix

Give DebugManager an exported constructor so a test can own one:

func NewDebugManager() *DebugManager {
	return &DebugManager{sessions: make(map[string]*DebugSession)}
}

GetDebugManager() stays as the process-wide accessor for production callers — the singleton is the right shape for a debug facility that arbitrary code paths record into without plumbing a handle. What is missing is only the ability to construct a second one, which is what makes the type testable.

Then each test builds its own manager, t.Parallel() applies to all 19, and the .golangci.yml exclusion for pkg/utils/debug_mode_test.go is deleted.

Worth checking while there: whether the broadcast is the intended semantics at all. It means two unrelated debug sessions each see the other's events, which is defensible for a global trace facility but is not what "start a session for components X and Y" reads as promising. If it is intended, it wants a sentence in the doc comment saying so, because a caller reading session.GetStats() has no way to know the count includes traffic it did not generate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testseffort: smallpriority: lowtype: technical-debt

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions