Skip to content

Lock main client from inside the session client when session requests are made#71

Merged
nikola-jokic merged 2 commits intomainfrom
nikola-jokic/lock-outer-client-when-used
Feb 16, 2026
Merged

Lock main client from inside the session client when session requests are made#71
nikola-jokic merged 2 commits intomainfrom
nikola-jokic/lock-outer-client-when-used

Conversation

@nikola-jokic
Copy link
Collaborator

Fixes #56

@nikola-jokic nikola-jokic requested a review from a team as a code owner February 12, 2026 13:41
Copilot AI review requested due to automatic review settings February 12, 2026 13:41
Copy link
Contributor

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 pull request addresses a race condition in token refresh when session requests are made from MessageSessionClient. The fix ensures that the parent Client's mutex is held when session requests invoke newActionsServiceRequest, which may trigger token refresh operations that modify shared client state.

Changes:

  • Lock the main client mutex in MessageSessionClient.doSessionRequest before calling methods that may update token state
  • Unlock the client mutex earlier in MessageSessionClient creation to avoid holding the lock during the initial session creation network call
  • Enable race detection in tests by adding the -race flag to the go test command

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
session_client.go Added locking of innerClient.mu in doSessionRequest to prevent concurrent token updates
client.go Moved the unlock to occur before createMessageSession is called to avoid holding the lock during network I/O in the creation phase
.github/workflows/go.yaml Added -race flag to test execution to detect race conditions

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

Comment on lines +238 to +239
c.innerClient.mu.Lock()
defer c.innerClient.mu.Unlock()
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

The mutex is held during HTTP I/O operations (lines 247-263), which could cause performance degradation in high-concurrency scenarios. While this pattern is consistent with how other Client methods work throughout the codebase, holding a mutex during network I/O can lead to head-of-line blocking where all operations on the parent Client are blocked while a session request is in progress.

However, this is necessary to prevent race conditions in updateTokenIfNeeded. Consider documenting this performance characteristic in the MessageSessionClient documentation, noting that session requests will serialize with other Client operations. Also consider whether future optimizations might use a more fine-grained locking strategy (e.g., separate locks for token state vs. other Client state).

Copilot uses AI. Check for mistakes.
@nikola-jokic nikola-jokic merged commit ebedf0b into main Feb 16, 2026
14 checks passed
@nikola-jokic nikola-jokic deleted the nikola-jokic/lock-outer-client-when-used branch February 16, 2026 11:58
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.

Race condition in token refresh under high concurrency

3 participants