-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: cpu contention when reading JWKs and suppress generating duplicate JWKs #3883
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
base: master
Are you sure you want to change the base?
Conversation
7aa20e9
to
9f23fee
Compare
…te JWKs Previously each concurrent caller would need to lock a shared mutex when reading or writing a given JWK set. The read path now doesn't require locking a mutex at all and instead returns valid query results directly. The write path is now protected by a concurrency control mechanism (using x/sync/singleflight) to ensure only one JWK set is generated and persisted. Note: Duplicate JWK sets may still be improperly generated if running more than one Hydra instance in a high traffic environment.
9f23fee
to
4b29aaf
Compare
What would be the correct way to verify that this change is indeed behaving as expected? I assume we need some type of race test that calls the helper function multiple times and it has to give one expected result? I think it would be good to have a test for this, as otherwise it's unclear if this fix works or breaks like the last time we merged a fix for this! Thank you :) |
@aeneasr Yeah that's a good idea. I think a good test would be to invoke the function with parallel calls that include a change to every argument that should cause unique results. Then ensure that each parallel caller gets a different result. I can give this a shot if you think that sounds reasonable. |
Nice! So I think a test set up like this would work:
|
The numbers probably depend, it maybe enough to generate 2 unique keys and call it 25 times instead. I think the big challenge here is making sure that a lot of requests on this function do not cause problems. I'm not sure if we depend on database magic for locking, if we do, you may need to run this against a real database because SQLite does not support concurrent writes iirc |
Would you still be open for the change? |
Yeah I mean to take a look, probably next weekend if possible. |
Previously each concurrent caller would need to lock a shared mutex when reading or writing a given JWK set. The read path now doesn't require locking a mutex at all and instead returns valid query results directly. The write path is now protected by a concurrency control mechanism (using x/sync/singleflight) to ensure only one JWK set is generated and persisted.
Note: Duplicate JWK sets may still be improperly generated if running more than one Hydra instance in a high traffic environment.
Related issue(s)
Fixes #3863
Checklist
introduces a new feature.
contributing code guidelines.
vulnerability. If this pull request addresses a security vulnerability, I
confirm that I got the approval (please contact
security@ory.sh) from the maintainers to push
the changes.
works.
Further Comments
This fixes a bug #3876 with the previous attempt to fix this issue .