-
-
Notifications
You must be signed in to change notification settings - Fork 244
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 caching issues in JSON::Util::URI #515
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #515 +/- ##
==========================================
- Coverage 90.08% 90.03% -0.06%
==========================================
Files 76 76
Lines 1584 1575 -9
==========================================
- Hits 1427 1418 -9
Misses 157 157 ☔ View full report in Codecov by Sentry. |
8452324
to
022e25c
Compare
Hey, @bastelfreak 👋 The PR Is ready for review. I would appreciate your feedback |
I have no idea about this gem, sorry. |
My apologies. I pinged you because you reviewed one of the recent PRs #509 |
f05f16d
to
2b57dd0
Compare
Both ruby version are EoL. We've another breaking change that I need to release, voxpupuli#515. 2.7 is EoL as well, but still widely used in the Puppet Ecosystem, so I think switching to 2.7 for now is fine.
Both ruby version are EoL. We've another breaking change that I need to release, voxpupuli#515. 2.7 is EoL as well, but still widely used in the Puppet Ecosystem, so I think switching to 2.7 for now is fine.
@bolshakov I think we're ready for a merge. Can you please rebase this and if the updated rubocop config is happy, I can merge it. |
2b57dd0
to
f4a5ca3
Compare
@bastelfreak ready |
Is there a timeline on when this will be merged? |
Hi, I am sorry for the huge delay. Thanks for all the work and thanks for pinging me again! |
I decided not to re-raise the `Addressable::URI::InvalidURIError`, which was originally re-raised as `JSON::Schema::UriError`. This decision was made because the subsequent call to `Addressable::URI.unescape(uri)` does not handle these errors, requiring the user to handle both types of errors. Given that any call to the Addressable gem could potentially lead to an error, and the JSON Schema gem does not consistently wrap them as `JSON::Schema::UriError`, I decided to simplify the code for now. Error handling can be added later if necessary.
…f method" This reverts commit 401f707.
f4a5ca3
to
2ad9e19
Compare
This PR fixes the issue #514
The implementation of the whole
JSON::Util::URI
was not thread-safe. For instance, if you look at the.parse
method.In line
#8
, the URL is retrieved from the cache, and the caller receives a duplicate of the cached URL. However, in line#10
, the caller does not receive a duplicate, and in a few instances, it changes the returned reference, effectively modifying it in the cache.As you can see, if the referred URI is not in the cache, it is added to the cache and then modified inside the
.normalize_ref
method. Basically, we pass an object by reference between methods and modify it. This code is not thread-safe because it accesses and modifies a shared resource,@parse_cache
, without any form of synchronization.Other methods that call
.parse
method to resolve references also implement a similar non-synchronized caching approach. In a multi-threaded environment, this could lead to a race condition and potential incorrect resolution of references in JSON Schemas.Initially, I wanted to add Mutex synchronization or use thread-safe data structures (such as
Corcurrent::Map
for cache), but it appeared that the code is deeply intertwined, with many methods from the module calling each other, making synchronization prone to deadlocks.I have removed caching from this module entirely because it is not thread-safe and causes more problems than it solves. The improvements it offers are minimal since we don't cache expensive network or disk calls but only in-memory calculations.
I also fixed a couple of bugs in method implementations along the way:
URI.absolutize_uri
method. TheURI.absolutize_ref
implementation does not work as described in the tests. The tests pass only because of caching that persists state between subsequent calls in the test example. See this PR showing how to reproduce the issue: Reproduce the issue with absolutize_uri #516.URI.normalized_ref
method which can persist state between calls leading to incorrect reference normalisation. This PR Reproduce caching issue with the JSON::Util::URI.normalize_ref method #517 reproduces the issue.I tried to keep, changes in the tests suite at minimum, but ended up changing this single test which behaviour was not consistent with other test examples..