Commit 006e980
authored
* fix(download-manager): validate output before marking a download complete
Both the torrent and HTTP paths could mark a download complete without
checking that anything was actually written to dest. The torrent path
trusted torrent.download() returning cleanly; the HTTP path only
checked SHA256 when one was supplied, so an empty or truncated body
still passed. Add a shared validation step (file exists, size > 0, size
matches the known total when available, SHA256 matches when provided)
and run it before marking either path complete. On failure the task is
marked error and the bad file is removed, matching the existing
SHA-mismatch behavior.
Fixes the backend half of #1548.
* fix(downloads): fold review findings on the false-complete validation
- MAJOR: do not treat Content-Length as the expected on-disk size for
content-encoded (gzip/br/deflate/zstd) responses. httpx auto-decompresses,
so the written file is larger than Content-Length and the size check would
delete a valid download. Leave total_bytes unknown and rely on the SHA.
- normalize SHA256 comparison to lowercase so an uppercase expected hash is
not a false mismatch
- torrent path: torrent.download() already SHA-verifies internally, so skip
the redundant full-file re-hash (avoids a multi-GB blocking read)
* fix(downloads): offload the SHA fallback read to a thread so it can't block the loop
The computed_sha256-is-None fallback re-reads the whole file to hash it;
for a multi-GB model that would stall the event loop. Move _validate_download
to async and run the read+hash via asyncio.to_thread.
1 parent 90cab1e commit 006e980
2 files changed
Lines changed: 137 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
314 | 314 | | |
315 | 315 | | |
316 | 316 | | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
317 | 334 | | |
318 | 335 | | |
319 | 336 | | |
| |||
391 | 408 | | |
392 | 409 | | |
393 | 410 | | |
| 411 | + | |
394 | 412 | | |
395 | 413 | | |
396 | 414 | | |
397 | 415 | | |
398 | 416 | | |
399 | 417 | | |
400 | 418 | | |
| 419 | + | |
401 | 420 | | |
402 | 421 | | |
403 | 422 | | |
| |||
414 | 433 | | |
415 | 434 | | |
416 | 435 | | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
417 | 492 | | |
418 | 493 | | |
419 | 494 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
100 | 132 | | |
101 | 133 | | |
102 | 134 | | |
| |||
133 | 165 | | |
134 | 166 | | |
135 | 167 | | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
136 | 183 | | |
137 | 184 | | |
138 | 185 | | |
| |||
160 | 207 | | |
161 | 208 | | |
162 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
163 | 218 | | |
164 | | - | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
165 | 223 | | |
166 | 224 | | |
167 | 225 | | |
168 | 226 | | |
169 | 227 | | |
170 | | - | |
| 228 | + | |
| 229 | + | |
171 | 230 | | |
172 | 231 | | |
173 | | - | |
| 232 | + | |
174 | 233 | | |
175 | 234 | | |
176 | 235 | | |
| |||
0 commit comments