Skip to content

feat: Prevent indefinite hang in ensure_initialized #20

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

Merged
merged 1 commit into from
Apr 3, 2025

Conversation

bertybuttface
Copy link
Contributor

Problem:

Requests could hang indefinitely within the RemoteJwksDecoder::decode method, specifically while awaiting self.initialized.notified().await inside ensure_initialized. This occurred even after the background refresh task had successfully fetched keys, populated the cache, and called notify_waiters().

Solution:

Modify ensure_initialized to first check if the keys_cache is non-empty. If keys are already present, indicating a successful prior initialization or refresh, the function returns immediately without awaiting the Notify signal. The notified().await is now only used as a fallback mechanism if the cache is empty when the check occurs, preserving the original intent for the very first request synchronization.

This ensures that once keys are successfully loaded, subsequent requests do not block unnecessarily on the potentially unreliable notification mechanism in this context.

Problem:

Requests could hang indefinitely within the `RemoteJwksDecoder::decode` method, specifically while awaiting `self.initialized.notified().await` inside `ensure_initialized`. This occurred even after the background
refresh task had successfully fetched keys, populated the cache, and called `notify_waiters()`.

Solution:
Modify `ensure_initialized` to first check if the `keys_cache` is non-empty. If keys are already present, indicating a successful prior initialization or refresh, the function returns immediately without awaiting the `Notify` signal. The `notified().await` is now only used as a fallback mechanism if the cache is empty when the check occurs, preserving the original intent for the very first request synchronization.

This ensures that once keys are successfully loaded, subsequent requests do not block unnecessarily on the potentially unreliable notification mechanism in this context.
@bertybuttface bertybuttface force-pushed the ensure-initialized-fix branch from 8bac981 to 8d01ee8 Compare April 3, 2025 13:39
@bertybuttface bertybuttface changed the title feat!: Prevent indefinite hang in ensure_initialized feat: Prevent indefinite hang in ensure_initialized Apr 3, 2025
@cmackenzie1
Copy link
Owner

Thanks for the PR to fix this @bertybuttface! Much appreciated!

@cmackenzie1 cmackenzie1 merged commit 85b1129 into cmackenzie1:main Apr 3, 2025
1 check passed
Copy link

codecov bot commented Apr 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 64.01%. Comparing base (237c4e8) to head (8d01ee8).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #20      +/-   ##
==========================================
+ Coverage   62.62%   64.01%   +1.39%     
==========================================
  Files           3        3              
  Lines         206      214       +8     
==========================================
+ Hits          129      137       +8     
  Misses         77       77              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cmackenzie1
Copy link
Owner

Released in v0.5.1 https://crates.io/crates/axum-jwt-auth/0.5.1

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.

2 participants