Skip to content

Conversation

@ANJAN672
Copy link

Description

This PR fixes a bug where the keys variable in get_api_keys() may be uninitialized, causing a NameError.

The Problem

If get_api_keys_from_database() fails all 3 retry attempts, the keys variable was never assigned, causing a crash on line 106:

NameError: name 'keys' is not defined

Changes Made

  1. Initialize keys = [] before the retry loop - Prevents NameError if all attempts fail
  2. Replace bare except: with except Exception as e: - Avoids catching system exceptions like KeyboardInterrupt
  3. Add logging for failed attempts - Helps with debugging
  4. Remove unused code - Deleted unused valid_now variable and commented-out code

Testing

  • Code review confirms the fix addresses the initialization issue
  • The change is backwards compatible and doesn't affect normal operation

Related

Fixes #3602

Signed commits

  • Yes, I signed my commits.

- Initialize keys=[] before retry loop to prevent NameError if all
  database attempts fail
- Replace bare except: with except Exception for better debugging
- Add logging for failed database attempts
- Remove unused variable assignment (valid_now) and dead commented code

Fixes chaoss#3602

Signed-off-by: ANJAN672 <[email protected]>
@MoralCode MoralCode changed the title fix: prevent NameError from uninitialized keys variable in github_api_key_handler.py fix: prevent theoretical NameError from uninitialized keys variable in github_api_key_handler.py Jan 18, 2026
@MoralCode
Copy link
Contributor

my understanding from slack was that you havent actually encountered this exception, but noticed it was theoretically possible, so i changed the issue title to reflect that

keys = self.get_api_keys_from_database()
break
except:
except Exception as e:
Copy link
Member

Choose a reason for hiding this comment

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

I think this may be more effective using except* to capture nested exceptions and decode them all; though I think @ABrain7710 will have the final say on whether or not this is a good idea.

@sgoggins sgoggins added the API Related to Augur's metrics API label Jan 20, 2026
@sgoggins
Copy link
Member

If this is not theoretical, can you add the stack, @ANJAN672 ?

@MoralCode MoralCode marked this pull request as draft January 21, 2026 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API Related to Augur's metrics API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Uninitialized variable in github_api_key_handler.py causes NameError

4 participants