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

server/topic.Subscribe can produce duplicate subscriber IDs #712

Closed
peterbourgon opened this issue May 1, 2023 · 5 comments
Closed

server/topic.Subscribe can produce duplicate subscriber IDs #712

peterbourgon opened this issue May 1, 2023 · 5 comments
Labels
🪲 bug Something isn't working

Comments

@peterbourgon
Copy link

subscriberID := rand.Int()

// Subscribe subscribes to this topic
func (t *topic) Subscribe(s subscriber, userID string, cancel func()) int {
	t.mu.Lock()
	defer t.mu.Unlock()
	subscriberID := rand.Int() // <---- here
	t.subscribers[subscriberID] = &topicSubscriber{
		userID:     userID, // May be empty
		subscriber: s,
		cancel:     cancel,
	}
	t.lastAccess = time.Now()
	return subscriberID
}

Nothing prevents subscriberID := rand.Int() from producing an integer which is already present in the t.subscribers map. In that case, the previous subscriber is over-written.

@peterbourgon peterbourgon added the 🪲 bug Something isn't working label May 1, 2023
@dropdevrahul
Copy link
Contributor

can I pick this up?

@binwiederhier
Copy link
Owner

Sure thing

@dropdevrahul
Copy link
Contributor

@binwiederhier tried a fix with #713

@dropdevrahul
Copy link
Contributor

dropdevrahul commented May 2, 2023

Please note Just added a simple retry mechanism and added a test case to catch the edge case

@binwiederhier
Copy link
Owner

Thanks to you both. The best effort retry should be enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🪲 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants