DispatcherQueueSequencer leaks every DispatcherQueueTimer it creates

未关闭 适合新手
#254 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp

调研方向

从 src/ReactiveUI.Primitives.WinUI/Concurrency/DispatcherQueueSequencer.cs 入手,重点查看第 197 行附近创建和释放计时器的代码。检查计时器触发时以及释放其返回的 disposable 时,如何清理 Tick 订阅。完成标准是:计时器触发或取消后,延迟工作不再保留计时器或其捕获的工作;请用提供的复现进行验证。

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

描述

What happens

In DispatcherQueueSequencer.cs

timer.Tick += (_, _) =>
{
    timer.Stop();
    work();
};
timer.Start();
return Disposable.Create(timer, static t => t.Stop());

Tick is never unsubscribed. The native timer keeps the handler alive, and the handler holds on to the timer and to work. DispatcherQueueTimer isn't a XAML object, so the GC can't break that cycle, and calling Stop() doesn't help.

So every Delay/Throttle/Buffer/Timer on the main scheduler leaves a timer behind, together with whatever the scheduled work references. It doesn't matter whether the timer fired or was cancelled.

What you expected instead

No leaks

Minimal reproduction
using System.Reactive.Disposables;
using Microsoft.UI.Dispatching;
using ReactiveUI.Primitives.Reactive.Concurrency;

const int Count = 10_000;

var queue = DispatcherQueueController.CreateOnDedicatedThread().DispatcherQueue;
var scheduler = new DispatcherQueueSequencer(queue);

var payloads = new WeakReference[Count];
using var ran = new CountdownEvent(Count);

for (var i = 0; i < Count; i++)
{
    var payload = new byte[10_000];
    payloads[i] = new WeakReference(payload);
    scheduler.Schedule(payload, TimeSpan.FromMilliseconds(1), (_, _) =>
    {
        ran.Signal();
        return Disposable.Empty;
    });
}

ran.Wait();
Thread.Sleep(500);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Console.WriteLine($"{Count} delayed actions ran, payloads alive after GC: {payloads.Count(w => w.IsAlive)}");
Additional information

Versions: ReactiveUI.Primitives.WinUI.Reactive 10.2.3 (same code on main), Windows App SDK 2.7.0, .NET 10, Windows 11 x64.

主要语言
C#
星标
16
派生
4
平均合并
3 小时 38 分钟
30 天内合并 PR
6

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

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

reactiveui/Primitives 的其他 Issue

查看 reactiveui/Primitives 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

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