]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-116738: Make csv module thread-safe (gh-141365) (gh-141825)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 21 Nov 2025 16:47:13 +0000 (17:47 +0100)
committerGitHub <noreply@github.com>
Fri, 21 Nov 2025 16:47:13 +0000 (16:47 +0000)
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.
(cherry picked from commit fb26d9c2ef739cbfdc134da5ab89470511f1f5fd)

Co-authored-by: Alper <alperyoney@fb.com>
Lib/test/test_free_threading/test_csv.py [new file with mode: 0644]
Misc/NEWS.d/next/Core_and_Builtins/2025-11-10-00-14-20.gh-issue-116738.IxliC_.rst [new file with mode: 0644]
Modules/_csv.c

diff --git a/Lib/test/test_free_threading/test_csv.py b/Lib/test/test_free_threading/test_csv.py
new file mode 100644 (file)
index 0000000..beb4510
--- /dev/null
@@ -0,0 +1,50 @@
+import csv
+import io
+import unittest
+
+from test.support import threading_helper
+from test.support.threading_helper import run_concurrently
+
+
+NTHREADS = 10
+
+
+@threading_helper.requires_working_threading()
+class TestCSV(unittest.TestCase):
+    def test_concurrent_reader_next(self):
+        input_rows = [f"{i},{i},{i}" for i in range(50)]
+        input_stream = io.StringIO("\n".join(input_rows))
+        reader = csv.reader(input_stream)
+        output_rows = []
+
+        def read_row():
+            for row in reader:
+                self.assertEqual(len(row), 3)
+                output_rows.append(",".join(row))
+
+        run_concurrently(worker_func=read_row, nthreads=NTHREADS)
+        self.assertSetEqual(set(input_rows), set(output_rows))
+
+    def test_concurrent_writer_writerow(self):
+        output_stream = io.StringIO()
+        writer = csv.writer(output_stream)
+        row_per_thread = 10
+        expected_rows = []
+
+        def write_row():
+            for i in range(row_per_thread):
+                writer.writerow([i, i, i])
+                expected_rows.append(f"{i},{i},{i}")
+
+        run_concurrently(worker_func=write_row, nthreads=NTHREADS)
+
+        # Rewind to the start of the stream and parse the rows
+        output_stream.seek(0)
+        output_rows = [line.strip() for line in output_stream.readlines()]
+
+        self.assertEqual(len(output_rows), NTHREADS * row_per_thread)
+        self.assertListEqual(sorted(output_rows), sorted(expected_rows))
+
+
+if __name__ == "__main__":
+    unittest.main()
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-11-10-00-14-20.gh-issue-116738.IxliC_.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-11-10-00-14-20.gh-issue-116738.IxliC_.rst
new file mode 100644 (file)
index 0000000..8b08bcc
--- /dev/null
@@ -0,0 +1,2 @@
+Make csv module thread-safe on the :term:`free threaded <free threading>`
+build.
index 87be7a8f1fb13692ec88ac2d58fbc6941185add9..1f41976e95fdb1889ece2befa6c420c89c87ab63 100644 (file)
@@ -918,7 +918,7 @@ parse_reset(ReaderObj *self)
 }
 
 static PyObject *
-Reader_iternext(PyObject *op)
+Reader_iternext_lock_held(PyObject *op)
 {
     ReaderObj *self = _ReaderObj_CAST(op);
 
@@ -985,6 +985,16 @@ err:
     return fields;
 }
 
+static PyObject *
+Reader_iternext(PyObject *op)
+{
+    PyObject *result;
+    Py_BEGIN_CRITICAL_SECTION(op);
+    result = Reader_iternext_lock_held(op);
+    Py_END_CRITICAL_SECTION();
+    return result;
+}
+
 static void
 Reader_dealloc(PyObject *op)
 {
@@ -1303,15 +1313,8 @@ join_append_lineterminator(WriterObj *self)
     return 1;
 }
 
-PyDoc_STRVAR(csv_writerow_doc,
-"writerow($self, row, /)\n"
-"--\n\n"
-"Construct and write a CSV record from an iterable of fields.\n"
-"\n"
-"Non-string elements will be converted to string.");
-
 static PyObject *
-csv_writerow(PyObject *op, PyObject *seq)
+csv_writerow_lock_held(PyObject *op, PyObject *seq)
 {
     WriterObj *self = _WriterObj_CAST(op);
     DialectObj *dialect = self->dialect;
@@ -1414,6 +1417,23 @@ csv_writerow(PyObject *op, PyObject *seq)
     return result;
 }
 
+PyDoc_STRVAR(csv_writerow_doc,
+"writerow($self, row, /)\n"
+"--\n\n"
+"Construct and write a CSV record from an iterable of fields.\n"
+"\n"
+"Non-string elements will be converted to string.");
+
+static PyObject *
+csv_writerow(PyObject *op, PyObject *seq)
+{
+    PyObject *result;
+    Py_BEGIN_CRITICAL_SECTION(op);
+    result = csv_writerow_lock_held(op, seq);
+    Py_END_CRITICAL_SECTION();
+    return result;
+}
+
 PyDoc_STRVAR(csv_writerows_doc,
 "writerows($self, rows, /)\n"
 "--\n\n"