Skip to content

Commit b10e26a

Browse files
committedJul 29, 2015
cancel contexts when client disconnects
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 6496011 commit b10e26a

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed
 

‎commands/http/handler.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
cors "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/rs/cors"
13+
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
1314

1415
cmds "github.com/ipfs/go-ipfs/commands"
1516
u "github.com/ipfs/go-ipfs/util"
@@ -150,7 +151,11 @@ func (i internalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
150151

151152
//ps: take note of the name clash - commands.Context != context.Context
152153
req.SetInvocContext(i.ctx)
153-
err = req.SetRootContext(node.Context())
154+
155+
ctx, cancel := context.WithCancel(node.Context())
156+
defer cancel()
157+
158+
err = req.SetRootContext(ctx)
154159
if err != nil {
155160
http.Error(w, err.Error(), http.StatusInternalServerError)
156161
return
@@ -246,7 +251,12 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req
246251
}
247252

248253
if err := writeResponse(status, w, out); err != nil {
249-
log.Error("error while writing stream", err)
254+
if strings.Contains(err.Error(), "broken pipe") {
255+
log.Info("client disconnect while writing stream ", err)
256+
return
257+
}
258+
259+
log.Error("error while writing stream ", err)
250260
}
251261
}
252262

0 commit comments

Comments
 (0)
Please sign in to comment.