Skip to content

fix: Exit initializer analysis on context cancel#10003

Open
liamdalg wants to merge 1 commit intoGoogleContainerTools:mainfrom
liamdalg:main
Open

fix: Exit initializer analysis on context cancel#10003
liamdalg wants to merge 1 commit intoGoogleContainerTools:mainfrom
liamdalg:main

Conversation

@liamdalg
Copy link

@liamdalg liamdalg commented Mar 5, 2026

Description

Running skaffold init in a directory with a large number of files can take an extremely long time as it crawls and analyzes all subdirectories. The use of context.Background() means that it is not derived from the top-level context, and signals (e.g. the user pressing CTRL-C) cannot cancel the operation.

This has caused issues for me when accidentally running e.g. skaffold dev in the wrong directory, which falls back to init.

I've added in a context.Context param where appropriate in the initializer package, and checked ctx.Err() when iterating over analyzers in order to exit out early.

This has required some changes to some public parts of the initializer package. If that's a problem, separate functions like AnalyzeWithContext function could be added instead.

User facing changes

Before: pressing CTRL-C skaffold init does nothing, as the parent context is not connected to the analysis context.

$ time ~/pkg/skaffold/skaffold dev
Skaffold config file skaffold.yaml not found - Trying to create one for you...
^C^C^C^C^C^C^C^C^C^C^C^Cunable to generate skaffold config file automatically - try running `skaffold init`: one or more valid build configuration must be present to build images with Skaffold; please provide at least one build config and try again, or run `skaffold init --skip-build`

real	0m4.638s
user	0m4.690s
sys	0m1.285s

After: pressing CTRL-C during skaffold init immediately terminates the operation.

λ time ~/pkg/skaffold/skaffold dev
Skaffold config file skaffold.yaml not found - Trying to create one for you...
^C
real	0m0.826s
user	0m1.006s
sys	0m0.272s

- Pass parent context down to analyzers
- Check for ctx.Err() before recursing subdirectories or analyzing
  files.

Running skaffold init in a directory with a large number of files
can take an extremely long time as it crawls and analyzes all
subdirectories. Previously, the use of context.Background() prevented
signals (e.g. the user pressing CTRL-C) from cancelling the operation.
@liamdalg liamdalg requested a review from a team as a code owner March 5, 2026 00:58
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the user experience for skaffold init by enabling context-aware cancellation. It resolves an issue where the initialization process, particularly in large codebases, could become unresponsive to user interruptions. By integrating context.Context throughout the relevant analysis functions, the system can now detect and respond to cancellation signals, leading to a more robust and user-friendly application.

Highlights

  • Context Cancellation for skaffold init: The skaffold init process, and skaffold dev when it falls back to init, can now be gracefully cancelled by user signals (e.g., CTRL-C). Previously, these operations used context.Background(), preventing interruption during lengthy directory analyses.
  • Context Propagation: The context.Context is now propagated through key functions within the initializer package, including AnalyzeProject and ProjectAnalysis.Analyze, allowing for early exit checks.
  • Early Exit Mechanism: The ProjectAnalysis.Analyze method now explicitly checks ctx.Err() during directory traversal and file analysis, ensuring that the operation terminates promptly upon context cancellation.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • cmd/skaffold/app/cmd/runner.go
    • Updated the call to initializer.Transparent to pass the context.Context instead of context.Background().
  • pkg/skaffold/initializer/analyze/analyze.go
    • Modified the Analyze method of ProjectAnalysis to accept a context.Context parameter.
    • Added checks for ctx.Err() at the beginning of Analyze and before calling analyzer.analyzeFile to enable early exit.
    • Updated recursive calls to a.Analyze to pass the new context.Context parameter.
  • pkg/skaffold/initializer/analyze/analyze_test.go
    • Updated test calls to a.Analyze to pass t.Context().
  • pkg/skaffold/initializer/helm_test.go
    • Updated test calls to a.Analyze to pass t.Context().
  • pkg/skaffold/initializer/init.go
    • Modified DoInit and AnalyzeProject functions to accept and propagate a context.Context parameter.
  • pkg/skaffold/initializer/transparent.go
    • Updated the call to AnalyzeProject to pass the context.Context parameter.
Activity
  • The author identified a critical issue where skaffold init (and skaffold dev fallback) could not be cancelled, leading to long waits in large directories.
  • The author proposed a solution involving context.Context propagation and early exit checks.
  • The author provided clear before-and-after examples demonstrating the user-facing impact of the change, showing a significant reduction in termination time.
  • The author acknowledged potential concerns regarding changes to public parts of the initializer package.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an important usability issue by making the initializer analysis cancellable. The changes correctly propagate the context down through the initializer package and add checks for cancellation. This will prevent skaffold from hanging when run in large directories. I've added one suggestion to further improve the responsiveness of the cancellation.

Comment on lines 197 to 201
for _, subdir := range subdirectories {
if err := a.Analyze(subdir); err != nil {
if err := a.Analyze(ctx, subdir); err != nil {
return err
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To improve responsiveness to cancellation, it's good practice to check if the context is done inside loops that might have many iterations. While the recursive call to Analyze checks the context at the beginning, adding a check here before the call will make cancellation more immediate if there are many subdirectories to process.

for _, subdir := range subdirectories {
	if err := ctx.Err(); err != nil {
		return err
	}
	if err := a.Analyze(ctx, subdir); err != nil {
		return err
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant