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
Add a MemoryError singleton to fix an unlimited loop when the memory is exhausted #64034
Comments
Under very low memory condition, PyErr_NoMemory() or PyErr_NormalizeException() enters an unlimited loop when the free list of MemoryError becomes empty. I propose to add a MemoryError read-only singleton to fix this corner case. Attributes cannot be modified, new attributes cannot be added. MemoryError attributes values:
PyException_SetTraceback(), PyException_SetCause() and PyException_SetContext() do nothing when called on the MemoryError singleton. A MemoryError can be raised on an integer overflow when computing the size of a memory block. In this case, you may still have available free memory and so you may be able to build a traceback object, chain exceptions, etc. If you consider that attributes must be modifiable in this case, we can keep the MemoryError unchanged and keep the free list, but add a new special "ReadOnlyMemoryError" type which has exactly one instance, preallocated at startup (the singleton). Read-only attributes are required to not keep references to objects when the MemoryError (singleton) is ne more used ("destroyed"): see for example test_memory_error_cleanup() of test_exceptions. Python has already a PyExc_RecursionErrorInst singleton, which is used on recursion error. It is just a preallocated RuntimeError error, attributes are modifiable. Does this exception keep a traceback when it is no more used ("destroyed")? Maybe using read-only attributes is overkill? Replacing the free list with a singleton is enough? -- See also issue bpo-5437: before Python had a MemoryError singleton, but it was replaced by a free list to not keep references. changeset: 65690:c6d86439aa91 ........ Issue bpo-5437: A preallocated MemoryError instance should not hold traceback -- Another option is maybe to clear frames of the traceback. Issue bpo-17934 added a clear() method to frame objects for that. |
I got this bug when I worked on the issue bpo-19817 which adds an arbitary limit to memory allocations using tracemalloc. I used this limit on the Python test suite to check how Python behaves under very low memory condition. |
The real question is why the free list becomes empty. Either way, I don't think a read-only singleton is a good idea. It may be simpler to call Py_FatalError in such cases. |
Victor, do you still want to champion this, or shall we close it? |
Serhiy recently worked on MemoryError, maybe he wants to work on this issue? I'm no more interested to work on this issue. |
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:
The text was updated successfully, but these errors were encountered: