diff options
author | Benjamin Peterson <benjamin@python.org> | 2017-09-20 06:29:56 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2017-09-20 14:34:19 (GMT) |
commit | 09d93a4e7f79b7dd16de68dffc12629516fb69cb (patch) | |
tree | 909edc061b297034c3c5664c09997a611f7a236d /Lib | |
parent | 7795321c566322513892543a80a5b06adbe3b04d (diff) | |
download | cpython-benjamin-iteration-torture.zip cpython-benjamin-iteration-torture.tar.gz cpython-benjamin-iteration-torture.tar.bz2 |
bpo-31530: stop crashes when iterating over a file on multiple threadsbenjamin-iteration-torture
Multiple threads iterating over a file can corrupt the file's internal readahead
buffer resulting in crashes. To fix this, cache buffer state thread-locally for
the duration of a file_iternext call and only update the file's internal state
after reading completes.
No attempt is made to define or provide "reasonable" semantics for iterating
over a file on multiple threads. (Non-crashing) races are still
present. Duplicated, corrupt, and missing data will happen.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_file2k.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/Lib/test/test_file2k.py b/Lib/test/test_file2k.py index e39ef70..3b59ed3 100644 --- a/Lib/test/test_file2k.py +++ b/Lib/test/test_file2k.py @@ -652,6 +652,16 @@ class FileThreadingTests(unittest.TestCase): self.f.writelines('') self._test_close_open_io(io_func) + def test_iteration_torture(self): + with open(self.filename, "wb") as fp: + for i in xrange(2**20): + fp.write(b"0"*50 + b"\n") + with open(self.filename, "rb") as f: + def it(): + for l in f: + pass + self._run_workers(it, 10) + @unittest.skipUnless(os.name == 'posix', 'test requires a posix system.') class TestFileSignalEINTR(unittest.TestCase): |