Skip to content

Comments

refactor: enable context-aware operations and graceful shutdown throughout#3

Merged
appleboy merged 1 commit intomainfrom
fix/lint
Feb 20, 2026
Merged

refactor: enable context-aware operations and graceful shutdown throughout#3
appleboy merged 1 commit intomainfrom
fix/lint

Conversation

@appleboy
Copy link
Member

  • 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>
Copilot AI review requested due to automatic review settings February 20, 2026 01:52
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

@appleboy appleboy merged commit e6d3dec into main Feb 20, 2026
20 checks passed
@appleboy appleboy deleted the fix/lint branch February 20, 2026 01:57
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.

1 participant