Skip to content
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

Fix remaining memory bugs #443

Merged
merged 18 commits into from
Oct 10, 2024
Merged

Conversation

Xmader
Copy link
Member

@Xmader Xmader commented Sep 24, 2024

The CI sometimes fail on main even after we released the "stable" PythonMonkey v1.0.

After all pytests are done, the program segfaults during the Python interpreter finalization.

============================= 678 passed in 23.59s =============================
/Users/runner/work/_temp/cab3e84e-a831-4a41-9c31-79102f1651a5.sh: line 7:  5781 Segmentation fault: 11  (core dumped) poetry run python -m pytest tests/python

Debugged using a debug build of Python

… should return `undefined` instead of throwing a Python error
We cannot use `Py_AtExit(cleanup);` because the GIL is unavailable after Python finalization, no more Python APIs can be called.
…the GIL is already handed over to another thread
…ython

Otherwise a `_PyObject_AssertFailed` error will be raised

See: `_PyUnicode_CheckConsistency` https://github.com/python/cpython/blob/v3.11.3/Objects/unicodeobject.c#L594-L600, #L552-L553
…thod with the simpler `JS_EncodeStringToUTF8` SpiderMonkey API
`PyList_SetItem` steals the reference, so we must increase the reference count by 1
WIP: I don't know exactly why...
@Xmader Xmader marked this pull request as ready for review September 24, 2024 11:38
@Xmader Xmader marked this pull request as draft September 24, 2024 11:50
@Xmader
Copy link
Member Author

Xmader commented Sep 24, 2024

* thread #1, name = 'python', stop reason = signal SIGSEGV: address not mapped to object
  * frame #0: 0x0000ffff97f6acf8 libpython3.11.so.1.0`PyObject_GC_Del [inlined] gc_list_remove(node=0x0000ffff9124d2b0) at gcmodule.c:269:5
    frame #1: 0x0000ffff97f6acf8 libpython3.11.so.1.0`PyObject_GC_Del(op=0x0000ffff9124d2c0) at gcmodule.c:2350:9
    frame #2: 0x0000ffff97f02d34 libpython3.11.so.1.0`maybe_freelist_clear(interp=0x0000ffff982706d0, fini=0) at tupleobject.c:1251:13
    frame #3: 0x0000ffff9800cee0 libpython3.11.so.1.0`gc_collect_main [inlined] clear_freelists(interp=0x0000ffff982706d0) at gcmodule.c:1037:5
    frame #4: 0x0000ffff9800ced8 libpython3.11.so.1.0`gc_collect_main(tstate=0x0000ffff9828aa50, generation=2, n_collected=0x0000000000000000, n_uncollectable=0x0000000000000000, nofail=1) at gcmodule.c:1313:9
    frame #5: 0x0000ffff9800d5a4 libpython3.11.so.1.0`_PyGC_CollectNoFail(tstate=<unavailable>) at gcmodule.c:2110:9
    frame #6: 0x0000ffff97ffd2a0 libpython3.11.so.1.0`Py_FinalizeEx at pylifecycle.c:1833:5
    frame #7: 0x0000ffff9800b788 libpython3.11.so.1.0`Py_RunMain at main.c:682:9
    frame #8: 0x0000ffff9800b468 libpython3.11.so.1.0`Py_BytesMain(argc=<unavailable>, argv=<unavailable>) at main.c:734:12
    frame #9: 0x0000ffff97bb73fc libc.so.6`___lldb_unnamed_symbol2962 + 108
    frame #10: 0x0000ffff97bb74cc libc.so.6`__libc_start_main + 152
    frame #11: 0x0000aaaade3e08b0 python3.11`_start + 48

src/PyListProxyHandler.cc Show resolved Hide resolved
src/modules/pythonmonkey/pythonmonkey.cc Outdated Show resolved Hide resolved
@zollqir zollqir self-requested a review September 24, 2024 19:16
Copy link
Collaborator

@zollqir zollqir left a comment

Choose a reason for hiding this comment

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

Looks good so far, let me know when this is ready to merge and I can give it another pass

python/pythonmonkey/require.py Outdated Show resolved Hide resolved
src/JSObjectProxy.cc Show resolved Hide resolved
src/modules/pythonmonkey/pythonmonkey.cc Show resolved Hide resolved
src/PyListProxyHandler.cc Outdated Show resolved Hide resolved
Xmader and others added 4 commits October 1, 2024 14:36
Since each call of `PyList_SetItem` steals a reference (even if its to the same object), we need multiple references to it for it to steal

Co-authored-by: Caleb Aikens <caleb@distributive.network>
…nger usable/useful after the context is destroyed
…hon 3.11

Something is double-free-ed during the final finalization:  in a debug build of Python, `./Include/object.h:602: _Py_NegativeRefcount: Assertion failed: object has negative ref count`

In non-debug build of Python, it simply segfaults at the end during finalization.
@Xmader Xmader marked this pull request as ready for review October 1, 2024 21:01
Base automatically changed from Xmader/feat/python-3.13-support to main October 10, 2024 16:32
Copy link
Collaborator

@wesgarland wesgarland left a comment

Choose a reason for hiding this comment

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

LGTM

@wesgarland wesgarland merged commit 38309a3 into main Oct 10, 2024
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants