Skip to content

fix(engine): stop disposing of one torrent twice, which panicked and wedged the engine - #7

Merged
jodacame merged 1 commit into
mainfrom
fix/double-drop-panic
Aug 8, 2026
Merged

jodacame merged 1 commit into
mainfrom
fix/double-drop-panic

Conversation

@jodacame

@jodacame jodacame commented Aug 8, 2026

Copy link
Copy Markdown
Owner

Field report: the server goes silent mid-session, with http: panic serving ... already closed in the logs.

Cause

AddTorrentSpec returns the same *torrent.Torrent for an infohash the client already knows, and anacrolix Drop() panics on a second call:

func (t *Torrent) close(wg *sync.WaitGroup) (err error) {
	if !t.closed.Set() {
		err = errors.New("already closed")   // t.go:105 turns this into a panic

A client retrying a link whose metadata never resolves lands several Add calls on one infohash at once. Each is handed that same torrent, and each closes it on ctx.Done():

select {
case <-t.GotInfo():
case <-ctx.Done():
	t.Drop()        // engine.go:391 — every concurrent caller reaches this

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/http recovers a handler panic, so the process survives and only that request fails. But the same corruption also reaches enforceActiveLimitLocked, which runs under e.mu with a bare Unlock — the panic leaves the engine mutex held forever. Every later List/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

  • Serialize add/drop/delete per infohash, so concurrent callers never share a torrent. This also closes the narrower window where Drop had already left e.managed but not yet closed the torrent, letting an Add adopt it.
  • Make disposal idempotent, so paths that legitimately race (drop-after-playback, the seed enforcer, the sweep, shutdown) cannot double-close.
  • Defer the sweep's unlock, so any future panic there degrades to a failed request instead of a deadlock.

Regression test

TestConcurrentAddOfUnresolvableHash reproduces it offline with no content and no network — DHT off, an infohash that can never resolve, six concurrent adds:

### without the fix
panic: already closed
	anacrolix/torrent@v1.58.1/t.go:105 +0xd2
FAIL

### with the fix
ok  	github.com/jodacame/fluxtorrent/internal/engine	2.789s

Full suite passes under -race.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LcSmC4nXzcn2UAofR7JCgZ

…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
@jodacame
jodacame merged commit 8d16b11 into main Aug 8, 2026
2 checks passed
@jodacame
jodacame deleted the fix/double-drop-panic branch August 8, 2026 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant