Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hanging goroutine in get fileArchive handler #8973

Merged
merged 1 commit into from
May 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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