`DownloadHistoryService` records every event as a Torrent, and then discards the value anyway

オープン
#973 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

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

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp, sqlite
領域
backend, databases

調査の方向性

Start with listenarr.infrastructure/ActivityHistory/Services/DownloadHistoryService.cs, especially the cited recording methods, AddUnifiedAsync and ToLegacy; then read listenarr.domain/ActivityHistory/History.cs and the migration and schema-test references described in the issue. Trace how protocol values are resolved, persisted, and returned, and run the protocol round-trip tests across the named methods and client types. Done means known protocols survive write/read, unknown cases remain Unknown, and the migration-related tests pass.

索引モデルが issue の本文から書いたものです。

説明

p3

What is wrong

DownloadHistoryService builds a DownloadHistory for each event it records. RecordGrabbedAsync takes the protocol from its caller and uses it. Every other method hardcodes Protocol = DownloadProtocol.Torrent, at listenarr.infrastructure/ActivityHistory/Services/DownloadHistoryService.cs:125, :149, :174, :198, :221, :241 and :261 on canary a630572e. So a SABnzbd or NZBGet download that completes, fails, is imported, is paused, is resumed or is removed is filed as a torrent.
There is a second half to it, and it is the reason nobody has noticed. AddUnifiedAsync at :329 maps the DownloadHistory onto the unified History row that actually gets saved, and History in listenarr.domain/ActivityHistory/History.cs has no protocol field at all. So the value is dropped on the way to storage, including the correct one that RecordGrabbedAsync works out. Coming back the other way, ToLegacy at :353 never sets Protocol either, and DownloadProtocol.Torrent is the enum's zero member, so every row read back through GetHistoryAsync or GetLatestEventAsync reports Torrent whatever was recorded.
Net effect today: the protocol on a download history event is meaningless in both directions. It is wrong where it is constructed, it is discarded before it is stored, and it is fabricated on read.
I want to be plain that this is latent rather than user-visible right now. I looked for a consumer and could not find one: nothing in the tree reads Protocol off an IDownloadHistoryService result. The separate DownloadHistories table does have a protocol column and does carry a real value, but that table is written through IDownloadHistoryRepository by DownloadHashRetrievalService, which is a different path and is not affected. So the cost of this is not a broken screen today, it is that the next thing to read the protocol off a history row will get a plausible answer that happens to be wrong, and there will be nothing in the data to indicate it.

How to reproduce

On a checkout of canary, against the unified history path rather than the legacy repository:

  1. Record a Usenet grab: RecordGrabbedAsync(id, clientId, title, DownloadProtocol.Usenet).
  2. Record its import: RecordImportedAsync(id, clientId, title).
  3. Read both back with GetHistoryAsync(id, clientId).
    Both rows come back with Protocol == DownloadProtocol.Torrent. The grab was recorded as Usenet and the import was recorded as Torrent, and neither value survived, so the two agree only by accident.
    Inspecting the saved rows directly shows why: the History table has no protocol column to have saved it into.

What the fix branch does

Three things, because fixing any one alone leaves the value meaningless.

  1. History gains a nullable DownloadProtocol? column, added by a migration. Nullable on purpose: rows written before the column existed genuinely have no protocol, and defaulting them to the enum's first member would be the same mistake in a new place. AddUnifiedAsync carries the value through, and ToLegacy reads it back, mapping a null to DownloadProtocol.Unknown rather than to Torrent.
  2. RecordDownloadCompleteAsync, RecordDownloadFailedAsync and RecordImportedAsync take an optional DownloadProtocol?. A caller that knows is believed. A caller that does not, which is every current caller, gets the protocol resolved from the download client configuration: the client's type is looked up and the adapter registered for that type is asked what it speaks, which is the same pairing the queue and submission paths already rely on. A client that no longer exists, or a type with no adapter, resolves to Unknown.
  3. A second commit does the same for the remaining four methods, RecordImportFailedAsync, RecordPausedAsync, RecordResumedAsync and RecordRemovedAsync. It is separated so it can be dropped independently, but the defect in them is identical and leaving them seemed the wrong place to stop.
    Tests assert the recorded protocol per client type across the three methods, that a caller-supplied protocol wins without a lookup, that an unresolvable client records Unknown rather than Torrent, and that the value survives a write and a read. That last one matters most: without the column, any fix to the construction alone is unobservable, so a test of it would pass for the wrong reason.

Caveats worth stating

The branch adds a migration. It is an additive ADD COLUMN on a nullable column, so there is no table rebuild and no SQLite ALTER COLUMN exposure, and an older binary ignores the column. It is still a schema change in a change that is otherwise about a wrong constant, and if you would rather see the construction fixed and the persistence handled separately, that is a smaller diff and I am happy to split it. My reason for keeping them together is that the fix has nothing to show for itself otherwise.
Resolving the protocol costs one small indexed read of the download client configuration per recorded event, on paths that have already done network work.
Two overlaps to flag. A separate fix for the unified history mapping, covering the audiobook id type mismatch, touches the same AddUnifiedAsync method; the two are independent in intent and would need to be applied in a chosen order. And because this adds a migration, it also touches the model snapshot and the post-canary migration manifest that SqliteMigrationSchemaTests pins, which is the same pair of files two other open branches already collide on, so it will not merge cleanly alongside another branch that adds a migration.
I have not observed this on a running instance, because there is nothing to observe: the value never reaches storage. What is verified is the code path, at the commit cited.
Disclosure: drafted with Claude Code at my direction; I read the cited code at the stated commit and reviewed this before posting.

主要言語
C#
スター
901
フォーク
70
平均マージ
16時間 27分
マージ済み PR(30日)
60

環境構築

はじめの一歩

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

Listenarrs/Listenarr のほかの issue

Listenarrs/Listenarr の issue をすべて見る

似ている issue

C# の issue をもっと見る

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

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