{"record":{"id":"9625a1e8a4511432","repo":"ipfs/kubo","slug":"error-can-t-ping-self","errorCode":null,"errorMessage":"error: can't ping self","messagePattern":"error: can't ping self","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/commands/ping.go","lineNumber":33,"sourceCode":"\tpstore \"github.com/libp2p/go-libp2p/core/peerstore\"\n\tping \"github.com/libp2p/go-libp2p/p2p/protocol/ping\"\n\tma \"github.com/multiformats/go-multiaddr\"\n)\n\nconst kPingTimeout = 10 * time.Second\n\ntype PingResult struct {\n\tSuccess bool\n\tTime    time.Duration\n\tText    string\n}\n\nconst (\n\tpingCountOptionName = \"count\"\n)\n\n// ErrPingSelf is returned when the user attempts to ping themself.\nvar ErrPingSelf = errors.New(\"error: can't ping self\")\n\nvar PingCmd = &cmds.Command{\n\tHelptext: cmds.HelpText{\n\t\tTagline: \"Send echo request packets to IPFS hosts.\",\n\t\tShortDescription: `\n'ipfs ping' is a tool to test sending data to other nodes. It finds nodes\nvia the routing system, sends pings, waits for pongs, and prints out round-\ntrip latency information.\n\t\t`,\n\t},\n\tArguments: []cmds.Argument{\n\t\tcmds.StringArg(\"peer ID\", true, true, \"ID of peer to be pinged.\").EnableStdin(),\n\t},\n\tOptions: []cmds.Option{\n\t\tcmds.IntOption(pingCountOptionName, \"n\", \"Number of ping messages to send.\").WithDefault(10),\n\t},\n\tRun: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {\n\t\tn, err := cmdenv.GetNode(env)","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/core/commands/ping.go#L15-L51","documentation":"`ErrPingSelf` is a sentinel error returned by the ping command when the target peer ID equals the local node's own identity (`n.Identity`). Pinging yourself is meaningless in the DHT/ping protocol, so kubo rejects it upfront. The doc comment explicitly marks it as the error for attempting to ping yourself.","triggerScenarios":"Running `ipfs ping <own-peer-id>` where the argument is the node's own ID from `ipfs id`; scripts that resolve a peer ID via DNSLink/multiaddr and accidentally resolve back to the local node; clusters/tests where a node list accidentally includes self.","commonSituations":"Copy-pasting the peer ID from `ipfs id` on the same machine you're testing from; loopback multiaddr pings (`ipfs ping /p2p/<self>`); automation iterating over a peer list that includes the local node without filtering.","solutions":["Ping a different peer: get a remote peer ID from `ipfs swarm peers` or `ipfs dht findprovidercmd` output and use that.","Compare the target with `ipfs id --format='<id>'` before pinging and skip when equal (self-check in scripts).","If you meant to test connectivity to yourself, use the daemon's own endpoints instead (e.g. `ipfs id`, API health) rather than `ipfs ping`.","In code, check for `ErrPingSelf` with `errors.Is(err, corecommands.ErrPingSelf)` and treat it as a user mistake, not a network failure."],"exampleFix":"// before (shell)\nSELF=$(ipfs id --format='<id>')\nipfs ping \"$SELF\"\n// after\nSELF=$(ipfs id --format='<id>')\nTARGET=\"$argv[1]\"\n[ \"$TARGET\" != \"$SELF\" ] && ipfs ping \"$TARGET\" || echo 'refusing to ping self'","handlingStrategy":"try-catch","validationCode":"target, _ := peer.Decode(args[0])\nself, _ := node.Identity.Decode()\nif target == self {\n    return fmt.Errorf(\"refusing to ping self\")\n}","typeGuard":"func isPingSelf(err error) bool {\n    return errors.Is(err, ErrPingSelf)\n}","tryCatchPattern":"pid, err := ipfsPing(ctx, target)\nif err != nil {\n    if errors.Is(err, ErrPingSelf) {\n        // user mistake: skip or report, do not retry\n        return fmt.Errorf(\"target %s is the local node; pick a remote peer\", target)\n    }\n    return err\n}","preventionTips":["Filter the local node's own peer ID out of any peer list before pinging","Resolve multiaddrs first and compare the decoded peer ID against `ipfs id`","Treat ErrPingSelf as a usage error in tooling, distinct from network timeouts","Never copy the peer ID from `ipfs id` when testing local connectivity"],"tags":["cli","ping","self-reference","validation"],"backgroundTag":"invalid-target-self","analyzedSha":"329838acdfafae224582930457efe80aa217afc0","analyzedAt":"2026-09-03T18:30:52.135Z","contentChangedAt":"2026-09-03T18:30:52.135Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}