Skip to content

Commit

Permalink
Add some gc safety around _mysql__fetch_row (#348)
Browse files Browse the repository at this point in the history
Users of gc.get_referrers() could cause a SystemError to be raised if this function is running in a different python thread.
  • Loading branch information
fried authored Feb 5, 2020
1 parent 18163d7 commit c67dbd4
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions MySQLdb/_mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -1373,9 +1373,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;
Expand Down

0 comments on commit c67dbd4

Please sign in to comment.