Skip to content

Conversation

@nathfavour
Copy link

I'm building vibeauracle, a CLI tool leveraging the Copilot agent, and found that the Go SDK's unsubscribe function was failing due to invalid function pointer comparisons. I've refactored the handler registration to use unique IDs, ensuring reliable cleanup while maintaining dispatch order.

Copilot AI review requested due to automatic review settings January 15, 2026 23:12
@nathfavour nathfavour requested a review from a team as a code owner January 15, 2026 23:12
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a critical bug in the Go SDK's session event handler unsubscription mechanism. The original implementation used invalid function pointer comparison (&h == &handler) which would never match, preventing handlers from being properly unsubscribed. The fix introduces a unique ID system for tracking handlers, ensuring reliable cleanup while maintaining the order of handler dispatch.

Changes:

  • Introduced sessionHandler struct with unique ID tracking for event handlers
  • Fixed unsubscribe logic to use ID comparison instead of invalid function pointer comparison
  • Updated dispatchEvent to properly extract handler functions from the wrapper struct

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +148 to +150
id := s.nextHandlerID
s.nextHandlerID++
s.handlers = append(s.handlers, sessionHandler{id: id, fn: handler})
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nextHandlerID counter uses uint64 which will eventually overflow after 2^64 increments. While this is extremely unlikely in practice (would require registering handlers trillions of times per second for years), consider documenting this assumption or adding a check for wraparound if this is a long-running system. Alternatively, using a map[uint64]SessionEventHandler would allow for ID reuse after unsubscription, though the current approach is likely sufficient for all practical purposes.

Copilot uses AI. Check for mistakes.
Comment on lines +246 to +249
handlers := make([]SessionEventHandler, 0, len(s.handlers))
for _, h := range s.handlers {
handlers = append(handlers, h.fn)
}
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unsubscribe functionality lacks dedicated test coverage. While the helper function GetFinalAssistantMessage in testharness/helper.go uses unsubscribe, there are no tests that verify: (1) multiple handlers can be registered and all receive events, (2) unsubscribing one handler doesn't affect others, (3) calling unsubscribe multiple times is safe as documented. Consider adding unit tests for these scenarios to prevent regression of the bug this PR fixes.

Copilot uses AI. Check for mistakes.
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.

1 participant