diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 3c846274..7811d3ba 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -1343,9 +1343,15 @@ _mysql_ResultObject_fetch_row( convert_row = row_converters[how]; if (maxrows) { if (!(r = PyTuple_New(maxrows))) goto error; - rowsadded = _mysql__fetch_row(self, &r, skiprows, maxrows, - convert_row); + + // see: https://docs.python.org/3/library/gc.html#gc.get_referrers + // This function can get a reference to the tuple r, and if that + // code is preempted while holding a ref to r, the _PyTuple_Resize + // will raise a SystemError because the ref count is 2. + PyObject_GC_UnTrack(r); + rowsadded = _mysql__fetch_row(self, &r, skiprows, maxrows, convert_row); if (rowsadded == -1) goto error; + PyObject_GC_Track(r); } else { if (self->use) { maxrows = 1000;