Scanning a just-removed custom Python consolidator can crash the engine

未关闭
#10,161 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
77/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp, python
领域
backend, testing

调研方向

Start with Common/Data/SubscriptionManager.cs around ScanPastConsolidators, then trace removal through ConsolidatorWrapper.cs and its disposed guard. Run the supplied NUnit reproduction using Python.Runtime and QuantConnect.Tests.Engine.DataFeeds; done means removing a custom Python consolidator before its first scan does not access it again, and the scan completes without an active registration.

由索引模型根据 Issue 内容生成。

描述

priority:p2

Problem

On master at 8a0c6494f, adding a custom Python consolidator and removing it before its first scan leaves a disposed entry in _consolidatorsToAdd. The next ScanPastConsolidators calls AdvanceScanTime on that entry before checking Disposed. AdvanceScanTime reads WorkingData through the disposed Python adapter, which can crash the process.

This requires only one registration, not duplicate registration or sharing a consolidator between indicators.

Reproduction

The following uses Lean's existing NUnit test support (Python.Runtime, QuantConnect.Tests.Engine.DataFeeds):

using var gil = Py.GIL();
using var module = PyModule.FromString("pending_scan_removal", @"
from AlgorithmImports import *

class CustomConsolidator(PythonConsolidator):
    def __init__(self):
        self.consolidated = None
        self.working_data = None
        self.input_type = TradeBar
        self.output_type = TradeBar
    def update(self, data):
        pass
    def scan(self, time):
        pass

def prepare(algorithm):
    symbol = algorithm.add_equity('SPY').symbol
    source = CustomConsolidator()
    algorithm.subscription_manager.add_consolidator(symbol, source)
    algorithm.subscription_manager.remove_consolidator(symbol, source)
");
var algorithm = new AlgorithmStub();
using var pyAlgorithm = algorithm.ToPython();
using var result = module.GetAttr("prepare").Invoke(pyAlgorithm);
algorithm.SubscriptionManager.ScanPastConsolidators(
    algorithm.UtcTime.AddDays(1), algorithm);

Observed against the base implementation: the test host crashes during the final call. The add/remove calls are made from Python, so this does not depend on reusing a disposed C# PyObject handle.

Cause and required outcome

  • Common/Data/SubscriptionManager.cs:350-355 advances every pending scan entry unconditionally.
  • Removal marks its ConsolidatorWrapper disposed, but does not remove it from that pending collection.
  • Common/Data/ConsolidatorWrapper.cs:127 reads the disposed adapter's WorkingData.
  • The disposed-entry guard in ScanPastConsolidators only runs later, when dequeuing from the priority queue.

Removing a consolidator before its first scan must not read or invoke it again, and the next scan must complete without a retained active registration. A suggested fix is to discard disposed pending entries before advancing/enqueuing them, with regression coverage for a custom Python consolidator removed before its first scan.

Priority P2: confirmed process termination in an early-cleanup case, without evidence of widespread impact.

主要语言
C#
星标
22.6k
派生
5.4k
平均合并
11 小时 39 分钟
30 天内合并 PR
104

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

QuantConnect/Lean 的其他 Issue

查看 QuantConnect/Lean 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。