@@ -398,8 +398,9 @@ def _read(
398398 )
399399
400400 # For large local uncompressed files with the C engine, attempt parallel reading.
401- # Each worker gets its own file handle and TextReader so the GIL-free tokenisation
402- # and type-conversion code in parsers.pyx runs truly in parallel.
401+ # Each worker gets its own view of the mmapped file and its own TextReader so
402+ # the GIL-free tokenisation and type-conversion code in parsers.pyx runs truly
403+ # in parallel.
403404 if not iterator and chunksize is None and nrows is None :
404405 _n_workers = _default_n_workers ()
405406 if _n_workers > 1 and _can_parallelize_csv (filepath_or_buffer , kwds ):
@@ -1007,7 +1008,7 @@ def _read_csv_chunks(
10071008 chunk_failed = threading .Event ()
10081009 workers_readers : list = []
10091010
1010- def _worker () -> None :
1011+ def _worker (mv : memoryview ) -> None :
10111012 reader = TextFileReader (io .BytesIO (b"" ), ** chunk_kwds )
10121013 assert isinstance (reader ._engine , CParserWrapper )
10131014 # Buffers are reused across the queued chunks and freed at close;
@@ -1091,10 +1092,15 @@ def _piece_ndarray(piece) -> np.ndarray | None:
10911092 mm = mmap .mmap (fh .fileno (), 0 , access = mmap .ACCESS_READ )
10921093 try :
10931094 with (
1094- memoryview ( mm ) as mv ,
1095+ contextlib . ExitStack ( ) as views ,
10951096 ThreadPoolExecutor (max_workers = n_workers ) as pool ,
10961097 ):
1097- for fut in [pool .submit (_worker ) for _ in range (n_workers )]:
1098+ # One view per worker: slicing a shared memoryview from several
1099+ # threads races on free-threaded builds (GH#71418).
1100+ worker_views = [
1101+ views .enter_context (memoryview (mm )) for _ in range (n_workers )
1102+ ]
1103+ for fut in [pool .submit (_worker , mv ) for mv in worker_views ]:
10981104 fut .result ()
10991105 if chunk_failed .is_set ():
11001106 # Let the serial read raise: a chunk's error can be an artifact
0 commit comments