1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-26 23:53:19 +08:00

fix panic caused by accessing config after repo closed

License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
Jeromy
2015-09-03 09:28:36 -07:00
parent 3fc1854d24
commit ab0c668ab8
14 changed files with 84 additions and 41 deletions

View File

@ -204,7 +204,13 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
return
}
if routingOption == routingOptionSupernodeKwd {
servers, err := repo.Config().SupernodeRouting.ServerIPFSAddrs()
rcfg, err := repo.Config()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
servers, err := rcfg.SupernodeRouting.ServerIPFSAddrs()
if err != nil {
res.SetError(err, cmds.ErrNormal)
repo.Close() // because ownership hasn't been transferred to the node

View File

@ -192,7 +192,7 @@ func cmdCtx(node *core.IpfsNode, repoPath string) commands.Context {
Online: true,
ConfigRoot: repoPath,
LoadConfig: func(path string) (*config.Config, error) {
return node.Repo.Config(), nil
return node.Repo.Config()
},
ConstructNode: func() (*core.IpfsNode, error) {
return node, nil

View File

@ -135,7 +135,11 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
}
if cfg.Online {
do := setupDiscoveryOption(n.Repo.Config().Discovery)
rcfg, err := n.Repo.Config()
if err != nil {
return err
}
do := setupDiscoveryOption(rcfg.Discovery)
if err := n.startOnlineServices(ctx, cfg.Routing, cfg.Host, do); err != nil {
return err
}

View File

@ -72,7 +72,11 @@ in the bootstrap list).
return
}
defer r.Close()
cfg := r.Config()
cfg, err := r.Config()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
deflt, _, err := req.Option("default").Bool()
if err != nil {
@ -148,7 +152,11 @@ var bootstrapRemoveCmd = &cmds.Command{
return
}
defer r.Close()
cfg := r.Config()
cfg, err := r.Config()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
all, _, err := req.Option("all").Bool()
if err != nil {
@ -197,7 +205,11 @@ var bootstrapListCmd = &cmds.Command{
return
}
defer r.Close()
cfg := r.Config()
cfg, err := r.Config()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
peers, err := cfg.BootstrapPeers()
if err != nil {

View File

@ -136,7 +136,10 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
n.Reporter = metrics.NewBandwidthCounter()
// get undialable addrs from config
cfg := n.Repo.Config()
cfg, err := n.Repo.Config()
if err != nil {
return err
}
var addrfilter []*net.IPNet
for _, s := range cfg.Swarm.AddrFilters {
f, err := mamask.NewMask(s)
@ -156,7 +159,7 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
}
// Ok, now we're ready to listen.
if err := startListening(ctx, n.PeerHost, n.Repo.Config()); err != nil {
if err := startListening(ctx, n.PeerHost, cfg); err != nil {
return err
}
@ -325,7 +328,7 @@ func (n *IpfsNode) Bootstrap(cfg BootstrapConfig) error {
cfg.BootstrapPeers = func() []peer.PeerInfo {
ps, err := n.loadBootstrapPeers()
if err != nil {
log.Warningf("failed to parse bootstrap peers from config: %s", n.Repo.Config().Bootstrap)
log.Warning("failed to parse bootstrap peers from config")
return nil
}
return ps
@ -342,7 +345,12 @@ func (n *IpfsNode) loadID() error {
return errors.New("identity already loaded")
}
cid := n.Repo.Config().Identity.PeerID
cfg, err := n.Repo.Config()
if err != nil {
return err
}
cid := cfg.Identity.PeerID
if cid == "" {
return errors.New("Identity was not set in config (was ipfs init run?)")
}
@ -363,7 +371,12 @@ func (n *IpfsNode) LoadPrivateKey() error {
return errors.New("private key already loaded")
}
sk, err := loadPrivateKey(&n.Repo.Config().Identity, n.Identity)
cfg, err := n.Repo.Config()
if err != nil {
return err
}
sk, err := loadPrivateKey(&cfg.Identity, n.Identity)
if err != nil {
return err
}
@ -375,7 +388,12 @@ func (n *IpfsNode) LoadPrivateKey() error {
}
func (n *IpfsNode) loadBootstrapPeers() ([]peer.PeerInfo, error) {
parsed, err := n.Repo.Config().BootstrapPeers()
cfg, err := n.Repo.Config()
if err != nil {
return nil, err
}
parsed, err := cfg.BootstrapPeers()
if err != nil {
return nil, err
}

View File

@ -107,8 +107,12 @@ func commandsOption(cctx commands.Context, command *commands.Command) ServeOptio
AllowedMethods: []string{"GET", "POST", "PUT"},
},
}
rcfg, err := n.Repo.Config()
if err != nil {
return nil, err
}
addHeadersFromConfig(cfg, n.Repo.Config())
addHeadersFromConfig(cfg, rcfg)
addCORSFromEnv(cfg)
addCORSDefaults(cfg)
patchCORSVars(cfg, l.Addr())

View File

@ -30,7 +30,12 @@ func NewGateway(conf GatewayConfig) *Gateway {
func (g *Gateway) ServeOption() ServeOption {
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
// pass user's HTTP headers
g.Config.Headers = n.Repo.Config().Gateway.HTTPHeaders
cfg, err := n.Repo.Config()
if err != nil {
return nil, err
}
g.Config.Headers = cfg.Gateway.HTTPHeaders
gateway, err := newGatewayHandler(n, g.Config)
if err != nil {

View File

@ -11,7 +11,11 @@ import (
// Mount mounts ipns at a given location, and returns a mount.Mount instance.
func Mount(ipfs *core.IpfsNode, ipnsmp, ipfsmp string) (mount.Mount, error) {
cfg := ipfs.Repo.Config()
cfg, err := ipfs.Repo.Config()
if err != nil {
return nil, err
}
allow_other := cfg.Mounts.FuseAllowOther
if ipfs.IpnsFs == nil {

View File

@ -10,7 +10,10 @@ import (
// Mount mounts ipfs at a given location, and returns a mount.Mount instance.
func Mount(ipfs *core.IpfsNode, mountpoint string) (mount.Mount, error) {
cfg := ipfs.Repo.Config()
cfg, err := ipfs.Repo.Config()
if err != nil {
return nil, err
}
allow_other := cfg.Mounts.FuseAllowOther
fsys := NewFileSystem(ipfs)
return mount.NewMount(ipfs.Process(), fsys, mountpoint, allow_other)

View File

@ -445,11 +445,8 @@ func (r *FSRepo) Close() error {
return nil
}
// Config returns the FSRepo's config. This method must not be called if the
// repo is not open.
//
// Result when not Open is undefined. The method may panic if it pleases.
func (r *FSRepo) Config() *config.Config {
func (r *FSRepo) Config() (*config.Config, error) {
// It is not necessary to hold the package lock since the repo is in an
// opened state. The package lock is _not_ meant to ensure that the repo is
@ -460,9 +457,9 @@ func (r *FSRepo) Config() *config.Config {
defer packageLock.Unlock()
if r.closed {
panic("repo is closed")
return nil, errors.New("cannot access config, repo not open")
}
return r.config
return r.config, nil
}
// setConfigUnsynced is for private use.

View File

@ -15,8 +15,8 @@ type Mock struct {
D ds.ThreadSafeDatastore
}
func (m *Mock) Config() *config.Config {
return &m.C // FIXME threadsafety
func (m *Mock) Config() (*config.Config, error) {
return &m.C, nil // FIXME threadsafety
}
func (m *Mock) SetConfig(updated *config.Config) error {

View File

@ -14,7 +14,7 @@ var (
)
type Repo interface {
Config() *config.Config
Config() (*config.Config, error)
SetConfig(*config.Config) error
SetConfigKey(key string, value interface{}) error

View File

@ -70,7 +70,11 @@ func run() error {
if err != nil { // owned by node
return err
}
cfg := repo.Config()
cfg, err := repo.Config()
if err != nil {
return err
}
cfg.Bootstrap = servers
if err := repo.SetConfig(cfg); err != nil {
return err
@ -236,7 +240,7 @@ func cmdCtx(node *core.IpfsNode, repoPath string) commands.Context {
Online: true,
ConfigRoot: repoPath,
LoadConfig: func(path string) (*config.Config, error) {
return node.Repo.Config(), nil
return node.Repo.Config()
},
ConstructNode: func() (*core.IpfsNode, error) {
return node, nil

View File

@ -1,14 +0,0 @@
package util
// FIXME: we need the go-random/random utility for our sharness test wich depends on go-humanize
// Godep will drop it if we dont use it in ipfs. There should be a better way to do this.
import _ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/dustin/go-humanize"
// similar to the above, only used in the tests makefile
import _ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/iptb"
import _ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/chriscool/go-sleep"
// imported by chegga/pb on windows, this is here so running godeps on non-windows doesnt
// drop it from our vendoring
import _ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/olekukonko/ts"