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
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
7ea7a4c
fix: `Py_XDECREF` requires the GIL to be held, but it's unavailable a…
Xmader Sep 24, 2024
3453696
fix: accessing a non-existent property on a Python `bytes` in JS land…
Xmader Sep 24, 2024
dd0aaed
fix: clean up SpiderMonkey when the PythonMonkey module gets destroyed
Xmader Sep 24, 2024
a0e3b05
fix: to fix memory corruption, use `PyUnicode_AsUTF8AndSize` with the…
Xmader Sep 24, 2024
667abc3
fix: `PyUnicode_AsUTF8` needs a strict Python str object (not a subtype)
Xmader Sep 24, 2024
c804ca9
fix: to fix memory corruption, use `PyUnicode_AsUTF8AndSize` with the…
Xmader Sep 24, 2024
7c74274
fix: `PyEventLoop`'s destructor should not use any Python API, after …
Xmader Sep 24, 2024
73957c0
fix: `PyUnicodeObject` needs to be well-formed in a debug build of C…
Xmader Sep 24, 2024
5a13901
fix: the code argument to `python.exec`/`eval` needs to be a well-for…
Xmader Sep 24, 2024
8e365a7
refactor: replace the use of our own roundtrip `StrType::getValue` me…
Xmader Sep 24, 2024
0eeda35
fix: properly handle the reference count when doing `list.concat()`
Xmader Sep 24, 2024
c63b609
perf: simply do a pythonic `result = list[:]` to get a copy of all it…
Xmader Sep 24, 2024
9474990
fix the reference count
Xmader Sep 24, 2024
a1f11b4
Merge branch 'Xmader/feat/python-3.13-support' into Xmader/fix/fix-me…
Xmader Oct 1, 2024
dda2916
fix: reference count for `array_fill`
Xmader Oct 1, 2024
5b9deec
set `GLOBAL_CX = null` in the final cleanup function since it's no lo…
Xmader Oct 1, 2024
a4762ae
fix the reference count for dicts `test_get_default_not_found` in Pyt…
Xmader Oct 1, 2024
38309a3
Merge branch 'main' into Xmader/fix/fix-mem-bugs-using-debug-build
wesgarland Oct 10, 2024
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
19 changes: 18 additions & 1 deletion src/modules/pythonmonkey/pythonmonkey.cc
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,20 @@ PyTypeObject JSObjectItemsProxyType = {
};

static void cleanup() {
// Clean up the PythonMonkey module
Py_XDECREF(PythonMonkey_Null);
Py_XDECREF(PythonMonkey_BigInt);

// Clean up SpiderMonkey
delete autoRealm;
delete global;
if (GLOBAL_CX) JS_DestroyContext(GLOBAL_CX);
Xmader marked this conversation as resolved.
Show resolved Hide resolved
delete JOB_QUEUE;
JS_ShutDown();
}
static void cleanup(PyObject *) {
cleanup();
}

static PyObject *collect(PyObject *self, PyObject *args) {
JS_GC(GLOBAL_CX);
Expand Down Expand Up @@ -550,7 +558,6 @@ PyMODINIT_FUNC PyInit_pythonmonkey(void)
PyErr_SetString(SpiderMonkeyError, "Spidermonkey could not be initialized.");
return NULL;
}
Py_AtExit(cleanup);

GLOBAL_CX = JS_NewContext(JS::DefaultHeapMaxBytes);
if (!GLOBAL_CX) {
Expand Down Expand Up @@ -643,6 +650,16 @@ PyMODINIT_FUNC PyInit_pythonmonkey(void)
if (pyModule == NULL)
return NULL;

// Clean up SpiderMonkey when the PythonMonkey module gets destroyed (module.___cleanup is GCed)
// The `cleanup` function will be called automatically when this PyCapsule gets GCed
// We cannot use `Py_AtExit(cleanup);` because the GIL is unavailable after Python finalization, no more Python APIs can be called.
PyObject *autoDestructor = PyCapsule_New(&pythonmonkey, NULL, cleanup);
if (PyModule_AddObject(pyModule, "___cleanup", autoDestructor) < 0) {
Py_DECREF(autoDestructor);
Py_DECREF(pyModule);
return NULL;
}

Py_INCREF(&NullType);
if (PyModule_AddObject(pyModule, "null", (PyObject *)&NullType) < 0) {
Py_DECREF(&NullType);
Expand Down
Loading