@@ -87,8 +87,8 @@ get_posixsubprocess_state(PyObject *module)
87
87
88
88
#define _posixsubprocessstate_global get_posixsubprocess_state(PyState_FindModule(&_posixsubprocessmodule))
89
89
90
- /* If gc was disabled, call gc.enable(). Return 0 on success . */
91
- static int
90
+ /* If gc was disabled, call gc.enable(). Ignore errors . */
91
+ static void
92
92
_enable_gc (int need_to_reenable_gc , PyObject * gc_module )
93
93
{
94
94
PyObject * result ;
@@ -98,15 +98,17 @@ _enable_gc(int need_to_reenable_gc, PyObject *gc_module)
98
98
PyErr_Fetch (& exctype , & val , & tb );
99
99
result = PyObject_CallMethodNoArgs (
100
100
gc_module , _posixsubprocessstate_global -> enable );
101
+ if (result == NULL ) {
102
+ /* We might have created a child process at this point, we
103
+ * we have no good way to handle a failure to reenable GC
104
+ * and return information about the child process. */
105
+ PyErr_Print ();
106
+ }
107
+ Py_XDECREF (result );
101
108
if (exctype != NULL ) {
102
109
PyErr_Restore (exctype , val , tb );
103
110
}
104
- if (result == NULL ) {
105
- return 1 ;
106
- }
107
- Py_DECREF (result );
108
111
}
109
- return 0 ;
110
112
}
111
113
112
114
@@ -774,7 +776,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
774
776
int child_umask ;
775
777
PyObject * cwd_obj , * cwd_obj2 = NULL ;
776
778
const char * cwd ;
777
- pid_t pid ;
779
+ pid_t pid = -1 ;
778
780
int need_to_reenable_gc = 0 ;
779
781
char * const * exec_array , * const * argv = NULL , * const * envp = NULL ;
780
782
Py_ssize_t arg_num , num_groups = 0 ;
@@ -1010,8 +1012,6 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
1010
1012
sigset_t all_sigs ;
1011
1013
sigfillset (& all_sigs );
1012
1014
if ((saved_errno = pthread_sigmask (SIG_BLOCK , & all_sigs , & old_sigs ))) {
1013
- errno = saved_errno ;
1014
- PyErr_SetFromErrno (PyExc_OSError );
1015
1015
goto cleanup ;
1016
1016
}
1017
1017
old_sigmask = & old_sigs ;
@@ -1050,50 +1050,33 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
1050
1050
}
1051
1051
#endif
1052
1052
1053
- Py_XDECREF (cwd_obj2 );
1054
-
1055
1053
if (need_after_fork )
1056
1054
PyOS_AfterFork_Parent ();
1057
- if (envp )
1058
- _Py_FreeCharPArray (envp );
1059
- if (argv )
1060
- _Py_FreeCharPArray (argv );
1061
- _Py_FreeCharPArray (exec_array );
1062
-
1063
- /* Reenable gc in the parent process (or if fork failed). */
1064
- if (_enable_gc (need_to_reenable_gc , gc_module )) {
1065
- pid = -1 ;
1066
- }
1067
- PyMem_RawFree (groups );
1068
- Py_XDECREF (preexec_fn_args_tuple );
1069
- Py_XDECREF (gc_module );
1070
1055
1071
- if (pid == -1 ) {
1056
+ cleanup :
1057
+ if (saved_errno != 0 ) {
1072
1058
errno = saved_errno ;
1073
1059
/* We can't call this above as PyOS_AfterFork_Parent() calls back
1074
1060
* into Python code which would see the unreturned error. */
1075
1061
PyErr_SetFromErrno (PyExc_OSError );
1076
- return NULL ; /* fork() failed. */
1077
1062
}
1078
1063
1079
- return PyLong_FromPid (pid );
1080
-
1081
- cleanup :
1064
+ Py_XDECREF (preexec_fn_args_tuple );
1065
+ PyMem_RawFree (groups );
1082
1066
Py_XDECREF (cwd_obj2 );
1083
1067
if (envp )
1084
1068
_Py_FreeCharPArray (envp );
1069
+ Py_XDECREF (converted_args );
1070
+ Py_XDECREF (fast_args );
1085
1071
if (argv )
1086
1072
_Py_FreeCharPArray (argv );
1087
1073
if (exec_array )
1088
1074
_Py_FreeCharPArray (exec_array );
1089
1075
1090
- PyMem_RawFree (groups );
1091
- Py_XDECREF (converted_args );
1092
- Py_XDECREF (fast_args );
1093
- Py_XDECREF (preexec_fn_args_tuple );
1094
1076
_enable_gc (need_to_reenable_gc , gc_module );
1095
1077
Py_XDECREF (gc_module );
1096
- return NULL ;
1078
+
1079
+ return pid == -1 ? NULL : PyLong_FromPid (pid );
1097
1080
}
1098
1081
1099
1082
0 commit comments