Skip to content

Commit c67dbd4

Browse files
authored
Add some gc safety around _mysql__fetch_row (#348)
Users of gc.get_referrers() could cause a SystemError to be raised if this function is running in a different python thread.
1 parent 18163d7 commit c67dbd4

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Diff for: MySQLdb/_mysql.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -1373,9 +1373,15 @@ _mysql_ResultObject_fetch_row(
13731373
convert_row = row_converters[how];
13741374
if (maxrows) {
13751375
if (!(r = PyTuple_New(maxrows))) goto error;
1376-
rowsadded = _mysql__fetch_row(self, &r, skiprows, maxrows,
1377-
convert_row);
1376+
1377+
// see: https://docs.python.org/3/library/gc.html#gc.get_referrers
1378+
// This function can get a reference to the tuple r, and if that
1379+
// code is preempted while holding a ref to r, the _PyTuple_Resize
1380+
// will raise a SystemError because the ref count is 2.
1381+
PyObject_GC_UnTrack(r);
1382+
rowsadded = _mysql__fetch_row(self, &r, skiprows, maxrows, convert_row);
13781383
if (rowsadded == -1) goto error;
1384+
PyObject_GC_Track(r);
13791385
} else {
13801386
if (self->use) {
13811387
maxrows = 1000;

0 commit comments

Comments
 (0)