Lazy-validate AAI_AUTH_PORT to prevent import-time crashes#53
Merged
Conversation
- auth/endpoints.py parsed AAI_AUTH_PORT with a module-level int() at import
time. This module is on the CLI's import hot path, so a malformed value
(e.g. AAI_AUTH_PORT=abc) raised a raw ValueError that crashed *every* aai
command, even 'aai --help' — not just 'aai login'. Resolve the port lazily
in a validated loopback_port() that raises a clean CLIError (with range
check) only on the login path.
- init/runner.spawn() passed log_path.open("w") straight to Popen and never
closed the parent's handle, leaking a file descriptor for the lifetime of
the (long-lived) tunnel process. Close it after Popen returns; the child
keeps its own dup.
Tests updated to drive the port via AAI_AUTH_PORT, with added boundary
coverage (1, 65535, 65536, 0, non-integer).
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.
Summary
Refactor the loopback port configuration to validate
AAI_AUTH_PORTlazily at call time rather than at module import time. This prevents malformed environment variables from crashing every CLI command (even--help) with a rawValueError, instead surfacing cleanCLIErrorexceptions only on the login path.Key Changes
aai_cli/auth/endpoints.pyLOOPBACK_PORT = int(os.environ.get(...))with aloopback_port()function that validates on demand_invalid_auth_port()helper to emit cleanCLIError(exit code 2) with actionable suggestionsValueErrorfrom non-integer input and re-raise asCLIErrorto avoid raw exceptions at import timeredirect_uri()to callloopback_port()instead of referencing the constantaai_cli/auth/loopback.pyloopback_port()once at the start ofcapture_callback()and reuse the result to avoid redundant validationtests/test_auth_endpoints.pytest_loopback_port_rejects_non_integer()— typo'd port surfaces as cleanCLIErrortest_loopback_port_rejects_above_max()— reject 65536+test_loopback_port_accepts_boundary_values()— accept 1 and 65535test_loopback_port_rejects_zero()— reject OS-assigned port 0test_env_override_changes_redirect_uri()to usemonkeypatch.setenv()instead of direct attribute patchingtests/test_auth_loopback.pymonkeypatch.setattr(endpoints, "LOOPBACK_PORT", ...)withmonkeypatch.setenv("AAI_AUTH_PORT", ...)throughoutendpoints.LOOPBACK_PORTreferences to callendpoints.loopback_port()aai_cli/init/runner.pyspawn(): close the log file handle immediately afterPopenreturns (the child process retains its own duplicate), preventing the fd from being held open for the process's entire lifetimetests/test_init_runner.pyspawn()returns, confirming the leak is fixedImplementation Details
The lazy validation approach ensures that:
AAI_AUTH_PORTloopback_port()(i.e.,aai login) trigger validationCLIErrorwith exit code 2 and a suggestion, not a raw tracebackhttps://claude.ai/code/session_01AsrPL95Kr31qCfvfb1Lkaz