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

Abierto
#973 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
csharp, sqlite

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C#
Estrellas
901
Forks
70
Merge medio
16 h 27 min
PR fusionados (30 d)
60

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Listenarrs/Listenarr

Todos los issues de Listenarrs/Listenarr

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.