Skip to content

fix: use cmd.Context() instead of context.Background()#2981

Merged
dgageot merged 1 commit into
docker:mainfrom
dgageot:board/47021be572c1fdfd
Jun 3, 2026
Merged

fix: use cmd.Context() instead of context.Background()#2981
dgageot merged 1 commit into
docker:mainfrom
dgageot:board/47021be572c1fdfd

Conversation

@dgageot
Copy link
Copy Markdown
Member

@dgageot dgageot commented Jun 3, 2026

During an audit of context.Background() usage in the codebase, this was identified as the one genuinely suspicious instance. The shell-completion handler for completeMessage was passing context.Background() to config.Load(), which meant completion operations ignored the cobra command's own context and could not be cancelled or respect deadline constraints.

This change passes cmd.Context() instead, ensuring that shell completion respects cancellation and any deadlines set by the caller. The now-unused context import was removed.

All other context.Background() occurrences in the codebase were reviewed and deemed intentional (detached long-lived resources, sync.Once caches, fire-and-forget calls), so no further changes are needed.

@dgageot dgageot requested a review from a team as a code owner June 3, 2026 08:39
Copy link
Copy Markdown

@docker-agent docker-agent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assessment: 🟢 APPROVE

This is a clean, well-scoped fix. The change correctly replaces context.Background() with cmd.Context() in the completeMessage shell-completion handler.

Analysis:

  • cmd.Context() in Cobra always returns a non-nil context (falls back to context.Background() if none is explicitly set), so this is strictly safer than a bare context.Background().
  • The existing error handling (return nil, cobra.ShellCompDirectiveNoFileComp on config.Load error) already handles any context-cancellation-driven errors gracefully.
  • The context import removal is correct — no other usages of the context package remain in the file.
  • The change is minimal, targeted, and correct. No bugs were found.

@dgageot dgageot merged commit 8404a8e into docker:main Jun 3, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants