Skip to content

fix(provider): close keystore when no provider takes ownership - #11493

Open
amalsaidov wants to merge 1 commit into
ipfs:masterfrom
amalsaidov:fix/provider-keystore-leak-routing-none
Open

amalsaidov wants to merge 1 commit into
ipfs:masterfrom
amalsaidov:fix/provider-keystore-leak-routing-none

Conversation

@amalsaidov

Copy link
Copy Markdown

What changed

With Routing.Type=none (or a node built with the nil router) Kubo builds a NoopProvider, so no provider ever takes ownership of the DHT provider keystore. NewResettableKeystore still runs before that decision and, in factory mode, eagerly opens the active namespace datastore under <repo>/provider-keystore/. The constructor then returns NoopProvider with a nil keystore and the opened datastore is dropped on the floor: initKeystore and ensureProviderClosesBeforeKeystore both skip NoopProvider, so nothing closes it.

The leaked datastore keeps its lock for the life of the process. Reopening the same repo in the same process then fails:

  • Linux: failed to create datastore for namespace 0: ... resource temporarily unavailable
  • Windows: ... The process cannot access the file because it is being used by another process.

The ipfs daemon CLI does not notice because the process exits and the OS releases the lock, but anything that embeds Kubo and restarts a node in-process (tests, apps) does.

Fix

Close the keystore on every path out of the provider constructor that does not hand it to a provider. A single deferred close guarded by a keystoreHandedOff flag covers the NoopProvider case and the provider-construction error paths; the flag is set right before the two returns that pass the keystore to a buffered.New(...) provider. ResettableKeystore.Close is idempotent and only persists/syncs size and closes the datastores it owns (it does not delete data or touch the caller-owned repo datastore), so closing an unused keystore is safe.

Test

core.TestNilRoutingKeystoreReopen inits an on-disk (pebbleds) repo, builds and closes a node with Routing: none, then builds it again in the same process. An in-memory datastore takes no lock, so the test uses an on-disk one and also asserts provider-keystore/ was created. The test fails before the fix (failed to create datastore for namespace 0: ... lock held by current process) and passes after.

$ go test ./core/ -run TestNilRoutingKeystoreReopen -v
=== RUN   TestNilRoutingKeystoreReopen
--- PASS: TestNilRoutingKeystoreReopen (0.37s)
PASS

go vet ./core/... ./core/node/ and go test ./core/node/ are clean. golangci-lint reports no issues in the changed files (the only failure is a pre-existing stylecheck warning in fuse/node/mount_darwin.go, unrelated to this change).

AI-assisted; directed, reviewed and tested by the author (amalsaidov).

References

With Routing.Type=none the provider is a NoopProvider and the keystore is
never handed off, but NewResettableKeystore already opened its datastore
under <repo>/provider-keystore/. Nothing closed it, so the datastore kept
its lock and reopening the same repo in the same process failed (Linux:
"resource temporarily unavailable"; Windows: "being used by another
process"). Software that embeds Kubo and restarts a node in-process hit
this; the daemon CLI did not, because process exit released the lock.

Close the keystore on every path that does not pass it to a provider,
covering the NoopProvider case and provider construction errors.
@amalsaidov
amalsaidov requested a review from a team as a code owner November 13, 2026 01:25
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.

Routing none: provider keystore datastore is never closed, so reopening the repo in the same process fails

1 participant