4747#define POSIX_CALL (call ) do { if ((call) == -1) goto error; } while (0)
4848
4949
50- /* Given the gc module call gc.enable() and return 0 on success. */
50+ /* If gc was disabled, call gc.enable(). Return 0 on success. */
5151static int
52- _enable_gc (PyObject * gc_module )
52+ _enable_gc (int need_to_reenable_gc , PyObject * gc_module )
5353{
5454 PyObject * result ;
5555 _Py_IDENTIFIER (enable );
56+ PyObject * exctype , * val , * tb ;
5657
57- result = _PyObject_CallMethodId (gc_module , & PyId_enable , NULL );
58- if (result == NULL )
59- return 1 ;
60- Py_DECREF (result );
58+ if (need_to_reenable_gc ) {
59+ PyErr_Fetch (& exctype , & val , & tb );
60+ result = _PyObject_CallMethodId (gc_module , & PyId_enable , NULL );
61+ if (exctype != NULL ) {
62+ PyErr_Restore (exctype , val , tb );
63+ }
64+ if (result == NULL ) {
65+ return 1 ;
66+ }
67+ Py_DECREF (result );
68+ }
6169 return 0 ;
6270}
6371
@@ -698,6 +706,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
698706 && _PyImport_ReleaseLock () < 0 && !PyErr_Occurred ()) {
699707 PyErr_SetString (PyExc_RuntimeError ,
700708 "not holding the import lock" );
709+ pid = -1 ;
701710 }
702711 import_lock_held = 0 ;
703712#endif
@@ -710,9 +719,8 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
710719 _Py_FreeCharPArray (exec_array );
711720
712721 /* Reenable gc in the parent process (or if fork failed). */
713- if (need_to_reenable_gc && _enable_gc (gc_module )) {
714- Py_XDECREF (gc_module );
715- return NULL ;
722+ if (_enable_gc (need_to_reenable_gc , gc_module )) {
723+ pid = -1 ;
716724 }
717725 Py_XDECREF (preexec_fn_args_tuple );
718726 Py_XDECREF (gc_module );
@@ -736,14 +744,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
736744 Py_XDECREF (converted_args );
737745 Py_XDECREF (fast_args );
738746 Py_XDECREF (preexec_fn_args_tuple );
739-
740- /* Reenable gc if it was disabled. */
741- if (need_to_reenable_gc ) {
742- PyObject * exctype , * val , * tb ;
743- PyErr_Fetch (& exctype , & val , & tb );
744- _enable_gc (gc_module );
745- PyErr_Restore (exctype , val , tb );
746- }
747+ _enable_gc (need_to_reenable_gc , gc_module );
747748 Py_XDECREF (gc_module );
748749 return NULL ;
749750}
0 commit comments