Repository navigation
fix(engine): stop disposing of one torrent twice, which panicked and wedged the engine - #7
Merged
Merged
Conversation
…wedged the engine anacrolix returns the same *torrent.Torrent for an infohash the client already knows, and its Drop() panics with "already closed" on the second call — the "it's always safe to do this" in its doc comment only holds once. A client retrying a link whose metadata never resolves lands several Add calls on one infohash at a time. Each gets that same torrent, and each closes it when its context expires: the first wins, the rest panic. Field logs show exactly this, three panics at the metadata-timeout Drop reporting the identical torrent pointer, right after repeated "timed out fetching torrent metadata". The same corruption reaches the active-torrent sweep, and that one is worse than a failed request. net/http recovers a handler panic, so the process survives — but the sweep runs under e.mu with a bare Unlock, so the panic leaves the engine mutex held forever. Every later List/Get/Add/stream blocks on it and the server goes silent while the container still reports healthy. Serialize add/drop/delete per infohash so concurrent callers can never share a torrent, make disposal idempotent so the paths that legitimately race can't double-close, and defer the sweep's unlock so a future panic there degrades to a failed request instead of a deadlock. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LcSmC4nXzcn2UAofR7JCgZ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Field report: the server goes silent mid-session, with
http: panic serving ... already closedin the logs.Cause
AddTorrentSpecreturns the same*torrent.Torrentfor an infohash the client already knows, and anacrolixDrop()panics on a second call:A client retrying a link whose metadata never resolves lands several
Addcalls on one infohash at once. Each is handed that same torrent, and each closes it onctx.Done():The logs match: repeated
timed out fetching torrent metadata, then three panics at that Drop reporting the identical torrent pointer (Drop(0xc003824008)).Why the service dies rather than just erroring
net/httprecovers a handler panic, so the process survives and only that request fails. But the same corruption also reachesenforceActiveLimitLocked, which runs undere.muwith a bareUnlock— the panic leaves the engine mutex held forever. Every laterList/Get/Add/stream blocks on the orphaned lock, so the server goes silent while the container still looks healthy. The reported log ends at exactly that panic.Fix
Drophad already lefte.managedbut not yet closed the torrent, letting anAddadopt it.Regression test
TestConcurrentAddOfUnresolvableHashreproduces it offline with no content and no network — DHT off, an infohash that can never resolve, six concurrent adds:Full suite passes under
-race.🤖 Generated with Claude Code
https://claude.ai/code/session_01LcSmC4nXzcn2UAofR7JCgZ