Skip to content

fix: handle partial initialization in BaseApiClient GC cleanup#2022

Open
chidifrank wants to merge 2 commits intogoogleapis:mainfrom
chidifrank:fix/aclose-attribute-error-partial-init
Open

fix: handle partial initialization in BaseApiClient GC cleanup#2022
chidifrank wants to merge 2 commits intogoogleapis:mainfrom
chidifrank:fix/aclose-attribute-error-partial-init

Conversation

@chidifrank
Copy link

@chidifrank chidifrank commented Feb 3, 2026

This fixes a crash that occurs during garbage collection if BaseApiClient fails to initialize (e.g., due to conflicting arguments). Because init can raise an exception before attributes like _http_options are set, the subsequent cleanup via del and aclose() previously failed with an AttributeError.

Added guards in close() and aclose() to ensure the client checks for the existence of these attributes before attempting to close them, allowing partially initialized objects to be garbage collected gracefully.

Fixes #2012

^ A more cleaner long-term solution would be to initialize all attributes at the very start of init. This ensures the object state is always deterministic, allowing close() and aclose() to use simple if checks instead of try-except blocks. This would require a bigger refactor. Tracked in #2023

@google-cla
Copy link

google-cla bot commented Feb 3, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@chidifrank chidifrank force-pushed the fix/aclose-attribute-error-partial-init branch 7 times, most recently from 8ec6f58 to 8984749 Compare February 5, 2026 07:45
@janasangeetha janasangeetha self-assigned this Feb 5, 2026
@janasangeetha janasangeetha added the size:L Code changes between 40-100 lines label Feb 5, 2026
@chidifrank chidifrank force-pushed the fix/aclose-attribute-error-partial-init branch from 8984749 to 2525254 Compare February 6, 2026 15:53
@chidifrank chidifrank force-pushed the fix/aclose-attribute-error-partial-init branch from 2525254 to c33f305 Compare February 7, 2026 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L Code changes between 40-100 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AttributeError in BaseApiClient GC cleanup when initialization fails

2 participants