DispatcherQueueSequencer leaks every DispatcherQueueTimer it creates

オープン 初心者向け
#254 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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分
マージ済み PR(30日)
6

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

reactiveui/Primitives のほかの issue

reactiveui/Primitives の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。