Repository navigation
Modernize for Go 1.26 and fix crashes on argument-less queries - #86
Merged
Merged
Conversation
Require Go 1.26 and upgrade direct dependencies to their latest releases. Drops the direct dependencies on anacrolix/multiless, bradfitz/iter, docopt-go and golang.org/x/exp in favour of the standard library.
ShivamB25
force-pushed
the
modernize-refactor
branch
from
September 22, 2026 19:30
cf56afd to
a1ce649
Compare
Test on Go 1.26, the minimum declared in go.mod, and on the latest stable release with current action versions. Run go vet, and add a golangci-lint job that uses the go.mod Go version.
Bug fixes: - Queries that require arguments (announce_peer, put, get) no longer dereference a nil arguments dict, which let any remote packet crash the server. They now get a protocol error, as get_peers and find_node already did. - find_node and get replies returned the nodes closest to the (unset) info_hash instead of the queried target. - Server.Put ignored its QueryRateLimiting argument. - getput.Get and getput.Put dereferenced a nil seq in unsigned responses, ignored errors from TraversalStartingNodes, and didn't wait for the traversal to stop before reading its stats. - krpc.Error decoding no longer relies on recover to reject malformed errors, and NodeInfo decoding checks its input length. Refactors: - Split handleQuery into a handler per query method. - Use the standard library in place of multiless, bradfitz/iter, docopt, x/exp/constraints and ioutil: cmp, slices, maps, math/rand/v2, errors.AsType, sync.OnceValue, WaitGroup.Go, atomic.Uint64, binary.Append*, range over int. - Replace the big.Int in int160.BitLen with math/bits, and cache the Castagnoli CRC table used for node ID security checks. - Remove dead code, unused fields, commented-out code, and stale or editorializing comments. Move test-only helpers into _test.go files. - Rename k-nearest-nodes/k-nearest-nodes.go.go. Adds regression tests for the argument-less queries and the find_node target, plus int160 tests.
ShivamB25
force-pushed
the
modernize-refactor
branch
from
September 22, 2026 19:31
a1ce649 to
b9dcf0d
Compare
BootstrapContext started a traversal and then returned without stopping it when TraversalStartingNodes failed, leaving the operation's run goroutine blocked forever. Fetch starting nodes first in Bootstrap, AnnounceTraversal and getput so the error paths never start a traversal, and add tests that check no traversal goroutine survives those failures.
Drop the blanket errcheck exclusion for _test.go. The only findings were in dht_test.go; they now assert or return on error. TestBootstrapRace unmarshalled the whole read buffer rather than the bytes read.
BootstrapContext returned as soon as its context was done, leaving outstanding find_node queries running until they timed out. Bootstrap and bucket refresh queries now use the traversal's context so stopping cancels them, and BootstrapContext always waits for the traversal to stop, returning its stats along with any context error.
Go evaluates a return statement's results before deferred functions run, so refreshBucket called Operation.Stats before Stopped. The named result is now assigned after the traversal stops, and a test checks the traversal goroutine is gone when refresh returns.
Announce.TraversalStats copied the stats struct while queries update the same fields with atomic adds, which races. LoadStats reads both counters atomically, and the announce, bootstrap, and get/put snapshots use it.
getPeers waited on traversal.Stopped while that query was still outstanding, so Close never reached Finished if nobody received from Peers. The send now also selects on the query context, which Stop cancels. The stats race test waits for the writer to increment before it loads.
TestAnnounceCloseWithoutReadingPeers only called Close after the send was blocked, so a failure before that left the traversal running.
Close did not cancel an in-flight query, so a ping that had already been sent waited out the resend delay. Query now ends when the server context is cancelled or the caller context ends. closestNodes kept an arbitrary prefix of a bucket. It keeps the nearest nodes by XOR distance. Operation.Closest raced query updates and now returns a snapshot taken under the operation lock. The block list was written under the server lock and read without it, so it is published atomically. BEP 44 cas was compared with the previous write's cas instead of the stored sequence number. Wrapper.Put holds one lock across that read and the write. ping no longer dereferences a response with no id, and dht-server closes the node when loading its table fails so the deferred socket close does not panic the serve loop.
Rank eligible nodes across the entire routing table. Release the server lock around configurable callbacks, store access, marshaling and status output, preserving veto and closed-state checks. Cancel and join CLI queries and bootstrap before returning or saving state. Check mutable CAS before sequence handling and compute Memory targets outside its mutex. Replace implementation-pinning tests with reproduced behavioral regressions and deterministic lifecycle handshakes.
Wait for an actual bootstrap packet before cancellation and assert transaction cleanup. Give callback and cancellation fixtures their own limiter so unrelated tests cannot terminate their traversal through rate limiting.
Require complete compact addresses and present BEP44 values. Report errors when no remote put succeeds, preserving remote protocol errors. Reject invalid announce ports, cancel rate-limited replies on Close, and prevent writes after a blocklist callback closes the server.
Reject malformed mutable keys and node-list responses instead of panicking. Preserve numeric-address semantics while resolving hostnames with the command context. Surface KRPC errors and prevent automatic sequence overflow.
Skip queued aliases for an already queried endpoint. Bound and yield both stats-test workloads, make callback fixture failure cleanup safe, assert callback reachability, remove vacuous external-behavior tests, and use ephemeral ports.
Use the signal context for query and ping-nodes hostname resolution. Reject invalid generic and IPv4-family compact address encodings instead of emitting bytes the decoder refuses, and cover negative announce ports.
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.
Modernizes the codebase for Go 1.26 and fixes a remotely triggerable crash, among other bugs. The commits can be reviewed separately.
Advisor residual fixes (
fd77e0b)CI run 35857753915 passed on this exact SHA: Go 1.26, stable, and lint.
queryandping-nodesnow use the same context-aware resolver as ping. Controlled DNS regressions reproduced that both commands ignored SIGINT before the fix; both now stop within the bounded cancellation deadline.NodeAddr.MarshalBinaryrejects IP lengths other than 4/16 bytes, matching its 6/18-byte decoder contract. Compact IPv4 lists also return an error for IPv6/nil input rather than panicking in the generic slice marshaler.tcpfor numeric and hostname inputs.go test -race -shuffle=on -count=2 -timeout 120s ./..., native/Windows vet, and lint (0 issues) passed.76452a2, using the local DHT replacement atfd77e0b, passedGOWORK=off go build ./...andGOWORK=off go test -short -count=1 -timeout 120s . ./tracker/....Adversarial edge-case review (
265bf4a)CI run 35856680846 completed successfully on this exact commit: Go 1.26, stable, and lint.
Six independent review tracks covered server boundaries, traversal/routing, BEP44 storage, CLI lifetimes, test quality, and wire/getput behavior. Four reviewers returned final reports; partial server/store findings were checked directly. Findings were checked sequentially against contracts and targeted reproductions rather than applied on recommendation alone.
Confirmed corrections
v; a legitimate bencoded empty string remains valid.getput.Putnow returns an error when no token-eligible node accepts the write, preserving remote protocol errors. A successful remote write remains success.ping-nodes, report KRPC errors in ping output, and prevent automatic sequence wrap at MaxInt64.Test-quality corrections
TestHooknow uses ephemeral ports; it passed with the old hard-coded UDP port deliberately occupied. Removed the invalid nil-IP compact-address success expectation.Evidence on this production tree
GOMAXPROCS=1 go test -race -shuffle=on -count=5 -timeout 120s ./...passed; the corresponding initial run reproduced the 120-second LoadStats test timeout.go test -race -shuffle=on -count=2 -timeout 120s ./...passed. Later test-only cleanup was checked with focused repeated race runs.go vet ./...passed; golangci-lint reports 0 issues;git diff --checkpassed.76452a2with the local DHT replacement passedGOWORK=off go build ./...andGOWORK=off go test -short -count=1 -timeout 120s . ./tracker/...again.The proposed stale-Stalled race did not reproduce in repeated bounded tests; no signaling rewrite or ineffective test was kept. This is not a proof that every possible schedule is safe. The synchronous
StartingNodescallback still cannot be forcibly interrupted by a context; a blocking callback can delay bootstrap cancellation. Explicit CAS-zero presence remains the separately documented public-API limitation.Research used Codegraph and Firecrawl Developer Index, with primary contracts checked in BEP 32, BEP 44,
rate.WaitN, and Go resolver/Ed25519 documentation.Bug fixes
announce_peer,putandgetqueries without ana(arguments) dict caused a nil pointer dereference, so any single UDP packet could take down the server. These queries now get a KRPC protocol error, asget_peersandfind_nodealready did.find_nodeandgetreplies returned the nodes closest to the unsetinfo_hashinstead of the queriedtarget.puts were rejected for lackingseq, which only mutable items carry.seqis now required only for mutable puts.Server.Putignored itsQueryRateLimitingargument.BootstrapContextleaked a running traversal goroutine whenTraversalStartingNodesfailed.BootstrapContext,AnnounceTraversalandexts/getputnow get starting nodes before starting a traversal.refreshBucketread traversal stats in its return statement, which Go evaluates before deferred functions run, soOperation.Statswas called beforeStopped. The stats are now assigned after the traversal stops.Announce.TraversalStatscopied the counters with a plain struct read while queries update them with atomic adds.LoadStatsreads both fields atomically, and the announce, bootstrap, and get/put snapshots use it.getPeerswaited ontraversal.Stoppedwhile that query was still outstanding, soClosewith nobody readingPeersnever reachedFinished. The send now also selects on the query context, whichStopcancels.BootstrapContextreturned while itsfind_nodequeries kept running until they timed out. Bootstrap and bucket-refresh queries now use the traversal's context, andBootstrapContextwaits for the traversal to stop before returning its stats.exts/getput: fixed a nilseqdereference on unsigned responses, stopped ignoring errors fromTraversalStartingNodes, and now waits for the traversal to stop before reading its stats.krpc.Errordecoding no longer usesrecoverto reject malformed errors, andNodeInfodecoding checks its input length.Server.Closedid not cancel an in-flight query, soPingwaited outQueryResendDelayafter the server closed.Querynow ends when the server context is cancelled or the caller's context ends.table.closestNodesnow ranks eligible nodes from all routing buckets by XOR distance to the requested target, then truncates to K. Buckets are relative to the local node, not the target; sorting a bucket prefix was insufficient. Deterministic regressions cover omitted higher-index buckets, filtering, and nonpositive limits.Operation.Closestreturned the live set. A response could replace it while a caller ranged it. It now returns a snapshot taken under the operation lock.SetIPBlockListwrote the list unders.muwhileIPBlocklistandTraversalNodeFilterread it with no lock. The list is published with an atomic pointer, nots.mu, becausewriteToNodealready holds that lock.casis compared with the stored sequence, before equal/stale sequence handling, so a mismatch returns 301 rather than succeeding or returning 302.Wrapper.Putserializes read/check/write; concurrent CAS tests assert one accepted update and the stored winner.Memory.Putcomputes immutable targets before taking its mutex, permitting a value marshaler to callMemory.Get. Explicitcas=0presence remains an existing API limitation; see boundaries below.cmd/dht pingdereferencedSenderID()when a reply had no id.dht-serverreturned a table-load error withoutServer.Close, and the deferred socket close panickedserveUntilClosed.Regression tests cover argument-less queries, immutable puts without
seq, thefind_nodetarget, the traversal goroutine leaks, and bootstrap cancellation.TestLoadStatsDuringUpdateswaits until the writer has incremented, then races atomic updates againstLoadStats.TestAnnounceCloseWithoutReadingPeersanswers oneget_peers, does not readPeers, and requiresFinishedafterClose. With the old select it failed:Finisheddid not fire.TestRefreshBucketStopsBeforeStatsonly checks that the refresh traversal is finished whenrefreshBucketreturns; it does not prove the stats were unread beforeStopped. There are also newint160tests.Modernization
go 1.26.0, with dependencies upgraded to their latest releases.anacrolix/multiless,bradfitz/iter,docopt-goandgolang.org/x/exp, replacing them withcmp,slices,maps,math/rand/v2,errors.AsType,sync.OnceValue,WaitGroup.Go,atomic.Uint64,binary.Append*and range over int.handleQueryinto one handler per query method.int160.BitLenusesmath/bitsinstead ofbig.Int, and the Castagnoli CRC table is cached for the node ID security checks._test.gofiles.CI
go vet, the race detector and a one-pass benchmark run..golangci.yml), taking its Go version from go.mod.errcheckis not excluded for tests; the few ignored errors indht_test.goare now checked.Follow-through on the concurrency audit
OnQuery, Logger, PeerStore, Store, Ranger, response marshalers, and status writers no longer run under the server mutex on the exercised paths. Table updates remain locked, mutation/veto behavior is preserved, and handlers recheck closure after user callbacks. Query metrics snapshot values before their goroutine starts, avoiding a race withOnQuerymutations.dht-serverjoins bootstrap before persisting the routing table.TestBadGetPeersResponsedeliberately retains the missing response type required by PR #19.Advisor follow-through (
651c25d)CI run 35850583783 passed on this exact commit: Go 1.26, stable, and lint.
get_peersresponder has a read deadline and bounded goroutine-exit cleanup. The intentionally missing response type from PR Don't announce peer if the sender ID is nil #19 remains unchanged.go test -race -shuffle=on -count=2 -timeout 120s ./..., native and Windows vet, and golangci-lint (0 issues) passed on this test-only follow-up.Earlier verification
Earlier tree committed as
6bccb09:GitHub Actions run 35847431455 completed successfully on that exact SHA:
test (1.26),test (stable), andlint.go test -race -shuffle=on -count=2 -timeout 120s ./...passed.go vet ./...andGOOS=windows go vet ./...passed.golangci-lint run ./...: 0 issues.-race; bootstrap join passed five repeated runs. CLI--helpandderive-put-target immutable --string hellowere exercised; the latter matched SHA-1 of bencodedhello.a28a2e2exposed shared-rate-limiter interference in an older bootstrap test. An exhausted-limiter reproduction failed with the same nil-error result. The isolated, packet-synchronized fixture then passed ten race runs; the temporary reproduction was removed.Downstream verification was rerun against the production changes in
a28a2e2(the final commit changes test fixtures only): torrent76452a2, with a local module replacement, passedGOWORK=off go build ./...andGOWORK=off go test -count=1 -short -timeout 120s . ./tracker/....The
apidiffcomparison remains from1135f58againsta669bdd, not the current head. It reported removal ofOrderingfromcmd/dht(package main). No new API-diff claim is made. The full torrent suite was not run; the older downstream vet attempt stopped on torrent's unkeyed composite literals.Contract boundaries
int64; the wire field usesomitempty. Explicitcas=0cannot currently be distinguished from omission. This is a representation limitation, not a BEP 44 rule. The PR preserves existing public API types rather than claiming complete zero-CAS support.Memory.Put→ value marshaler →Memory.Get; it does not promise arbitrary reentry into the serializingWrapperfrom its Store/marshaler callbacks.Primary contracts checked with Codegraph, Firecrawl Developer Index, and the specifications: BEP 5, BEP 44, and Go's
context.AfterFuncdocumentation.