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

copy context before starting stash operations to prevent ctx cancellations #1790

Merged
merged 2 commits into from
Sep 15, 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
14 changes: 14 additions & 0 deletions pkg/download/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (
"regexp"
"strings"
"sync"
"time"

"github.com/gomods/athens/pkg/download/mode"
"github.com/gomods/athens/pkg/errors"
"github.com/gomods/athens/pkg/log"
"github.com/gomods/athens/pkg/module"
"github.com/gomods/athens/pkg/observ"
"github.com/gomods/athens/pkg/requestid"
"github.com/gomods/athens/pkg/stash"
"github.com/gomods/athens/pkg/storage"
)
Expand Down Expand Up @@ -248,6 +251,10 @@ func (p *protocol) Zip(ctx context.Context, mod, ver string) (storage.SizeReadCl

func (p *protocol) processDownload(ctx context.Context, mod, ver string, f func(newVer string) error) error {
const op errors.Op = "protocol.processDownload"
// Create a new context with custom deadline and ditch whatever deadline was passed by the caller.
// This is needed so that the async go routines can continue even after the HTTP request is complete (which leads to context cancellation).
ctx, cancel := copyContextWithCustomTimeout(ctx, time.Minute*15)
defer cancel()
switch p.df.Match(mod) {
case mode.Sync:
newVer, err := p.stasher.Stash(ctx, mod, ver)
Expand Down Expand Up @@ -288,3 +295,10 @@ func union(list1, list2 []string) []string {
}
return unique
}

func copyContextWithCustomTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
ctxCopy, cancel := context.WithTimeout(context.Background(), timeout)
requestid.SetInContext(ctxCopy, requestid.FromContext(ctx))
log.SetEntryInContext(ctxCopy, log.EntryFromContext(ctx))
return ctxCopy, cancel
}