Conversation
Member
appleboy
commented
Feb 20, 2026
- Refactor functions throughout the codebase to accept and propagate context.Context for improved cancellation support
- Add signal-based context initialization for graceful shutdown in main
- Move application logic into a run function that returns an exit code for easier control flow
- Use net.ListenConfig with context in place of net.Listen to support context-aware socket binding
- Update browser open, callback server, token exchange, token verification, and refresh operations to be context-aware
- Update all related unit tests to provide context explicitly and use ListenConfig for port binding
- Replace plain lock.release calls with error-ignoring variants to avoid unused return value errors
- Add error handling for JSON encoding failures in all HTTP test handlers
- Introduce a helper function for error condition OAuth device flow tests to reduce duplicate test code
- Minor test improvements including commenting, error messages, and use of constants over literals for tokens
…ghout - Refactor functions throughout the codebase to accept and propagate context.Context for improved cancellation support - Add signal-based context initialization for graceful shutdown in main - Move application logic into a run function that returns an exit code for easier control flow - Use net.ListenConfig with context in place of net.Listen to support context-aware socket binding - Update browser open, callback server, token exchange, token verification, and refresh operations to be context-aware - Update all related unit tests to provide context explicitly and use ListenConfig for port binding - Replace plain lock.release calls with error-ignoring variants to avoid unused return value errors - Add error handling for JSON encoding failures in all HTTP test handlers - Introduce a helper function for error condition OAuth device flow tests to reduce duplicate test code - Minor test improvements including commenting, error messages, and use of constants over literals for tokens Signed-off-by: appleboy <appleboy.tw@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR refactors the OAuth client to support context-aware operations and graceful shutdown. The changes enable proper cancellation propagation throughout the authentication flows and improve the application's ability to handle interrupts and timeouts gracefully.
Changes:
- Introduce signal-based context initialization in main() with graceful shutdown on SIGINT/SIGTERM
- Refactor main() to return an exit code via a new run() function for cleaner control flow
- Add context.Context parameters to key functions: openBrowser, startCallbackServer, performBrowserFlow, exchangeCode, refreshAccessToken, verifyToken, and checkBrowserAvailability
- Replace net.Listen with net.ListenConfig to support context-aware socket binding
- Update all tests to pass context.Background() and use ListenConfig for port allocation
- Improve error handling by explicitly ignoring lock.release() return values with
_ = lock.release() - Add error handling for JSON encoding in HTTP test handlers
- Introduce pollForTokenErrorTest helper to reduce test code duplication
- Add testAccessToken constant to improve test maintainability
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| main.go | Added signal-based context, refactored main into run() function, propagated context to all operations |
| tokens.go | Changed lock.release() to explicitly ignore errors with _ = lock.release() |
| browser.go | Updated openBrowser to use exec.CommandContext for context-aware command execution |
| browser_flow.go | Added context parameter to performBrowserFlow and exchangeCode, propagated to child operations |
| callback.go | Added context parameter to startCallbackServer, used ListenConfig for context-aware binding, renamed shadowed variable |
| detect.go | Added context parameter to checkBrowserAvailability, used ListenConfig for port checking |
| polling_test.go | Added testAccessToken constant, improved error handling in test handlers, introduced pollForTokenErrorTest helper |
| main_test.go | Added context.Background() to refreshAccessToken call, added error handling for JSON encoding |
| filelock_test.go | Updated to explicitly ignore lock.release() errors |
| detect_test.go | Added context.Background() to all test calls, used ListenConfig for test port allocation |
| callback_test.go | Added context parameter to startCallbackServerAsync, added gosec nolint directives |
Comments suppressed due to low confidence (1)
callback.go:110
- The select statement should respect context cancellation. Currently, it only waits for either a result or a timeout, but doesn't check if the parent context is cancelled. Add a case for context cancellation:
case <-ctx.Done(): return "", ctx.Err()
select {
case result := <-resultCh:
if result.Error != "" {
if result.Desc != "" {
return "", fmt.Errorf("%s: %s", result.Error, result.Desc)
}
return "", fmt.Errorf("%s", result.Error)
}
return result.Code, nil
case <-time.After(callbackTimeout):
return "", fmt.Errorf("%w after %s", ErrCallbackTimeout, callbackTimeout)
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.