Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove default argument now from _seconds_until_refresh #356

Merged
merged 2 commits into from
Jul 25, 2024

Conversation

jackwotherspoon
Copy link
Collaborator

@jackwotherspoon jackwotherspoon commented Jul 24, 2024

_seconds_until_refresh which is used for background refresh strategy
(lazy refresh is unaffected!) should not have now as a default argument
due to Python only evaluating default arguments once at function definition.

Python's default arguments are evaluated only once when the function
is defined, not each time the function is called. (source)

Problematic code:

def _seconds_until_refresh(
expiration: datetime, now: datetime = datetime.now(timezone.utc)
) -> int:
"""
Calculates the duration to wait before starting the next refresh.
Usually the duration will be half of the time until certificate
expiration.
Args:
expiration (datetime.datetime): Time of certificate expiration.
now (datetime.datetime): Current time (UTC)
Returns:
int: Time in seconds to wait before performing next refresh.
"""
duration = int((expiration - now).total_seconds())
# if certificate duration is less than 1 hour
if duration < 3600:
# something is wrong with certificate, refresh now
if duration < _refresh_buffer:
return 0
# otherwise wait until 4 minutes before expiration for next refresh
return duration - _refresh_buffer
return duration // 2

Having now as a default argument means it is set once and then never
adjusted. So in subsequent calls to _seconds_until_refresh the duration
will just continue to grow larger and larger as it evaluates agains the same now

The result of this means that duration // 2 block is always being hit and
will continue to grow. It could grow to be greater than 3600 and lead to errors.

@jackwotherspoon jackwotherspoon self-assigned this Jul 24, 2024
@jackwotherspoon jackwotherspoon requested a review from a team as a code owner July 24, 2024 20:24
@jackwotherspoon jackwotherspoon changed the title chore: explicitly pass in now fix: remove default argument now from _seconds_until_refresh Jul 25, 2024
@enocom enocom self-requested a review July 25, 2024 01:51
@enocom
Copy link
Member

enocom commented Jul 25, 2024

Probably should add a note that this only affects clients using the default refresh strategy and not lazy refresh.

@jackwotherspoon
Copy link
Collaborator Author

Probably should add a note that this only affects clients using the default refresh strategy and not lazy refresh.

good call, added a note to PR description

@jackwotherspoon jackwotherspoon merged commit 27f31cd into main Jul 25, 2024
15 checks passed
@jackwotherspoon jackwotherspoon deleted the update-now branch July 25, 2024 12:21
@enocom
Copy link
Member

enocom commented Jul 25, 2024

Fixes #357

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.

3 participants