Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race condition when adding endpoints from goroutine #1484

Merged
merged 2 commits into from
Jan 12, 2024

Conversation

actatum
Copy link
Contributor

@actatum actatum commented Dec 7, 2023

fixes race condition when setting endpoint subscription, appending to list of endpoints and setting endpoint stats.

Description:

When AddEndpoint() calls addEndpoint() the Endpoint is assigned a subscription, the service list of endpoints is appended to, and the Endpoint's stats field is populated. There is no lock around any of this and if the AddEndpoint method is called from another goroutine it can cause a race condition.

This PR includes a test that recreates the race condition and can be verified by removing the lock and unlock calls that have been added to service.go running the test TestAddEndpoint_RaceCondition with the -race flag on. If you replace the lock/unlock calls the test will pass.

@piotrpio piotrpio self-requested a review January 12, 2024 10:15
@piotrpio
Copy link
Collaborator

@actatum thank you for the PR and sorry for the late review. LGTM!

@piotrpio piotrpio merged commit 51412b7 into nats-io:main Jan 12, 2024
1 check passed
@piotrpio piotrpio mentioned this pull request Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants