Skip to content

Commit fb26d9c

Browse files
authored
gh-116738: Make csv module thread-safe (gh-141365)
Added a critical section to protect the states of `ReaderObj` and `WriterObj` in the free-threading build. Without the critical sections, both new free-threading tests were crashing.
1 parent f15f6d0 commit fb26d9c

File tree

3 files changed

+81
-9
lines changed

3 files changed

+81
-9
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import csv
2+
import io
3+
import unittest
4+
5+
from test.support import threading_helper
6+
from test.support.threading_helper import run_concurrently
7+
8+
9+
NTHREADS = 10
10+
11+
12+
@threading_helper.requires_working_threading()
13+
class TestCSV(unittest.TestCase):
14+
def test_concurrent_reader_next(self):
15+
input_rows = [f"{i},{i},{i}" for i in range(50)]
16+
input_stream = io.StringIO("\n".join(input_rows))
17+
reader = csv.reader(input_stream)
18+
output_rows = []
19+
20+
def read_row():
21+
for row in reader:
22+
self.assertEqual(len(row), 3)
23+
output_rows.append(",".join(row))
24+
25+
run_concurrently(worker_func=read_row, nthreads=NTHREADS)
26+
self.assertSetEqual(set(input_rows), set(output_rows))
27+
28+
def test_concurrent_writer_writerow(self):
29+
output_stream = io.StringIO()
30+
writer = csv.writer(output_stream)
31+
row_per_thread = 10
32+
expected_rows = []
33+
34+
def write_row():
35+
for i in range(row_per_thread):
36+
writer.writerow([i, i, i])
37+
expected_rows.append(f"{i},{i},{i}")
38+
39+
run_concurrently(worker_func=write_row, nthreads=NTHREADS)
40+
41+
# Rewind to the start of the stream and parse the rows
42+
output_stream.seek(0)
43+
output_rows = [line.strip() for line in output_stream.readlines()]
44+
45+
self.assertEqual(len(output_rows), NTHREADS * row_per_thread)
46+
self.assertListEqual(sorted(output_rows), sorted(expected_rows))
47+
48+
49+
if __name__ == "__main__":
50+
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make csv module thread-safe on the :term:`free threaded <free threading>`
2+
build.

Modules/_csv.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ parse_reset(ReaderObj *self)
918918
}
919919

920920
static PyObject *
921-
Reader_iternext(PyObject *op)
921+
Reader_iternext_lock_held(PyObject *op)
922922
{
923923
ReaderObj *self = _ReaderObj_CAST(op);
924924

@@ -985,6 +985,16 @@ Reader_iternext(PyObject *op)
985985
return fields;
986986
}
987987

988+
static PyObject *
989+
Reader_iternext(PyObject *op)
990+
{
991+
PyObject *result;
992+
Py_BEGIN_CRITICAL_SECTION(op);
993+
result = Reader_iternext_lock_held(op);
994+
Py_END_CRITICAL_SECTION();
995+
return result;
996+
}
997+
988998
static void
989999
Reader_dealloc(PyObject *op)
9901000
{
@@ -1303,15 +1313,8 @@ join_append_lineterminator(WriterObj *self)
13031313
return 1;
13041314
}
13051315

1306-
PyDoc_STRVAR(csv_writerow_doc,
1307-
"writerow($self, row, /)\n"
1308-
"--\n\n"
1309-
"Construct and write a CSV record from an iterable of fields.\n"
1310-
"\n"
1311-
"Non-string elements will be converted to string.");
1312-
13131316
static PyObject *
1314-
csv_writerow(PyObject *op, PyObject *seq)
1317+
csv_writerow_lock_held(PyObject *op, PyObject *seq)
13151318
{
13161319
WriterObj *self = _WriterObj_CAST(op);
13171320
DialectObj *dialect = self->dialect;
@@ -1414,6 +1417,23 @@ csv_writerow(PyObject *op, PyObject *seq)
14141417
return result;
14151418
}
14161419

1420+
PyDoc_STRVAR(csv_writerow_doc,
1421+
"writerow($self, row, /)\n"
1422+
"--\n\n"
1423+
"Construct and write a CSV record from an iterable of fields.\n"
1424+
"\n"
1425+
"Non-string elements will be converted to string.");
1426+
1427+
static PyObject *
1428+
csv_writerow(PyObject *op, PyObject *seq)
1429+
{
1430+
PyObject *result;
1431+
Py_BEGIN_CRITICAL_SECTION(op);
1432+
result = csv_writerow_lock_held(op, seq);
1433+
Py_END_CRITICAL_SECTION();
1434+
return result;
1435+
}
1436+
14171437
PyDoc_STRVAR(csv_writerows_doc,
14181438
"writerows($self, rows, /)\n"
14191439
"--\n\n"

0 commit comments

Comments
 (0)