fix(links): use sync.Once for singleton init and guard nil namespace#3147
Open
Aprazor wants to merge 1 commit intosrl-labs:mainfrom
Open
fix(links): use sync.Once for singleton init and guard nil namespace#3147Aprazor wants to merge 1 commit intosrl-labs:mainfrom
Aprazor wants to merge 1 commit intosrl-labs:mainfrom
Conversation
GetHostLinkNode and getMgmtBrLinkNode use a check-then-write pattern on package-level variables without synchronization. Since containerlab deploys nodes in parallel, concurrent goroutines could both read nil and race on the write. Replace the manual nil check with sync.Once for thread-safe init. Also fix a nil dereference: when ns.GetCurrentNS() fails, currns is nil but the code calls currns.Path() unconditionally, panicking. Now return early on error so the singleton remains nil rather than crashing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two issues in the host and mgmt-net link node singletons:
1. Race condition on singleton initialization
GetHostLinkNode()andgetMgmtBrLinkNode()use a check-then-write pattern (if instance == nil { instance = ... }) on package-level variables without synchronization. Since containerlab deploys nodes in parallel (link resolution happens concurrently), two goroutines could both read nil and race on the write.Fix: Replace manual nil check with
sync.Oncefor thread-safe initialization.2. Nil dereference when GetCurrentNS fails
When
ns.GetCurrentNS()returns an error,currnsis nil. The code logs the error but unconditionally callscurrns.Path()on the next line, causing a nil pointer dereference panic.Fix: Return early on error so the singleton remains nil rather than crashing.
Testing
go vet ./links/...— cleango test -race ./links/...— all pass