Skip to content

feat: add CreateOAuthTokenHandler factory to Brokerage base class#9330

Open
Romazes wants to merge 1 commit intoQuantConnect:masterfrom
Romazes:feature/oauth-authentication-failure-shutdown
Open

feat: add CreateOAuthTokenHandler factory to Brokerage base class#9330
Romazes wants to merge 1 commit intoQuantConnect:masterfrom
Romazes:feature/oauth-authentication-failure-shutdown

Conversation

@Romazes
Copy link
Collaborator

@Romazes Romazes commented Mar 10, 2026

Description

  • Add AuthenticationFailed event to TokenHandler base class, raised when all retry attempts are exhausted after a failed token fetch
  • Add protected CreateOAuthTokenHandler<TRequest, TResponse>() factory method to Brokerage base class that creates an OAuthTokenHandler and automatically subscribes to AuthenticationFailed, routing it to OnMessage(BrokerageMessageType.Error) to trigger graceful Lean shutdown

Related PRs

Related Issue

Motivation and Context

When a live brokerage fails to refresh its OAuth access token (e.g. "Error refreshing access token."), the error was only logged and the algorithm continued running in a broken state. The root cause is that OAuthTokenHandler.GetAccessToken throws an InvalidOperationException which propagates through the HTTP stack but never reaches Lean's shutdown mechanism.

This change introduces a single centralized point - Brokerage.CreateOAuthTokenHandler - where the wiring between OAuth failure and Lean termination is defined. Individual brokerage implementations pass this factory when constructing their API clients; no per-brokerage error handling logic is needed.

Requires Documentation Change

No

How Has This Been Tested?

  • Solution builds with no errors
  • Verified the shutdown path: AuthenticationFailed event => OnMessage(BrokerageMessageType.Error) => DefaultBrokerageMessageHandler => algorithm.SetRuntimeError() => graceful Engine shutdown

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change which improves implementation)
  • Performance (non-breaking change which improves performance. Please add associated performance test and results)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Non-functional change (xml comments/documentation/etc)

Checklist:

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My branch follows the naming convention bug-<issue#>-<description> or feature-<issue#>-<description>

Introduce AuthenticationFailed event on TokenHandler raised when all
retry attempts are exhausted. Add CreateOAuthTokenHandler<TRequest,TResponse>
protected factory method on Brokerage that wires the event to OnMessage
(BrokerageMessageType.Error), triggering graceful Lean shutdown on
OAuth token refresh failure without requiring per-brokerage error logic.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Romazes Romazes requested a review from Martin-Molinero March 10, 2026 16:07
@Romazes Romazes self-assigned this Mar 10, 2026
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