-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Incomplete tuple created by PyTuple_New() and accessed via the GC can trigged a crash #59313
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
Comments
Hi, Sporadically, while running an sqlite3 query, the above error is seen. if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
Py_XDECREF(newitem);
/*
* Temp: Bug XYZ Generate core so that we can debug
*
* PyErr_BadInternalCall();
* return -1;
*/
char errmsg[200];
sprintf(errmsg, "Bug XYZ: PyTuple_Check(op) = %d "
"op->ob_refcnt = %d; see core\n", PyTuple_Check(op),
op->ob_refcnt);
Py_FatalError(errmsg);
} This generates a core with the following bt. Showing the top few frames only: (gdb) p *(PyTupleObject *)row 'row' was allocated via PyTuple_New() Is this a known issue? If not, what can I do to debug. Thanks, |
Could you possibly reproduce this in 2.7, 3.2 and/or "default" (future 3.3)?. Python 2.6 support is over. |
sorry, 2.7, 3.2 is not an option currently but I am hoping someone can |
I believe I have found the root-cause for this issue. It is occurring due to the use of the garbage collector in another “memMonitor” thread (we run it periodically to get stats on objects, track mem leaks, etc). Since _pysqlite_fetch_one_row() releases the GIL before calling PyTuple_SetItem(), if the memMonitor is scheduled to run and, say, calls gc.get_objects(), it increments the refcount on all tracked objects (via append_objects()->PyList_Append()->app1()->PY_INCREF()). I have stack traces to confirm. This seems to rule out the use of gc methods (such as get_objects(), get_referrers/referents()) in multi-threaded programs or have them handle SystemError arising from such usage. Agree? |
Thanks for the analysis! Here is a simple script inspired from the original bug; PySequence_Tuple() uses PyTuple_SET_ITEM() which is a macro without the ob_refcnt check, but we can make it call _PyTuple_Resize() and fail there. All versions of CPython are affected: import gc
TAG = object()
def monitor():
lst = [x for x in gc.get_referrers(TAG)
if isinstance(x, tuple)]
t = lst[0] # this *is* the result tuple
print(t) # full of nulls !
return t # Keep it alive for some time
def my_iter():
yield TAG # 'tag' gets stored in the result tuple
t = monitor()
for x in range(10):
yield x # SystemError when the tuple needs to be resized
tuple(my_iter()) |
I wonder why _pysqlite_fetch_one_row() releases the GIL around sqlite3_column_type(). By its name, it doesn't sound like an expensive function. Another workaround would be to call PyTuple_SET_ITEM instead of PyTuple_SetItem. |
Wondering the same thing myself, and yes sqlite3_column_type() by itself doesn't seem expensive. I assumed in general it was to allow more responsiveness for apps with huge number of columns (i.e. large tuple size). But we have about 20-25 columns and so I was going to try removing it and seeing the Re PyTuple_SET_ITEM...yes that's also a possibility but it would then hide genuine bugs. |
The GIL should be released:
Well, as long as your monitor only increfs the tuple and doesn't mutate |
I may be seeing this running tracd-1.1.2b1 on python 2.7.7 |
What is tracd doing? This issue should only appear when calling one of the debugging functions in the gc module, AFAICT. |
Still happening in 3.10: Python 3.10.0a5+ (heads/master:bf2e7e55d7, Feb 11 2021, 23:09:25) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import gc
>>> TAG = object()
>>>
>>> def monitor():
... lst = [x for x in gc.get_referrers(TAG)
... if isinstance(x, tuple)]
... t = lst[0] # this *is* the result tuple
... print(t) # full of nulls !
... return t # Keep it alive for some time
...
>>> def my_iter():
... yield TAG # 'tag' gets stored in the result tuple
... t = monitor()
... for x in range(10):
... yield x # SystemError when the tuple needs to be resized
...
>>> tuple(my_iter())
(<object object at 0x00000217225091B0>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SystemError: C:\Users\User\src\cpython-dev\Objects\tupleobject.c:963: bad argument to internal function
>>> |
The general issue here is a the PyTuple_New() is unsafe: it immediately tracks the newly created tuple in the GC, whereas the tuple is not initialized yet. If the GIL is released before the tuple is fully populated and something access to this tuple via the GC (ex: gc.get_objects()), accessing the tuple can crash, especially in the Python land (for example, repr(the_tuple) is likely to crash). IMO the unsafe PyTuple_New() API should be avoided. For example, allocate an array of PyObject* on the stack memory, and then call _PyTuple_FromArray(). This API is safe because it only tracks the tuple once it's fully initialized, and it calls INCREF on items. Problem: this safe and efficient API is currently private. There are other safe alternatives like Py_BuildValue("(OOO)", item1, item2, item3). _pysqlite_fetch_one_row() calls PyTuple_New() and releases the GIL at each sqlite3_column_type() call, so yeah, it has this exact bug. By the way, it doesn't check for PyTuple_SetItem() failure, whereas it's currently possible that there is more than one strong reference to the tuple which is being populated (because of the GC issue). PyTuple_New() is ok-ish if there is no way to trigger a GC collection and if the GIL cannot be released until the tuple is fully initialized. Maybe we need a private _PyTuple_NewUntracked() API to create a tuple which is not tracked by the GC, and also a _PyTuple_ResizeUntracked() API. By the way, _PyTuple_Resize() sounds like a nonsense since a tuple is supposed to be immutable ;-) |
Is not that simple, there are other APIs that track the tuple as _PyTuple_Resize. This problem also is not unique to tuples, although is mainly prominent in them. We have this warning in the docs:
That's because *by thesign* these APIs can potentially access half-initialized objects. I don't know if is worth to add a new API just for tuples, given that this problem happens with many other objects |
That's a lot slower unfortunately |
It can happen even without releasing the GIL: A new tuple is created, then some other object is created using the CAPI, the gc runs, the callback triggers (or the tuplevisit method is invoked) and then kaboom |
Ah sorry, I forgot PyTuple_Pack(3, item1, item2, item3) which should be very efficient. This function is also safe: only track the tuple when it is fully initialized.
PyList_New() is also affected. Do you think about other types? PyDict_New() and PySet_New() create empty containers and so are ok. |
Any C extension class that implements a new_whatever() method that leaves the class tracked and not ready. |
I'm not aware of such C extension but they likely exists. If we have such extensions in the stdlib, we can try to fix them. We cannot fix such GC bugs in third party code, and I don't think that we can hack the GC to work around this issue neither. |
Should we still fix sqlite3, or wait for an agreement on python/issues-test-cpython#24510? |
I suggest to let's all agree on how to fix this on the bigger scale first. |
Unless there is a simple, reliable, cheap, and universal solution at hand, consider closing this. Given how long PyTuple_New() has exist, it doesn't seem to be much of a problem in the real world. Historically, we punted on "crashers" involving either gc.get_referrers or byte code hacks. Both of those reach inside Python's black box, allowing disruption of otherwise sensible invariants. |
I mainly agree on closing this issue as we already have a warning about this behaviour in gc.get_referrers and other friends. On the other hand I am still a bit afraid of a crash that could happen if the GC does a pass and one of these tuples is in an inconsistent state. Is possible that this crashes Python without the user calling ever any function from the GC module. I need to do some investigation around this, but I agree that this is a separate issue, so I will open a new one if it happens to be relevant. |
I'm pretty sure the problem is not only with
That sounds like a crash waiting to happen to me. You can get away with it if the GC happens to not run until the class becomes ready. |
In #107183, Guido points out that for tuples, they are pre-filled with NULLs and |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: