Skip to content

Commit a45d3fe

Browse files
committed
fix: allow daemon to start correctly if the API is null
Fixes: #10056
1 parent b8c4725 commit a45d3fe

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

cmd/ipfs/daemon.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,11 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
714714
return nil, fmt.Errorf("serveHTTPApi: ConstructNode() failed: %s", err)
715715
}
716716

717-
if err := node.Repo.SetAPIAddr(rewriteMaddrToUseLocalhostIfItsAny(listeners[0].Multiaddr())); err != nil {
718-
return nil, fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %w", err)
717+
if len(listeners) > 0 {
718+
// Only add an api file if the API is running.
719+
if err := node.Repo.SetAPIAddr(rewriteMaddrToUseLocalhostIfItsAny(listeners[0].Multiaddr())); err != nil {
720+
return nil, fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %w", err)
721+
}
719722
}
720723

721724
errc := make(chan error)

test/cli/daemon_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cli
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ipfs/kubo/test/cli/harness"
7+
)
8+
9+
func TestDaemon(t *testing.T) {
10+
t.Parallel()
11+
12+
t.Run("daemon starts if api is set to null", func(t *testing.T) {
13+
t.Parallel()
14+
node := harness.NewT(t).NewNode().Init()
15+
node.SetIPFSConfig("API", nil)
16+
node.IPFS("daemon") // can't use .StartDaemon because it do a .WaitOnAPI
17+
})
18+
}

test/cli/harness/ipfs.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,20 @@ func (n *Node) SetIPFSConfig(key string, val interface{}, flags ...string) {
3838
n.IPFS(args...)
3939

4040
// validate the config was set correctly
41-
var newVal string
42-
n.GetIPFSConfig(key, &newVal)
43-
if val != newVal {
41+
42+
// Create a new value which is a pointer to the same type as the source.
43+
var newVal any
44+
if val != nil {
45+
// If it is not nil grab the type with reflect.
46+
newVal = reflect.New(reflect.TypeOf(val)).Interface()
47+
} else {
48+
// else just set a pointer to an any.
49+
var anything any
50+
newVal = &anything
51+
}
52+
n.GetIPFSConfig(key, newVal)
53+
// dereference newVal using reflect to load the resulting value
54+
if val != reflect.ValueOf(newVal).Elem().Interface() {
4455
log.Panicf("key '%s' did not retain value '%s' after it was set, got '%s'", key, val, newVal)
4556
}
4657
}

0 commit comments

Comments
 (0)