Skip to content

Add some gc safety around _mysql__fetch_row #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 5, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions MySQLdb/_mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -1284,9 +1284,15 @@ _mysql__fetch_row(
if (!self->use)
row = mysql_fetch_row(self->result);
else {
// 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these code to _mysql_ResultObject_fetch_row.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its pretty safe everywhere else we do it in the dangerous area where we allow threads.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is safe untrack soon after creatin, and track again after _PyTuple_Resize().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, convert_row may execute Python code.
Execute python code means it allows threads switching, Calling gc module, etc.

Py_BEGIN_ALLOW_THREADS is not the only danger API. There are many danger APIs
which can execute any Python code. e.g. Py_DECREF, PyObject_RichCompareBool,
PyList_Append, etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping?

Py_BEGIN_ALLOW_THREADS;
row = mysql_fetch_row(self->result);
Py_END_ALLOW_THREADS;
PyObject_GC_Track(*r);
}
if (!row && mysql_errno(&(((_mysql_ConnectionObject *)(self->conn))->connection))) {
_mysql_Exception((_mysql_ConnectionObject *)self->conn);
Expand Down