Skip to content

refactor: remove lifecycle context from constructors - #1282

Merged
guillaumemichel merged 28 commits into
masterfrom
refactor/remove-lifecycle-context
Jul 18, 2026
Merged

guillaumemichel merged 28 commits into
masterfrom
refactor/remove-lifecycle-context

Conversation

@guillaumemichel

Copy link
Copy Markdown
Member

Closes #1238

Built on top of #1277

Summary

Removes the caller-supplied lifecycle context from all constructors and makes Close the 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, while Close was the documented path. In practice callers passed context.Background() and used Close anyway. This
follows the same migration go-libp2p made for its constructors.

Breaking API changes

  • New, NewDHT, NewDHTClient, dual.New, records.NewProviderManager, and rtrefresh.NewRtRefreshManager lose their context.Context parameter.
  • IpfsDHT.Context() is deprecated. It still returns the internal lifetime context (cancelled by Close), but cancellation never signalled that shutdown had completed — use Close, which now blocks until it has.
  • Context values (tracing baggage, etc.) threaded through the old constructor context no longer reach internal background operations: the internal lifetime context now derives from context.Background().

Migration: drop the context argument. If you cancelled that context to shut the DHT down, call Close() instead.

Behavior changes

  • Close now blocks until all long-lived components have stopped: the background loops (tracked by a WaitGroup), the routing table refresh manager, and the provider/value stores, whose close errors are joined.
  • New cleans up after itself: any failure after background components have started tears them down via the deferred Close, so a failed constructor no longer leaks goroutines.
  • dual.New closes the already-running WAN DHT when LAN construction fails; fullrt.NewFullRT closes its event-bus subscription on the provider-manager error path.
  • provider/dual's WithAddLocalRecord callback and SearchValue's background updatePeerValues no longer go through the deprecated Context() accessor. Both bound purely local or lifetime-scoped work; the provider store fences post-close writes with ErrClosed.

guillaumemichel and others added 24 commits July 8, 2026 09:49
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 gammazero left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and there was much rejoicing! 🎉

Base automatically changed from refactor/records-storage to master July 18, 2026 07:37
@guillaumemichel
guillaumemichel merged commit 727bea0 into master Jul 18, 2026
9 checks passed
@gammazero
gammazero deleted the refactor/remove-lifecycle-context branch July 20, 2026 14:55
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.

Do not use context to shutdown dht

3 participants