Skip to content

Lock ordering risk: buildAlerts holds c.mu then acquires ss.mu #2

Description

@rajfirke

Problem

buildAlerts() in internal/source/collector.go:144 acquires c.mu first, then calls c.store.Snapshot() which acquires ss.mu.RLock — lock order: c.mu → ss.mu.

Collector.Snapshot() does the reverse sequentially (not nested): ss.mu.RLock then c.mu.

Currently safe because Snapshot() releases ss.mu before acquiring c.mu. But if someone refactors Snapshot() to hold ss.mu longer (e.g., to fix issue #1), the ordering becomes ss.mu → c.mu, which deadlocks against buildAlerts's c.mu → ss.mu.

Impact

Latent — only triggers if the code is refactored a specific way. No current deadlock.

Suggested Fix

Document the lock ordering convention (ss.mu before c.mu), or refactor buildAlerts to snapshot first, release store lock, then acquire c.mu to write alerts.

Files

  • internal/source/collector.go:144-182

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingconcurrencyRace conditions, lock ordering, thread safety

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions