Skip to content

Bugfix/devexp 765 syncronize token managers#156

Merged
marcos-sinch merged 14 commits into
v2.1-nextfrom
bugfix/DEVEXP-765-syncronize-token-managers
Jun 15, 2026
Merged

Bugfix/devexp 765 syncronize token managers#156
marcos-sinch merged 14 commits into
v2.1-nextfrom
bugfix/DEVEXP-765-syncronize-token-managers

Conversation

@marcos-sinch

@marcos-sinch marcos-sinch commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Synchronize token managers and refactor HTTP transport

Fixes a race condition in OAuth token creation and renewal under concurrent requests.

Token manager (token_manager.py)

  • Adds a threading.Lock to TokenManagerBase.
  • get_auth_token uses double-checked locking so that the initial token fetch happens exactly once even when many threads call it simultaneously.
  • Replaces the EXPIRED state + handle_invalid_token side-channel with a new refresh_auth_token(used_token) method: whichever thread holds the lock compares the used (stale) token against the currently cached one and fetches a new token only if they still match, deduplicating concurrent renewal attempts.

HTTP transport (http_transport.py, requests_http_transport.py)

  • Moves prepare_request and authenticate out of send() and into request(), so send() now receives an already-prepared HttpRequest instead of a raw HTTPEndpoint. This makes send() a pure I/O primitive and simplifies subclassing.
  • Refactor HttpTransport so new custom implementations uses send_request() instead of send() to avoid a breaking change for current implementations.
  • Extracts _get_bearer_token_from_request and _set_bearer_token helpers to keep the token-stamping logic in one place.

Tests

  • Replaces the abstract MockHTTPTransport test harness with tests that drive the real HTTPTransportRequests adapter.
  • Adds concurrency tests for both get_auth_token and refresh_auth_token that assert a single fetch under 20 simultaneous threads.

@JPPortier

Copy link
Copy Markdown

Hi @marcos-sinch : target branch should be 2.1 next but not main

@marcos-sinch

Copy link
Copy Markdown
Contributor Author

Hi @marcos-sinch : target branch should be 2.1 next but not main

@marcos-sinch marcos-sinch reopened this Jun 12, 2026
@marcos-sinch marcos-sinch changed the base branch from main to v2.1-next June 12, 2026 09:38

@JPPortier JPPortier left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please update also CHANGELOG file

Comment thread sinch/core/adapters/requests_http_transport.py Outdated
Comment thread sinch/core/ports/http_transport.py Outdated
Comment thread sinch/core/token_manager.py Outdated
Comment thread sinch/core/token_manager.py
@marcos-sinch marcos-sinch requested a review from JPPortier June 15, 2026 11:08
Comment thread sinch/core/token_manager.py Outdated
@marcos-sinch marcos-sinch requested a review from JPPortier June 15, 2026 13:21
@marcos-sinch marcos-sinch merged commit e3c52a7 into v2.1-next Jun 15, 2026
6 checks passed
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.

2 participants