From e332af8ed509a9485ebc506544d9f39ea4cd5fd5 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 30 Oct 2018 06:43:16 -0700 Subject: [PATCH] commands: fix a bunch of tiny commands-lib issues * Always check errors returned by emit. Otherwise, we may not notice when the client goes away. * Make sure to use EmitOnce instead of Emit when appropriate. Otherwise, we break javascript. (thanks Magik6k for finding this before we cut the release...) License: MIT Signed-off-by: Steven Allen --- core/commands/dag/dag.go | 4 ++-- core/commands/dns.go | 2 +- core/commands/pubsub.go | 6 ++++-- core/commands/stat.go | 14 ++++++++++---- core/commands/tar.go | 2 +- core/commands/version.go | 2 +- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/core/commands/dag/dag.go b/core/commands/dag/dag.go index 7f3b970be24..a78f67c065b 100644 --- a/core/commands/dag/dag.go +++ b/core/commands/dag/dag.go @@ -188,7 +188,7 @@ format. } out = final } - return res.Emit(&out) + return cmds.EmitOnce(res, &out) }, } @@ -219,7 +219,7 @@ var DagResolveCmd = &cmds.Command{ return err } - return res.Emit(&ResolveOutput{ + return cmds.EmitOnce(res, &ResolveOutput{ Cid: lastCid, RemPath: path.Join(rem), }) diff --git a/core/commands/dns.go b/core/commands/dns.go index f96ce9f57ed..ffc2dd159d3 100644 --- a/core/commands/dns.go +++ b/core/commands/dns.go @@ -77,7 +77,7 @@ The resolver can recursively resolve: if err != nil { return err } - return res.Emit(&ncmd.ResolvedPath{Path: output}) + return cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: output}) }, Encoders: cmds.EncoderMap{ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ncmd.ResolvedPath) error { diff --git a/core/commands/pubsub.go b/core/commands/pubsub.go index 1d49704b9d9..05a28ba61fd 100644 --- a/core/commands/pubsub.go +++ b/core/commands/pubsub.go @@ -105,12 +105,14 @@ This command outputs data in the following encodings: return err } - res.Emit(&pubsubMessage{ + if err := res.Emit(&pubsubMessage{ Data: msg.Data(), From: []byte(msg.From()), Seqno: msg.Seq(), TopicIDs: msg.Topics(), - }) + }); err != nil { + return err + } } }, Encoders: cmds.EncoderMap{ diff --git a/core/commands/stat.go b/core/commands/stat.go index 10c8d67dcd6..574e0c15d3d 100644 --- a/core/commands/stat.go +++ b/core/commands/stat.go @@ -127,14 +127,20 @@ Example: for { if pfound { stats := nd.Reporter.GetBandwidthForPeer(pid) - res.Emit(&stats) + if err := res.Emit(&stats); err != nil { + return err + } } else if tfound { protoId := protocol.ID(tstr) stats := nd.Reporter.GetBandwidthForProtocol(protoId) - res.Emit(&stats) + if err := res.Emit(&stats); err != nil { + return err + } } else { totals := nd.Reporter.GetBandwidthTotals() - res.Emit(&totals) + if err := res.Emit(&totals); err != nil { + return err + } } if !doPoll { return nil @@ -142,7 +148,7 @@ Example: select { case <-time.After(interval): case <-req.Context.Done(): - return nil + return req.Context.Err() } } }, diff --git a/core/commands/tar.go b/core/commands/tar.go index edb37dd9a95..b13e059fca4 100644 --- a/core/commands/tar.go +++ b/core/commands/tar.go @@ -57,7 +57,7 @@ represent it. c := node.Cid() fi.FileName() - return res.Emit(&coreiface.AddEvent{ + return cmds.EmitOnce(res, &coreiface.AddEvent{ Name: fi.FileName(), Hash: c.String(), }) diff --git a/core/commands/version.go b/core/commands/version.go index f6efd8657ff..07ff380be99 100644 --- a/core/commands/version.go +++ b/core/commands/version.go @@ -40,7 +40,7 @@ var VersionCmd = &cmds.Command{ cmdkit.BoolOption(versionAllOptionName, "Show all version information"), }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { - return res.Emit(&VersionOutput{ + return cmds.EmitOnce(res, &VersionOutput{ Version: version.CurrentVersionNumber, Commit: version.CurrentCommit, Repo: fmt.Sprint(fsrepo.RepoVersion),