-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-120317: Lock around global state in the tokenize module #120318
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
900dd6a
gh-120317: Lock around tokenizer calls in the tokenizer module
lysnikolaou abf568a
Use Py_BEGIN_CRITICAL_SECTION
lysnikolaou 9dfe174
Add more locking around global tokenizer state
lysnikolaou 5fd07b6
Add test
lysnikolaou 814d9af
Address feedback & improve test
lysnikolaou c1093be
Guard atomic store behind Py_GIL_DISABLED
lysnikolaou 117d256
Add requires working threading decorator to test
lysnikolaou 975f01a
Merge branch 'main' into tokenize-thread-safety
lysnikolaou ca466fa
Lock around all of tokenizeriter_next
lysnikolaou 3abb8ab
Merge branch 'main' into tokenize-thread-safety
lysnikolaou 601e539
Fix formatting
lysnikolaou 6290f19
Merge branch 'main' into tokenize-thread-safety
lysnikolaou 0ddde2f
Change sleeping time
lysnikolaou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import io | ||
import time | ||
import unittest | ||
import tokenize | ||
from functools import partial | ||
from threading import Thread | ||
|
||
from test.support import threading_helper | ||
|
||
|
||
@threading_helper.requires_working_threading() | ||
class TestTokenize(unittest.TestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested this locally: it crashes spectacularly without the fix :) |
||
def test_tokenizer_iter(self): | ||
source = io.StringIO("for _ in a:\n pass") | ||
it = tokenize._tokenize.TokenizerIter(source.readline, extra_tokens=False) | ||
|
||
tokens = [] | ||
def next_token(it): | ||
while True: | ||
try: | ||
r = next(it) | ||
tokens.append(tokenize.TokenInfo._make(r)) | ||
time.sleep(0.03) | ||
except StopIteration: | ||
return | ||
|
||
threads = [] | ||
for _ in range(5): | ||
threads.append(Thread(target=partial(next_token, it))) | ||
|
||
for thread in threads: | ||
thread.start() | ||
|
||
for thread in threads: | ||
thread.join() | ||
|
||
expected_tokens = [ | ||
tokenize.TokenInfo(type=1, string='for', start=(1, 0), end=(1, 3), line='for _ in a:\n'), | ||
tokenize.TokenInfo(type=1, string='_', start=(1, 4), end=(1, 5), line='for _ in a:\n'), | ||
tokenize.TokenInfo(type=1, string='in', start=(1, 6), end=(1, 8), line='for _ in a:\n'), | ||
tokenize.TokenInfo(type=1, string='a', start=(1, 9), end=(1, 10), line='for _ in a:\n'), | ||
tokenize.TokenInfo(type=11, string=':', start=(1, 10), end=(1, 11), line='for _ in a:\n'), | ||
tokenize.TokenInfo(type=4, string='', start=(1, 11), end=(1, 11), line='for _ in a:\n'), | ||
tokenize.TokenInfo(type=5, string='', start=(2, -1), end=(2, -1), line=' pass'), | ||
tokenize.TokenInfo(type=1, string='pass', start=(2, 2), end=(2, 6), line=' pass'), | ||
tokenize.TokenInfo(type=4, string='', start=(2, 6), end=(2, 6), line=' pass'), | ||
tokenize.TokenInfo(type=6, string='', start=(2, -1), end=(2, -1), line=' pass'), | ||
tokenize.TokenInfo(type=0, string='', start=(2, -1), end=(2, -1), line=' pass'), | ||
] | ||
|
||
tokens.sort() | ||
expected_tokens.sort() | ||
self.assertListEqual(tokens, expected_tokens) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.