Repository navigation
refactor: remove lifecycle context from constructors - #1282
Merged
Merged
Conversation
The repo-wide migration (#1280) merged into this branch left the records package on math/rand: the GetProviders shuffle default and the seeded shuffle test. The global rand.Shuffle is a drop-in in v2; the test seeds a PCG instead of the removed rand.NewSource.
dht.Close does not await in-flight RPC handlers (the libp2p host owns them), so an ADD_PROVIDER or GET_PROVIDERS handler could reach the datastore after ProviderManager.Close returned and race a datastore shutdown. Every datastore access in AddProvider and GetProviders already runs under mu, so Close now sets a stopped flag under mu once GC exits: after it returns nothing touches the datastore, and late calls return the new ErrClosed. - providers_manager.go: stopped flag, exported ErrClosed, checks in AddProvider and GetProviders, Close godoc with the new guarantee - providers_manager_test.go: TestCloseFencesDatastoreAccess pins the fence; TestProviderGCUnderConcurrentWrites stops GC by cancelling the construction context, since its post-Close re-provides are now fenced
- value_store.go: validation runs on every read, including serving GET_VALUE, and what that costs and buys; Close does not fence Get/Put from the backing datastore - dht.go, fullrt/dht.go: the exported Validator fields must not be reassigned after construction, since the value store captures the validator (the net/http fields contract) - dht.go: Close documents shutdown ordering for consumers (host before datastore) instead of the stale goprocess reference - dht_options.go: ProviderDatastore spells out that custom ProviderStore implementations cannot be injected and how to get equivalent behavior - handlers.go: why handleGetValue validates before serving
gammazero
approved these changes
Jul 17, 2026
gammazero
left a comment
Contributor
There was a problem hiding this comment.
and there was much rejoicing! 🎉
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.
Closes #1238
Built on top of #1277
Summary
Removes the caller-supplied lifecycle context from all constructors and makes
Closethe single, blocking shutdown authority.The old
New(ctx, ...)semantics were ambiguous: nothing in the signature said the context bounded the DHT's lifetime rather than construction, and it created two competing shutdown paths — cancelling the context killed goroutines with no way to wait for them, whileClosewas the documented path. In practice callers passedcontext.Background()and usedCloseanyway. Thisfollows the same migration go-libp2p made for its constructors.
Breaking API changes
New,NewDHT,NewDHTClient,dual.New,records.NewProviderManager, andrtrefresh.NewRtRefreshManagerlose theircontext.Contextparameter.IpfsDHT.Context()is deprecated. It still returns the internal lifetime context (cancelled byClose), but cancellation never signalled that shutdown had completed — useClose, which now blocks until it has.context.Background().Migration: drop the context argument. If you cancelled that context to shut the DHT down, call
Close()instead.Behavior changes
Closenow blocks until all long-lived components have stopped: the background loops (tracked by aWaitGroup), the routing table refresh manager, and the provider/value stores, whose close errors are joined.Newcleans up after itself: any failure after background components have started tears them down via the deferredClose, so a failed constructor no longer leaks goroutines.dual.Newcloses the already-running WAN DHT when LAN construction fails;fullrt.NewFullRTcloses its event-bus subscription on the provider-manager error path.provider/dual'sWithAddLocalRecordcallback andSearchValue's backgroundupdatePeerValuesno longer go through the deprecatedContext()accessor. Both bound purely local or lifetime-scoped work; the provider store fences post-close writes withErrClosed.