Skip to content

Schedule the brokerage reconnect check from the wall clock - #10271

Closed
quantconnect-kronos[bot] wants to merge 1 commit into
masterfrom
kronos/bug-67-outage-disconnect-grace-period
Closed

quantconnect-kronos[bot] wants to merge 1 commit into
masterfrom
kronos/bug-67-outage-disconnect-grace-period

Conversation

@quantconnect-kronos

@quantconnect-kronos quantconnect-kronos Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Closes #10270

Description

When a brokerage reports Disconnect, DefaultBrokerageMessageHandler waits before it stops the live algorithm. It waits 15 minutes when an exchange opens within the next 5 minutes. Otherwise it waits until 5 minutes before the next open.

To make that choice it read the algorithm time, but it computed the delay from the wall time. A live algorithm's time lags behind the wall time: by the warm-up period during warm-up, and by up to a slice in normal operation.

  • The delay could go negative. This happened when the algorithm time said the market was closed, but by the wall time the pre-open cutoff had already passed. A few seconds of lag around the cutoff were enough.
  • The check then never started. new CancellationTokenSource(delay) threw and the check was never scheduled.
  • Later disconnections were ignored. Brokerage.OnMessage had already recorded the brokerage as disconnected, so it dropped every later Disconnect as a duplicate. The algorithm kept running while disconnected, with no further shutdown check.

The change:

  • One clock. The handler now reads DateTime.UtcNow once and uses it for the open check, the next market open and the delay.
  • A check that is due runs. A delay below zero becomes zero.
  • A zero delay still runs the check. Task.Run no longer receives the token. A token that is already cancelled used to skip the task body, and with it CheckReconnected. A Reconnect before the task starts is still handled, because CheckReconnected does nothing while connected.

The handler's public API is unchanged. It serves every brokerage that reports Disconnect. QuantConnect/Lean.Brokerages.Tradier#68 starts reporting Tradier API outages through it.

Not changed: an algorithm whose securities are all custom data still fails in Min(), as before. That is #10262.

Tests

  • Tests/Engine/DefaultBrokerageMessageHandlerTests.cs has two new tests:

    • DoesNotThrowOnDisconnectDuringWarmUpWhenAlgorithmTimeIsInThePast: the algorithm time is 400 days behind and the algorithm is still warming up.
    • DoesNotThrowOnDisconnectWhenTheAlgorithmTimeLagsBehindThePreOpenCutoff: the algorithm time is 2 s before the pre-open cutoff and the wall time is 2 s after it.

    Both fail on master with ArgumentOutOfRangeException (Parameter 'delay') and pass with this change.

  • dotnet test Tests/QuantConnect.Tests.csproj --filter "FullyQualifiedName~DefaultBrokerageMessageHandlerTests": 4 passed on this branch. On master, the 2 new tests fail.

  • The full suite, run as CI runs it: dotnet build /p:Configuration=Release QuantConnect.Lean.sln and then dotnet test ./Tests/bin/Release/QuantConnect.Tests.dll --filter "TestCategory!=TravisExclude&TestCategory!=ResearchRegressionTests". Result: 40054 passed, 0 failed, 112 skipped.

  • Tested end to end with the Tradier brokerage, which reported a sustained 503 outage as Disconnect to this handler (5 s initial delay, default 5 minute pre-open threshold). Results:

    Case master This change
    Algorithm clock 4 s behind, 2 s before the pre-open cutoff Still running after 180 s; the handler threw and later Disconnects were dropped Stopped
    Still warming up, algorithm clock 400 days behind, at the pre-open cutoff Stopped, but only because the market was open at the algorithm time; the new warm-up unit test covers the closed case, which throws on master Stopped
    Market opens in 6 minutes Stopped Stopped

    In every case where the change stopped the algorithm, it did so within 120 s, with the brokerage's outage message as the runtime error.

DefaultBrokerageMessageHandler decided whether a market was about to open
from the algorithm time, then computed the check's delay from the wall
time. When the algorithm time lags, during warm up or just before the
pre-open cutoff, the delay came out negative and the check never started,
so a disconnected live algorithm kept running. Use one wall clock for both,
and run a check that is already due instead of skipping it.

Closes #10270

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quantconnect-kronos quantconnect-kronos Bot added the kronos:develop:working develop: in progress label Dec 1, 2026
@Romazes Romazes closed this Dec 1, 2026
@quantconnect-kronos quantconnect-kronos Bot removed the kronos:develop:working develop: in progress label Dec 1, 2026
@Martin-Molinero
Martin-Molinero deleted the kronos/bug-67-outage-disconnect-grace-period branch December 1, 2026 15:49
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.

DefaultBrokerageMessageHandler throws on a disconnection during warm-up: negative reconnect-check delay

1 participant