Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core/node/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,26 @@ func SweepingProviderOpt(cfg *config.Config) fx.Option {
if err != nil {
return nil, nil, err
}

// NewResettableKeystore eagerly opens the active namespace datastore
// under <repo>/provider-keystore/ (factory mode). If no provider takes
// ownership of the keystore below, nothing closes that datastore:
// Routing.Type=none yields a NoopProvider with a nil keystore, and
// provider construction can also fail after this point. A leaked open
// datastore keeps its file lock, so reopening the same repo in the same
// process fails (Windows: "being used by another process"; Linux:
// "resource temporarily unavailable"). Close it on every path that does
// not hand it off to a provider.
keystoreHandedOff := false
defer func() {
if keystoreHandedOff {
return
}
if cerr := ks.Close(); cerr != nil {
providerLog.Warnw("closing unused provider keystore", "err", cerr)
}
}()

// Constants for buffered provider configuration
// These values match the upstream defaults from go-libp2p-kad-dht and have been battle-tested
const (
Expand Down Expand Up @@ -778,6 +798,7 @@ func SweepingProviderOpt(cfg *config.Config) fx.Option {
if err != nil {
return nil, nil, err
}
keystoreHandedOff = true
return buffered.New(prov, ds, bufferedProviderOpts...), ks, nil
}
case *fullrt.FullRT:
Expand Down Expand Up @@ -826,6 +847,7 @@ func SweepingProviderOpt(cfg *config.Config) fx.Option {
if err != nil {
return nil, nil, err
}
keystoreHandedOff = true
return buffered.New(prov, ds, bufferedProviderOpts...), ks, nil
})

Expand Down
89 changes: 89 additions & 0 deletions core/provider_keystore_reopen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package core_test

import (
"io"
"os"
"path/filepath"
"strings"
"testing"

"github.com/cockroachdb/pebble/v2"
"github.com/ipfs/kubo/config"
"github.com/ipfs/kubo/core"
"github.com/ipfs/kubo/core/node/libp2p"
"github.com/ipfs/kubo/plugin"
"github.com/ipfs/kubo/plugin/plugins/pebbleds"
"github.com/ipfs/kubo/repo/fsrepo"
"github.com/stretchr/testify/require"
)

// TestNilRoutingKeystoreReopen guards against a provider-keystore datastore
// leak. With Routing.Type=none the provider is a NoopProvider, but the keystore
// still eagerly opens its on-disk datastore under <repo>/provider-keystore/.
// If nothing closes that datastore, its file lock blocks reopening the same
// repo in the same process, which breaks anything that embeds kubo and restarts
// a node (tests, apps). The daemon CLI hides this because the process exits and
// the OS releases the lock.
func TestNilRoutingKeystoreReopen(t *testing.T) {
registerPebbleDatastore(t)

repoPath := t.TempDir()

cfg, err := config.Init(io.Discard, 2048)
require.NoError(t, err)
// Bind ephemeral localhost ports so the test does not collide with a
// daemon already listening on the default swarm port.
cfg.Addresses.Swarm = []string{"/ip4/127.0.0.1/tcp/0"}
cfg.Bootstrap = []string{}
// A single on-disk datastore (not a MapDatastore) is required: the leak
// is an unreleased datastore lock, and an in-memory datastore takes none.
cfg.Datastore.Spec = map[string]any{
"type": "pebbleds",
"prefix": "pebble.datastore",
"path": "pebbleds",
"formatMajorVersion": int(pebble.FormatNewest),
}

require.NoError(t, fsrepo.Init(repoPath, cfg))

startStop := func() {
r, err := fsrepo.Open(repoPath)
require.NoError(t, err)

node, err := core.NewNode(t.Context(), &core.BuildCfg{
Online: true,
Routing: libp2p.NilRouterOption,
Repo: r,
})
require.NoError(t, err)
require.NoError(t, node.Close())
}

// First run creates and opens the keystore datastore.
startStop()

// The keystore datastore must have been closed on the first run. Before the
// fix this second open fails with a lock error ("resource temporarily
// unavailable" on Unix, "being used by another process" on Windows).
startStop()

// Sanity check: the keystore directory was actually created on disk, so the
// test really exercised an on-disk (lockable) datastore rather than silently
// falling back to an in-memory one.
keystoreDir := filepath.Join(repoPath, "provider-keystore")
_, err = os.Stat(keystoreDir)
require.NoError(t, err, "provider-keystore should exist after a node ran")
}

// registerPebbleDatastore registers the pebbleds datastore handler with fsrepo.
// AddDatastoreConfigHandler errors if the handler is already registered, which
// is harmless here, so a duplicate registration is tolerated.
func registerPebbleDatastore(t *testing.T) {
t.Helper()
dsPlugin, ok := pebbleds.Plugins[0].(plugin.PluginDatastore)
require.True(t, ok, "pebbleds plugin should implement PluginDatastore")
err := fsrepo.AddDatastoreConfigHandler(dsPlugin.DatastoreTypeName(), dsPlugin.DatastoreConfigParser())
if err != nil && !strings.Contains(err.Error(), "already have a datastore") {
t.Fatalf("registering pebbleds datastore handler: %v", err)
}
}
4 changes: 4 additions & 0 deletions docs/changelogs/v0.44.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

### 🔦 Highlights

#### 🐛 Provider keystore no longer leaks when `Routing.Type=none`

When `Routing.Type` is `none` (or a node is built with the nil router), Kubo creates no DHT provider, but the provider keystore under `<repo>/provider-keystore/` still opened its datastore and never closed it. The leaked datastore kept its lock, so reopening the same repo in the same process failed (for example `resource temporarily unavailable` on Linux or `being used by another process` on Windows). This only affected software that embeds Kubo and restarts a node in-process (the `ipfs daemon` CLI was unaffected because the process exit released the lock). The keystore is now closed on every path that does not hand it off to a provider.

#### 📦️ Dependency updates

- update `go-libp2p` to [v0.50.0](https://github.com/libp2p/go-libp2p/releases/tag/v0.50.0)
Expand Down