Repository navigation
Fix PortfolioTargetCollection.Contains(KeyValuePair) recursion - #10132
Conversation
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>
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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 waySecurityManager.Contains(KeyValuePair<Symbol, Security>)does. The result istrueonly 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.ValuetoContains(IPortfolioTarget). That overload matches by symbol only, so a pair with a different key or a different target for the same symbol would also returntrue. The dictionary's own pair check avoids that.No other member changes. The other paths of the collection behave as before.
Testing
PortfolioTargetCollectionTests.ContainsKeyValuePaircalls the method throughICollection<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.TestCategory!=TravisExclude&TestCategory!=ResearchRegressionTests): Passed 39600, Failed 0, Skipped 100.truefor the stored pair andfalsefor another target with the same symbol, and the backtest completed normally.