Skip to content

chore: Introduce re-usable teardown function#6679

Merged
PeterSchafer merged 1 commit intomainfrom
chore/CLI-1416_terminated
Apr 28, 2026
Merged

chore: Introduce re-usable teardown function#6679
PeterSchafer merged 1 commit intomainfrom
chore/CLI-1416_terminated

Conversation

@PeterSchafer
Copy link
Copy Markdown
Contributor

@PeterSchafer PeterSchafer commented Mar 23, 2026

Pull Request Submission Checklist

  • Follows CONTRIBUTING guidelines
  • Commit messages are release-note ready, emphasizing what was changed, not how.
  • Includes detailed description of changes
  • Contains risk assessment (Low | Medium | High)
  • Highlights breaking API changes (if applicable)
  • Links to automated tests covering new functionality
  • Includes manual testing instructions (if necessary)
  • Updates relevant GitBook documentation (PR link: ___)
  • Includes product update to be communicated in the next stable release notes

What does this PR do?

This PR refactors the application teardown logic into a re-usable function, so that future changes can be based on it, for example for special case handling like timeout, signals ...

The PR also adds missing support for context in the legacycli workflow.

  1. Refactors teardown logic into a dedicated tearDown() function that handles analytics, instrumentation, cleanup, and logging
  2. Extracts instrumentation helpers to a new instrumentation.go file for better code organization

Where should the reviewer start?

Start with cliv2/cmd/cliv2/main.go:487-523 for the new tearDown() function, then review the signal handling setup at lines 635-656.

How should this be manually tested?

This is just a refactoring to prepare for future changes, so it should be functionally unchanged.

What's the product update that needs to be communicated to CLI users?

N/A - No user-facing behavior changes.

Risk assessment (Low | Medium | High)?

Low - refactoring only

@PeterSchafer PeterSchafer requested review from a team as code owners March 23, 2026 17:36
@snyk-io
Copy link
Copy Markdown

snyk-io Bot commented Mar 23, 2026

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@snyk-pr-review-bot

This comment has been minimized.

@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch from 0381db7 to 511765e Compare March 24, 2026 10:18
@snyk-pr-review-bot

This comment has been minimized.

@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch from d94d307 to 402304a Compare March 24, 2026 11:04
@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch 2 times, most recently from eb91fb0 to dccddef Compare March 24, 2026 11:15
@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch from dccddef to 5984ccb Compare March 24, 2026 11:18
@snyk-pr-review-bot

This comment has been minimized.

@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch from 5984ccb to f9e02d6 Compare March 24, 2026 11:42
@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch from f9e02d6 to 1d4c73a Compare March 24, 2026 18:24
@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch from 1d4c73a to 8d03e26 Compare March 24, 2026 19:25
@PeterSchafer PeterSchafer enabled auto-merge March 24, 2026 19:25
@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch from 8d03e26 to efc5edd Compare March 24, 2026 19:34
@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch from efc5edd to 91dc563 Compare March 25, 2026 07:24
@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch from 91dc563 to 34f431c Compare March 30, 2026 12:13
@snyk-pr-review-bot

This comment has been minimized.

Comment thread cliv2/cmd/cliv2/main.go Outdated
errorListCopy := append([]error{}, errorList...)
errorListMutex.Unlock()

finalExitCode = tearDown(ctx, signalError, errorListCopy, startTime, ua, cliAnalytics, networkAccess)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

question: if teardown hangs (e.g. when making the analytics API call), can we still SIGINT the process? Or does teardownOnce prevent this? Basically, is it possible to be a hanging state?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As mentioned before, I'm working on a solution.

@snyk-pr-review-bot

This comment has been minimized.

@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch from a6b802f to 856f008 Compare April 1, 2026 16:21
@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch from 856f008 to 0a239d7 Compare April 2, 2026 15:56
@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch from 0a239d7 to ad74501 Compare April 2, 2026 19:42
@snyk-pr-review-bot

This comment has been minimized.

