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

Open Beginner friendly
#11,477 0 comments 0 reactions 0 assignees View on GitHub

@amalsaidov is already working on this.

Since Nov 13, 2026.

  • #11493 by @amalsaidov — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
backend, databases

Research direction

Start in core/node/provider.go, in SweepingProviderOpt, where the ResettableKeystore is created before the nil-DHT check that returns NoopProvider. Then look at the initKeystore and ensureProviderClosesBeforeKeystore lifecycle hooks, which skip NoopProvider. Run the reproduction test, which opens and closes the same repo twice in one process, and make it pass. Done means that test and go test ./core/node/ both pass, with the keystore closed on the nil-DHT path.

Written by the indexing model from the issue text.

Description

AI disclosure: this bug was found while analyzing a package that is not yet published on pub.dev, and the analysis, the reproduction and the proposed fix below were made entirely with AI (Claude). The author wants everyone who reads this issue (and any PR that follows) to know that, and acknowledges that, despite an extensive battery of tests, AI can still make mistakes.

Checklist
  • This is a bug report, not a question.
  • I have searched the issue tracker for my bug.
  • I am running the latest kubo version (v0.43.1; the same code is on master, 833e04a240d8).
Installation method

Built from source, kubo used as a library (core.NewNode), Go 1.26.5, linux/amd64 and windows/amd64.

Config

config.Init + the test profile, node built with Routing: libp2p.NilRouterOption (the same as Routing.Type none). Everything else default.

Description

With routing none, closing a node leaves the provider keystore datastore (<repo>/provider-keystore/<n>) open. Opening the same repo again in the same process then fails on its lock:

  • Windows: constructing the node (see log for full detail): failed to create datastore for namespace 0: The process cannot access the file because it is being used by another process.
  • Linux: ... resource temporarily unavailable

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

Reproduction:

package repro

import (
	"context"
	"io"
	"os"
	"testing"

	"github.com/ipfs/kubo/config"
	"github.com/ipfs/kubo/core"
	"github.com/ipfs/kubo/core/node/libp2p"
	"github.com/ipfs/kubo/plugin/loader"
	"github.com/ipfs/kubo/repo/fsrepo"
)

func TestNilRoutingReopen(t *testing.T) {
	pl, _ := loader.NewPluginLoader("")
	pl.Initialize()
	pl.Inject()

	ctx := context.Background()
	dir, _ := os.MkdirTemp("", "repro")
	cfg, _ := config.Init(io.Discard, 2048)
	config.Profiles["test"].Transform(cfg)
	if err := fsrepo.Init(dir, cfg); err != nil {
		t.Fatal(err)
	}
	start := func() error {
		r, err := fsrepo.Open(dir)
		if err != nil {
			return err
		}
		n, err := core.NewNode(ctx, &core.BuildCfg{Online: true, Repo: r, Routing: libp2p.NilRouterOption})
		if err != nil {
			r.Close()
			return err
		}
		return n.Close()
	}
	if err := start(); err != nil {
		t.Fatal(err)
	}
	if err := start(); err != nil {
		t.Fatal(err) // fails here
	}
}

Cause: in SweepingProviderOpt (core/node/provider.go) the fx.Provide creates the ResettableKeystore, which opens the on-disk datastore, before it knows whether there is a DHT to provide with. With no DHT (impl == nil) it returns &NoopProvider{}, nil, nil, dropping the keystore without closing it, and both lifecycle hooks that would close it (initKeystore and ensureProviderClosesBeforeKeystore) return early for *NoopProvider.

Proposed fix, which we have been running as a local patch (the reproduction passes, and go test ./core/node/ still passes):

 		if impl == nil {
+			// No DHT to provide with: the keystore opened above goes unused,
+			// and the shutdown hooks skip the NoopProvider, so close it here.
+			if err := ks.Close(); err != nil {
+				return nil, nil, err
+			}
 			return &NoopProvider{}, nil, nil
 		}

If this looks right, we can open a PR with a regression test.

Dominant language
Go
Stars
17.2k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ipfs/kubo

All issues in ipfs/kubo

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.