Skip to content

Commit

Permalink
fix: hanging goroutine in get fileArchive handler
Browse files Browse the repository at this point in the history
Fixes #8957

The context was only checked while reading data.
Not while writing data to the http connection.
So since the data flow through an io.Pipe the closing didn't flowed through and left the writer open hanging.

Co-authored-by: Antonio Navarro Perez <antnavper@gmail.com>
  • Loading branch information
Jorropo and ajnavarro committed May 13, 2022
1 parent 53299e8 commit 7892cc9
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions core/commands/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ may also specify the level of compression by specifying '-l=<1-9>'.
return err
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx := req.Context
cmplvl, err := getCompressOptions(req)
if err != nil {
return err
Expand All @@ -73,7 +74,7 @@ may also specify the level of compression by specifying '-l=<1-9>'.

p := path.New(req.Arguments[0])

file, err := api.Unixfs().Get(req.Context, p)
file, err := api.Unixfs().Get(ctx, p)
if err != nil {
return err
}
Expand All @@ -90,6 +91,13 @@ may also specify the level of compression by specifying '-l=<1-9>'.
if err != nil {
return err
}
go func() {
// We cannot defer a close in the response writer (like we should)
// Because the cmd framework outsmart us and doesn't call response
// if the context is over.
<-ctx.Done()
reader.Close()
}()

return res.Emit(reader)
},
Expand Down Expand Up @@ -273,7 +281,7 @@ func (i *identityWriteCloser) Close() error {
return nil
}

func fileArchive(f files.Node, name string, archive bool, compression int) (io.Reader, error) {
func fileArchive(f files.Node, name string, archive bool, compression int) (io.ReadCloser, error) {
cleaned := gopath.Clean(name)
_, filename := gopath.Split(cleaned)

Expand Down

0 comments on commit 7892cc9

Please sign in to comment.