Comment thread cliv2/cmd/cliv2/main.go Outdated
Comment on lines +491 to +493
func tearDown(ctx context.Context, err error, errorList []error, startTime time.Time, ua networking.UserAgentInfo, cliAnalytics analytics.Analytics, networkAccess networking.NetworkAccess) int {
// Create a context with timeout for teardown operations to ensure we don't hang indefinitely
teardownCtx, cancel := context.WithTimeout(ctx, teardownTimeout)
Copy link
Copy Markdown
Contributor

@danskmt danskmt Apr 3, 2026

Choose a reason for hiding this comment

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

The context ctx here can happen to be already-cancelled as it is derived from the parent context. Testing locally, I saw this issue happening:

Image

Here I suppose you should use a fresh context for tearing down:

teardownCtx, cancel := context.WithTimeout(context.Background(), teardownTimeout)

Also, the context parameter we can use ctxCancel, updating the function signature:
func tearDown(ctx context.Context, ... -> func tearDown(ctxCancel context.CancelFunc, ...

in the calls of tearDown (L664 and L716), we provide ctxCancel:

finalExitCode = tearDown(ctxCancel, signalError, errorListCopy, startTime, ua, cliAnalytics, networkAccess)
...
finalExitCode = tearDown(ctxCancel, err, errorListCopy, startTime, ua, cliAnalytics, networkAccess)

And after sendInstrumentation call (line 516) can terminate child processes:

sendInstrumentation(teardownCtx, globalEngine, cliAnalytics.GetInstrumentation(), globalLogger)
ctxCancel()

i.e. instead of calling ctxCancel in line 655, we do it here after sendInstrumentation, wdyt?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @danskmt ! Good catches! I marked the PR as draft as it is far from ready. There are plenty of local changes as well, so I prematurely marked this as to review. Sorry for that!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No worries! Let me know when you want me to review it again.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

@PeterSchafer PeterSchafer marked this pull request as draft April 7, 2026 08:53
auto-merge was automatically disabled April 7, 2026 08:53

Pull request was converted to draft

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch 4 times, most recently from 45986fc to 5999dd6 Compare April 28, 2026 08:21
@PeterSchafer PeterSchafer changed the title chore: Send instrumentation data on kill chore: Introduce re-usable teardown function Apr 28, 2026
@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch 4 times, most recently from ce8c162 to 544b6b0 Compare April 28, 2026 10:40
@PeterSchafer PeterSchafer marked this pull request as ready for review April 28, 2026 10:43
@PeterSchafer PeterSchafer requested a review from a team as a code owner April 28, 2026 10:43
@snyk-pr-review-bot

This comment has been minimized.

@PeterSchafer PeterSchafer force-pushed the chore/CLI-1416_terminated branch from 544b6b0 to 808dbbd Compare April 28, 2026 12:19
@snyk-pr-review-bot
Copy link
Copy Markdown

PR Reviewer Guide 🔍

🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Lifecycle Logic Error 🟠 [major]

The tearDown function derives the exit code based on the err parameter. In MainWithErrorCode, the sync.Once closure captures the err variable, which is initialized to nil and only updated once rootCommand.Execute() returns. If an interrupt signal triggers tearDown while the command is still running (the intended use case for this refactor), tearDown will receive the current nil value of err. This results in the CLI returning exit code 0 and reporting success to analytics even when the process was terminated prematurely by a signal.

finalExitCode = tearDown(err, errorListCopy, startTime, ua, cliAnalytics, networkAccess)
Context Inconsistency 🟡 [minor]

The tearDown function creates a new teardownCtx with a 5-second timeout to ensure cleanup operations don't hang. However, it passes globalContext to displayError instead of teardownCtx. If globalContext is already cancelled (e.g., if it was hooked to a termination signal) or if the UI output blocks, displayError may fail to show the final error message or may bypass the teardown grace period. Using teardownCtx would ensure the error display is subject to the same lifecycle constraints as the rest of the teardown.

displayError(outputError, globalEngine.GetUserInterface(), globalConfiguration, globalContext)
📚 Repository Context Analyzed

This review considered 23 relevant code sections from 12 files (average relevance: 0.92)

@PeterSchafer PeterSchafer enabled auto-merge April 28, 2026 14:52
@PeterSchafer PeterSchafer merged commit 7a7dec7 into main Apr 28, 2026
9 checks passed
@PeterSchafer PeterSchafer deleted the chore/CLI-1416_terminated branch April 28, 2026 16:46
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