Skip to content

Fix PortfolioTargetCollection.Contains(KeyValuePair) recursion - #10132

Merged
quantconnect-kronos[bot] merged 1 commit into
masterfrom
kronos/bug-10130-target-collection-contains
Nov 13, 2026
Merged

quantconnect-kronos[bot] merged 1 commit into
masterfrom
kronos/bug-10130-target-collection-contains

Conversation

@quantconnect-kronos

Copy link
Copy Markdown
Contributor

Closes #10130

Description

PortfolioTargetCollection.Contains(KeyValuePair<Symbol, IPortfolioTarget>) called itself with the same argument, so it recursed until the stack overflowed. It now checks the pair against the inner dictionary under the collection's lock, the same way SecurityManager.Contains(KeyValuePair<Symbol, Security>) does. The result is true only when the symbol key is present and maps to that exact target, which is what the method's documentation says ("whether the specified symbol/target pair exists").

Note: the issue suggested forwarding target.Value to Contains(IPortfolioTarget). That overload matches by symbol only, so a pair with a different key or a different target for the same symbol would also return true. The dictionary's own pair check avoids that.

No other member changes. The other paths of the collection behave as before.

Testing

  • New PortfolioTargetCollectionTests.ContainsKeyValuePair calls the method through ICollection<KeyValuePair<Symbol, IPortfolioTarget>> and directly. It covers a matching pair, a different target for the same symbol (false) and the same target under a different key (false).
  • dotnet build /p:Configuration=Release QuantConnect.Lean.sln: 0 errors.
  • Full unit test suite, run with the CI filter (TestCategory!=TravisExclude&TestCategory!=ResearchRegressionTests): Passed 39600, Failed 0, Skipped 100.
  • Tested end to end with a backtest whose algorithm calls the method through the generic collection interface. It returned true for the stored pair and false for another target with the same symbol, and the backtest completed normally.

The overload called itself with the same argument and recursed until the
stack overflowed. It now checks the pair against the inner dictionary,
like SecurityManager.Contains(KeyValuePair) does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot 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.

Approving. Nothing to fix.

The fix. Contains(KeyValuePair<Symbol, IPortfolioTarget>) now checks the pair against the inner dictionary while holding the same _targets lock the rest of the class uses. This is what SecurityManager.Contains(KeyValuePair<Symbol, Security>) (Common/Securities/SecurityManager.cs:134) and CashBook.Contains(KeyValuePair<string, Cash>) (Common/Securities/CashBook.cs:306) do. The dictionary compares values with EqualityComparer<IPortfolioTarget>.Default. PortfolioTarget doesn't override Equals, so a target matches only if it is the same instance. That is the same check Remove(IPortfolioTarget) makes with existing.Equals(target). The result is true only when the key is present and maps to that target, which is what the doc comment promises. A null key throws ArgumentNullException, as ContainsKey(null) already does; before this change the call crashed the process with a stack overflow.

Scope. Only the broken overload changed. Not taking the issue's suggestion of forwarding to Contains(IPortfolioTarget) is justified: that overload only checks the symbol, so it would have returned true for a different target under the same symbol, and for the right target under the wrong key. The issue left the approach to whoever fixes it.

Tests and CI. The new ContainsKeyValuePair test calls the method through ICollection<KeyValuePair<...>> and directly. It checks the matching pair, a different target for the same symbol, and the same target under a different key. Without the fix it would hit the recursion. Every CI check passes. The description reports a clean full unit test run and a passing end-to-end run.

Other repositories. None need an update. This only fixes an existing public method, with no signature change.

Risk. I set risk:low. The change is a few lines in one method that could only crash before, it is covered by a new test and an end-to-end run, CI is green, it follows the existing pattern, and it is easy to revert.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added risk:low Low risk and removed kronos:review:working review: in progress labels Nov 13, 2026
@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 13, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued and removed kronos:review Request review labels Nov 13, 2026
@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot added kronos:review:working review: in progress and removed kronos:review:queued review: queued labels Nov 13, 2026

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot 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.

Approving; no blocking findings.

The fix replaces the unconditional recursion with a dictionary pair lookup under the existing collection lock. It checks both the key and the stored target, rather than forwarding to the symbol-only target overload, which meets #10130's expected pair-membership behavior. The lookup is constant-time and does not enumerate or copy targets. No signatures or other collection operations change.

I checked the sibling implementations: SecurityManager, CashBook, BaseExtendedDictionary, and OptionChains delegate pair membership to their backing collections; none has this recursion. SecurityPortfolioManager has existing key-only behavior and is unaffected. No consumer, documentation, or stub updates are needed.

Verification:

  • dotnet test Tests/QuantConnect.Tests.csproj --configuration Release --filter FullyQualifiedName~PortfolioTargetCollectionTests: all 18 tests passed, including the new regression test.
  • Verified the CI logs for this commit: build completed with 0 errors; the full unit suite passed 39,600 tests, with 0 failures and 100 skipped. All applicable PR checks pass.
  • Independently tested end to end: a backtest exercised the direct overload and generic collection interface during initialization and on two trading days. Matching pairs, different target instances, mismatched keys, empty and cleared collections, replacements, and null stored values all produced the expected results; the backtest completed normally.

Retaining risk:low: this is a small, localized, easily reverted fix to a path that previously could not return normally, with regression coverage, green CI, and verified end-to-end behavior.

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot removed the kronos:review:working review: in progress label Nov 13, 2026
@quantconnect-kronos
quantconnect-kronos Bot merged commit 78de2cc into master Nov 13, 2026
8 checks passed
@quantconnect-kronos
quantconnect-kronos Bot deleted the kronos/bug-10130-target-collection-contains branch November 13, 2026 17:04
@quantconnect-kronos quantconnect-kronos Bot removed the kronos:develop:working develop: in progress label Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:low Low risk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PortfolioTargetCollection.Contains(KeyValuePair) recurses instead of returning

0 participants