Repository navigation
Fix thread-unsafe cache initialization in encode() and decode() - #29
Open
JonathanLStoff wants to merge 1 commit into
Open
JonathanLStoff wants to merge 1 commit into
JonathanLStoff wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi @hukkin and the
executablebooksmaintainers — thank you for maintainingmdurl! We rely on it in our Python tooling viamarkdown-it-py.Fixes #27.
Problem
Both
get_encode_cache()insrc/mdurl/_encode.pyandget_decode_cache()insrc/mdurl/_decode.pystore a newly allocated emptylistin the module-level cache (encode_cache[exclude] = cache/decode_cache[exclude] = cache) before populating its 128 ASCII entries and applying theexcludeoverrides.When multiple threads call
mdurl.encode()ormdurl.decode()concurrently with an uncachedexcludestring (for example,markdown-it-py'snormalizeLink()andnormalizeLinkText()running across worker threads on startup, under GIL thread switches or free-threaded Python 3.13t/3.14t):encode_cache/decode_cachewhile the first loop (range(128)) is still appending observes a partial list and raisesIndexError: list index out of rangewhen indexingcache[code]orcache[b1].excludeloop finishes silently observes an incomplete lookup table withoutexcludeapplied — causingencode()to percent-encode excluded URL delimiters ordecode()to decode excluded sequences such as%2F,%3F, and%25.Solution
cachecompletely as a local list inget_encode_cache()andget_decode_cache()before publishingencode_cache[exclude] = cacheanddecode_cache[exclude] = cache..get(exclude)lookup on the fast path instead ofinfollowed by[].tests/test_encode.pyandtests/test_decode.pyexercising concurrent cold-cache calls toencode()anddecode().Verification
test_encode_thread_safetyandtest_decode_thread_safetyfail deterministically before this change (IndexErrorand unexcluded decode corruption) and pass with the fix (pytest, 191 passed).black --checkandflake8/isort-equivalent checks.