DispatcherQueueSequencer leaks every DispatcherQueueTimer it creates
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 78/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- csharp
- 领域
- backend, performance
调研方向
从 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 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
reactiveui/Primitives 的其他 Issue
-
难度 5/5 一周以上 新手友好度 35/100
reactiveui/Primitives#216 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 45/100
reactiveui/Primitives#154 · 11 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 25/100
reactiveui/Primitives#47 ·
维护者通常 1 天内回复
查看 reactiveui/Primitives 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
Facepunch/sbox-public#12456 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 84/100
mcneel/rhino.inside-revit#1736 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 76/100
Kyrodan/KeeAnywhere#510 ·
-
area-Tools-ILLink untriaged
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复