Skip to content

Commit

Permalink
Fix asyncpg with Py_DEBUG mode (#719)
Browse files Browse the repository at this point in the history
If Py_DEBUG enabled, then newly allocated memory is filled with the byte 
0xCD (CLEANBYTE) https://docs.python.org/3/c-api/memory.html#c.PyMem_SetupDebugHooks

This breaks checks for `pointer == NULL` and results in crash.

From documentation PyObject_GC_Track 
https://docs.python.org/3/c-api/gcsupport.html#c.PyObject_GC_Track:

> This should be called once all the fields followed by the tp_traverse 
> handler become valid, usually near the end of the constructor.
  • Loading branch information
shadchin authored Apr 26, 2021
1 parent fa2c1e5 commit a113d90
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion asyncpg/protocol/record/recordobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ApgRecord_New(PyTypeObject *type, PyObject *desc, Py_ssize_t size)
{
ApgRecordObject *o;
Py_ssize_t i;
int need_gc_track = 0;

if (size < 0 || desc == NULL || !ApgRecordDesc_CheckExact(desc)) {
PyErr_BadInternalCall();
Expand All @@ -54,7 +55,7 @@ ApgRecord_New(PyTypeObject *type, PyObject *desc, Py_ssize_t size)
}
}

PyObject_GC_Track(o);
need_gc_track = 1;
} else {
assert(PyType_IsSubtype(type, &ApgRecord_Type));

Expand All @@ -78,6 +79,9 @@ ApgRecord_New(PyTypeObject *type, PyObject *desc, Py_ssize_t size)
Py_INCREF(desc);
o->desc = (ApgRecordDescObject*)desc;
o->self_hash = -1;
if (need_gc_track) {
PyObject_GC_Track(o);
}
return (PyObject *) o;
}

Expand Down

0 comments on commit a113d90

Please sign in to comment.