ipfs / kubo

An IPFS implementation in Go
https://docs.ipfs.tech/how-to/command-line-quick-start/
Other
16.18k stars 3.01k forks source link

unix domain socket not removed if it exists #6856

Open jmgurney opened 4 years ago

jmgurney commented 4 years ago

When using a unix domain socket, config: "API": [ "/ip4/127.0.0.1/tcp/5001", "/unix/var/db/ipfs-go/socket/api.sock" ],

the first time it starts up, it works fine, but the second time it fails:

Error: serveHTTPApi: manet.Listen(/unix/var/db/ipfs-go/socket/api.sock) failed: listen unix /var/db/ipfs-go/socket/api.sock: bind: address already in use

It is common for programs to either use a lock file, or just simply remove it on start.

I've worked around the issue by removing it in my startup file, but a better solution that is more generic would be nice.

hsanjuan commented 4 years ago

I cannot reproduce by stopping ipfs normally. The file stays behind only when -KILLing ipfs.

Would you like to submit a patch to remove the unix socket on boot? Probably affects cmd/ipfs/daemon.go (serveHTTPApi).

djdv commented 4 years ago

(This snippet might be useful) https://github.com/djdv/go-ipfs/blob/6f22508188cc19132a08523488cc6d3e4f8047aa/plugin/plugins/filesystem/plugin.go#L127-L153

Edit: I've since modified this, looks like my most recent version is:

// maybeRemoveUnixSockets attempts to remove all Unix domain socket paths in a given multiaddr.
// It returns all errors encountered (wrapped), excluding all "not exist" errors.
func maybeRemoveUnixSockets(ma multiaddr.Multiaddr) (err error) {
    multiaddr.ForEach(ma, func(comp multiaddr.Component) bool {
        if comp.Protocol().Code == multiaddr.P_UNIX {
            localPath := comp.Value()
            if runtime.GOOS == "windows" { // `/C:\path` -> `C:\path`
                localPath = strings.TrimPrefix(localPath, `/`)
            }
            osErr := os.Remove(localPath)
            if osErr != nil && !os.IsNotExist(osErr) {
                err = maybeWrapErr(err, osErr)
            }
        }
        return false
    })
    return
}
NorseGaud commented 3 years ago

Hey @hsanjuan , what's a safe way to kill ipfs if I'm backgrounding the daemon? -15 doesn't seem to impact the running daemon.

Stebalien commented 3 years ago

@NorseGaud please ask on https://discuss.ipfs.io.