diff --git a/app/api/controller/pullreq/comment_apply_suggestions.go b/app/api/controller/pullreq/comment_apply_suggestions.go index f1451ad2d..d62a097bd 100644 --- a/app/api/controller/pullreq/comment_apply_suggestions.go +++ b/app/api/controller/pullreq/comment_apply_suggestions.go @@ -280,10 +280,7 @@ func (c *Controller) CommentApplySuggestions( // TODO: This is a small change to reduce likelihood of dirty state (e.g. git work done but db canceled). // We still require a proper solution to handle an application crash or very slow execution times const timeout = 1 * time.Minute - ctx, cancel := context.WithTimeout( - contextutil.WithNewValues(context.Background(), ctx), - timeout, - ) + ctx, cancel := contextutil.WithNewTimeout(ctx, timeout) defer cancel() // Create internal write params. Note: This will skip the pre-commit protection rules check. diff --git a/app/api/controller/pullreq/merge.go b/app/api/controller/pullreq/merge.go index d700fab67..6d12f0728 100644 --- a/app/api/controller/pullreq/merge.go +++ b/app/api/controller/pullreq/merge.go @@ -237,10 +237,7 @@ func (c *Controller) Merge( // TODO: This is a small change to reduce likelihood of dirty state. // We still require a proper solution to handle an application crash or very slow execution times // (which could cause an unlocking pre operation completion). - ctx, cancel := context.WithTimeout( - contextutil.WithNewValues(context.Background(), ctx), - timeout, - ) + ctx, cancel := contextutil.WithNewTimeout(ctx, timeout) defer cancel() //nolint:nestif diff --git a/app/api/controller/repo/default_branch.go b/app/api/controller/repo/default_branch.go index 202d2f6fb..a5554148f 100644 --- a/app/api/controller/repo/default_branch.go +++ b/app/api/controller/repo/default_branch.go @@ -72,10 +72,7 @@ func (c *Controller) UpdateDefaultBranch( // create new, time-restricted context to guarantee update completion, even if request is canceled. // TODO: a proper error handling solution required. - ctx, cancel := context.WithTimeout( - contextutil.WithNewValues(context.Background(), ctx), - timeout, - ) + ctx, cancel := contextutil.WithNewTimeout(ctx, timeout) defer cancel() repoFull, err := c.repoStore.Find(ctx, repo.ID) diff --git a/app/api/controller/space/purge.go b/app/api/controller/space/purge.go index 5bb891dad..d10c1840b 100644 --- a/app/api/controller/space/purge.go +++ b/app/api/controller/space/purge.go @@ -61,10 +61,7 @@ func (c *Controller) PurgeNoAuth( // the max time we give a purge space to succeed const timeout = 15 * time.Minute // create new, time-restricted context to guarantee space purge completion, even if request is canceled. - ctx, cancel := context.WithTimeout( - contextutil.WithNewValues(context.Background(), ctx), - timeout, - ) + ctx, cancel := contextutil.WithNewTimeout(ctx, timeout) defer cancel() var toBeDeletedRepos []*types.Repository diff --git a/app/services/importer/repository.go b/app/services/importer/repository.go index 4de3fec3f..789a54b92 100644 --- a/app/services/importer/repository.go +++ b/app/services/importer/repository.go @@ -360,7 +360,7 @@ func (r *Repository) Handle(ctx context.Context, data string, _ job.ProgressRepo repo.GitUID = gitUID // make sure to delete the correct directory - if errDel := r.deleteGitRepository(context.Background(), &systemPrincipal, repo); errDel != nil { + if errDel := r.deleteGitRepository(context.WithoutCancel(ctx), &systemPrincipal, repo); errDel != nil { log.Warn().Err(errDel). Msg("failed to delete git repository after failed import") } diff --git a/app/services/locker/locker.go b/app/services/locker/locker.go index 1bc5a3fe2..e44d8bfd6 100644 --- a/app/services/locker/locker.go +++ b/app/services/locker/locker.go @@ -74,10 +74,7 @@ func (l Locker) lock( unlockFn := func() { // always unlock independent of whether source context got canceled or not - ctx, cancel := context.WithTimeout( - contextutil.WithNewValues(context.Background(), ctx), - 30*time.Second, - ) + ctx, cancel := contextutil.WithNewTimeout(ctx, 30*time.Second) defer cancel() err := mutext.Unlock(ctx) diff --git a/contextutil/contextutil.go b/contextutil/contextutil.go index b5e16ff4e..cc990ec6e 100644 --- a/contextutil/contextutil.go +++ b/contextutil/contextutil.go @@ -16,21 +16,10 @@ package contextutil import ( "context" + "time" ) -// WithNewValues creates a new context derived from originalCtx with values from valuesCtx. -func WithNewValues(originalCtx context.Context, valuesCtx context.Context) context.Context { - return &combinedContext{ - Context: originalCtx, - valuesCtx: valuesCtx, - } -} - -type combinedContext struct { - context.Context - valuesCtx context.Context -} - -func (c *combinedContext) Value(key any) any { - return c.valuesCtx.Value(key) +// WithNewTimeout creates a new context derived from original context, but without canceling and with the new timeout. +func WithNewTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) { + return context.WithTimeout(context.WithoutCancel(ctx), timeout) }