Skip to content

Commit 2c14939

Browse files
committed
Make sure datastack memory is freed after fork.
1 parent 0091341 commit 2c14939

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

Objects/frameobject.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,16 +630,18 @@ frame_dealloc(PyFrameObject *f)
630630
PyCodeObject *co = f->f_code;
631631

632632
/* Kill all local variables */
633-
if (f->f_own_locals_memory) {
633+
if (f->f_localsptr) {
634634
for (int i = 0; i < co->co_nlocalsplus+FRAME_SPECIALS_SIZE; i++) {
635635
Py_CLEAR(f->f_localsptr[i]);
636636
}
637637
/* Free items on stack */
638638
for (int i = 0; i < f->f_stackdepth; i++) {
639639
Py_XDECREF(f->f_valuestack[i]);
640640
}
641-
PyMem_Free(f->f_localsptr);
642-
f->f_own_locals_memory = 0;
641+
if (f->f_own_locals_memory) {
642+
PyMem_Free(f->f_localsptr);
643+
f->f_own_locals_memory = 0;
644+
}
643645
}
644646
f->f_stackdepth = 0;
645647
Py_XDECREF(f->f_back);

Python/ceval.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5179,7 +5179,6 @@ _PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
51795179
*/
51805180
assert (!is_coro);
51815181
assert(f->f_own_locals_memory == 0);
5182-
assert(f->f_stackdepth == 0);
51835182
if (Py_REFCNT(f) > 1) {
51845183
Py_DECREF(f);
51855184
_PyObject_GC_TRACK(f);
@@ -5188,10 +5187,11 @@ _PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
51885187
}
51895188
}
51905189
else {
5190+
++tstate->recursion_depth;
5191+
f->f_localsptr = NULL;
51915192
for (int i = 0; i < code->co_nlocalsplus + FRAME_SPECIALS_SIZE; i++) {
51925193
Py_XDECREF(localsarray[i]);
51935194
}
5194-
++tstate->recursion_depth;
51955195
Py_DECREF(f);
51965196
--tstate->recursion_depth;
51975197
}

Python/pystate.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,13 @@ PyThreadState_Clear(PyThreadState *tstate)
897897
if (tstate->on_delete != NULL) {
898898
tstate->on_delete(tstate->on_delete_data);
899899
}
900+
_PyStackChunk *chunk = tstate->datastack_chunk;
901+
tstate->datastack_chunk = NULL;
902+
while (chunk != NULL) {
903+
_PyStackChunk *prev = chunk->previous;
904+
_PyObject_VirtualFree(chunk, chunk->size);
905+
chunk = prev;
906+
}
900907
}
901908

902909

@@ -941,7 +948,6 @@ _PyThreadState_Delete(PyThreadState *tstate, int check_current)
941948
}
942949
}
943950
tstate_delete_common(tstate, gilstate);
944-
_PyObject_VirtualFree(tstate->datastack_chunk, tstate->datastack_chunk->size);
945951
PyMem_RawFree(tstate);
946952
}
947953

0 commit comments

Comments
 (0)