diff --git a/Include/internal/pycore_jit.h b/Include/internal/pycore_jit.h index a7041ef8d4b000..b96ac879289673 100644 --- a/Include/internal/pycore_jit.h +++ b/Include/internal/pycore_jit.h @@ -25,6 +25,7 @@ typedef _Py_CODEUNIT *(*jit_func)( int _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction *trace, size_t length); void _PyJIT_Free(_PyExecutorObject *executor); +void _PyJIT_Fini(void); #endif // _Py_JIT diff --git a/Include/internal/pycore_opcode_metadata.h b/Include/internal/pycore_opcode_metadata.h index 351cf56355b7d0..08bddfbfbe6619 100644 --- a/Include/internal/pycore_opcode_metadata.h +++ b/Include/internal/pycore_opcode_metadata.h @@ -1341,7 +1341,7 @@ _PyOpcode_macro_expansion[256] = { [BINARY_OP_SUBSCR_LIST_INT] = { .nuops = 5, .uops = { { _GUARD_TOS_INT, OPARG_SIMPLE, 0 }, { _GUARD_NOS_LIST, OPARG_SIMPLE, 0 }, { _BINARY_OP_SUBSCR_LIST_INT, OPARG_SIMPLE, 5 }, { _POP_TOP_INT, OPARG_SIMPLE, 5 }, { _POP_TOP, OPARG_SIMPLE, 5 } } }, [BINARY_OP_SUBSCR_LIST_SLICE] = { .nuops = 3, .uops = { { _GUARD_TOS_SLICE, OPARG_SIMPLE, 0 }, { _GUARD_NOS_LIST, OPARG_SIMPLE, 0 }, { _BINARY_OP_SUBSCR_LIST_SLICE, OPARG_SIMPLE, 5 } } }, [BINARY_OP_SUBSCR_STR_INT] = { .nuops = 5, .uops = { { _GUARD_TOS_INT, OPARG_SIMPLE, 0 }, { _GUARD_NOS_UNICODE, OPARG_SIMPLE, 0 }, { _BINARY_OP_SUBSCR_STR_INT, OPARG_SIMPLE, 5 }, { _POP_TOP_INT, OPARG_SIMPLE, 5 }, { _POP_TOP, OPARG_SIMPLE, 5 } } }, - [BINARY_OP_SUBSCR_TUPLE_INT] = { .nuops = 3, .uops = { { _GUARD_TOS_INT, OPARG_SIMPLE, 0 }, { _GUARD_NOS_TUPLE, OPARG_SIMPLE, 0 }, { _BINARY_OP_SUBSCR_TUPLE_INT, OPARG_SIMPLE, 5 } } }, + [BINARY_OP_SUBSCR_TUPLE_INT] = { .nuops = 5, .uops = { { _GUARD_TOS_INT, OPARG_SIMPLE, 0 }, { _GUARD_NOS_TUPLE, OPARG_SIMPLE, 0 }, { _BINARY_OP_SUBSCR_TUPLE_INT, OPARG_SIMPLE, 5 }, { _POP_TOP_INT, OPARG_SIMPLE, 5 }, { _POP_TOP, OPARG_SIMPLE, 5 } } }, [BINARY_OP_SUBTRACT_FLOAT] = { .nuops = 5, .uops = { { _GUARD_TOS_FLOAT, OPARG_SIMPLE, 0 }, { _GUARD_NOS_FLOAT, OPARG_SIMPLE, 0 }, { _BINARY_OP_SUBTRACT_FLOAT, OPARG_SIMPLE, 5 }, { _POP_TOP_FLOAT, OPARG_SIMPLE, 5 }, { _POP_TOP_FLOAT, OPARG_SIMPLE, 5 } } }, [BINARY_OP_SUBTRACT_INT] = { .nuops = 5, .uops = { { _GUARD_TOS_INT, OPARG_SIMPLE, 0 }, { _GUARD_NOS_INT, OPARG_SIMPLE, 0 }, { _BINARY_OP_SUBTRACT_INT, OPARG_SIMPLE, 5 }, { _POP_TOP_INT, OPARG_SIMPLE, 5 }, { _POP_TOP_INT, OPARG_SIMPLE, 5 } } }, [BINARY_SLICE] = { .nuops = 1, .uops = { { _BINARY_SLICE, OPARG_SIMPLE, 0 } } }, diff --git a/Include/internal/pycore_uop_ids.h b/Include/internal/pycore_uop_ids.h index 204210ff101efe..69f1c3829cd239 100644 --- a/Include/internal/pycore_uop_ids.h +++ b/Include/internal/pycore_uop_ids.h @@ -387,7 +387,7 @@ extern "C" { #define _BINARY_OP_SUBSCR_LIST_INT_r23 580 #define _BINARY_OP_SUBSCR_LIST_SLICE_r21 581 #define _BINARY_OP_SUBSCR_STR_INT_r23 582 -#define _BINARY_OP_SUBSCR_TUPLE_INT_r21 583 +#define _BINARY_OP_SUBSCR_TUPLE_INT_r23 583 #define _BINARY_OP_SUBTRACT_FLOAT_r03 584 #define _BINARY_OP_SUBTRACT_FLOAT_r13 585 #define _BINARY_OP_SUBTRACT_FLOAT_r23 586 diff --git a/Include/internal/pycore_uop_metadata.h b/Include/internal/pycore_uop_metadata.h index f751f642b81ff8..0a49231e53f44a 100644 --- a/Include/internal/pycore_uop_metadata.h +++ b/Include/internal/pycore_uop_metadata.h @@ -122,7 +122,7 @@ const uint32_t _PyUop_Flags[MAX_UOP_ID+1] = { [_BINARY_OP_SUBSCR_STR_INT] = HAS_DEOPT_FLAG, [_GUARD_NOS_TUPLE] = HAS_EXIT_FLAG, [_GUARD_TOS_TUPLE] = HAS_EXIT_FLAG, - [_BINARY_OP_SUBSCR_TUPLE_INT] = HAS_DEOPT_FLAG | HAS_ESCAPES_FLAG, + [_BINARY_OP_SUBSCR_TUPLE_INT] = HAS_DEOPT_FLAG, [_GUARD_NOS_DICT] = HAS_EXIT_FLAG, [_GUARD_TOS_DICT] = HAS_EXIT_FLAG, [_BINARY_OP_SUBSCR_DICT] = HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, @@ -1155,7 +1155,7 @@ const _PyUopCachingInfo _PyUop_Caching[MAX_UOP_ID+1] = { .entries = { { -1, -1, -1 }, { -1, -1, -1 }, - { 1, 2, _BINARY_OP_SUBSCR_TUPLE_INT_r21 }, + { 3, 2, _BINARY_OP_SUBSCR_TUPLE_INT_r23 }, { -1, -1, -1 }, }, }, @@ -3453,7 +3453,7 @@ const uint16_t _PyUop_Uncached[MAX_UOP_REGS_ID+1] = { [_GUARD_TOS_TUPLE_r11] = _GUARD_TOS_TUPLE, [_GUARD_TOS_TUPLE_r22] = _GUARD_TOS_TUPLE, [_GUARD_TOS_TUPLE_r33] = _GUARD_TOS_TUPLE, - [_BINARY_OP_SUBSCR_TUPLE_INT_r21] = _BINARY_OP_SUBSCR_TUPLE_INT, + [_BINARY_OP_SUBSCR_TUPLE_INT_r23] = _BINARY_OP_SUBSCR_TUPLE_INT, [_GUARD_NOS_DICT_r02] = _GUARD_NOS_DICT, [_GUARD_NOS_DICT_r12] = _GUARD_NOS_DICT, [_GUARD_NOS_DICT_r22] = _GUARD_NOS_DICT, @@ -3969,7 +3969,7 @@ const char *const _PyOpcode_uop_name[MAX_UOP_REGS_ID+1] = { [_BINARY_OP_SUBSCR_STR_INT] = "_BINARY_OP_SUBSCR_STR_INT", [_BINARY_OP_SUBSCR_STR_INT_r23] = "_BINARY_OP_SUBSCR_STR_INT_r23", [_BINARY_OP_SUBSCR_TUPLE_INT] = "_BINARY_OP_SUBSCR_TUPLE_INT", - [_BINARY_OP_SUBSCR_TUPLE_INT_r21] = "_BINARY_OP_SUBSCR_TUPLE_INT_r21", + [_BINARY_OP_SUBSCR_TUPLE_INT_r23] = "_BINARY_OP_SUBSCR_TUPLE_INT_r23", [_BINARY_OP_SUBTRACT_FLOAT] = "_BINARY_OP_SUBTRACT_FLOAT", [_BINARY_OP_SUBTRACT_FLOAT_r03] = "_BINARY_OP_SUBTRACT_FLOAT_r03", [_BINARY_OP_SUBTRACT_FLOAT_r13] = "_BINARY_OP_SUBTRACT_FLOAT_r13", diff --git a/Lib/multiprocessing/spawn.py b/Lib/multiprocessing/spawn.py index daac1ecc34b55e..d43864c939cb63 100644 --- a/Lib/multiprocessing/spawn.py +++ b/Lib/multiprocessing/spawn.py @@ -184,7 +184,7 @@ def get_preparation_data(name): sys_argv=sys.argv, orig_dir=process.ORIGINAL_DIR, dir=os.getcwd(), - start_method=get_start_method(), + start_method=get_start_method(allow_none=True), ) # Figure out whether to initialise main in the subprocess as a module diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index d03eb1dfb253ec..c8c386101a0669 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -5967,6 +5967,26 @@ def test_context(self): self.assertRaises(ValueError, ctx.set_start_method, None) self.check_context(ctx) + @staticmethod + def _dummy_func(): + pass + + @warnings_helper.ignore_fork_in_thread_deprecation_warnings() + def test_spawn_dont_set_context(self): + # Run a process with spawn or forkserver context may change + # the global start method, see gh-109263. + for method in ('fork', 'spawn', 'forkserver'): + multiprocessing.set_start_method(None, force=True) + + try: + ctx = multiprocessing.get_context(method) + except ValueError: + continue + process = ctx.Process(target=self._dummy_func) + process.start() + process.join() + self.assertIsNone(multiprocessing.get_start_method(allow_none=True)) + def test_context_check_module_types(self): try: ctx = multiprocessing.get_context('forkserver') diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index 16288a447e20fe..8f7314d579df6f 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -3114,6 +3114,25 @@ def testfunc(n): self.assertNotIn("_POP_TOP_INT", uops) self.assertIn("_POP_TOP_NOP", uops) + def test_binary_subscr_tuple_int(self): + def testfunc(n): + t = (1,) + x = 0 + for _ in range(n): + y = t[0] + x += y + return x + + res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) + self.assertEqual(res, TIER2_THRESHOLD) + self.assertIsNotNone(ex) + uops = get_opnames(ex) + + self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops) + self.assertNotIn("_POP_TOP", uops) + self.assertNotIn("_POP_TOP_INT", uops) + self.assertIn("_POP_TOP_NOP", uops) + def test_143026(self): # https://github.com/python/cpython/issues/143026 diff --git a/Misc/NEWS.d/next/Library/2025-06-22-18-57-19.gh-issue-109263.f92V95.rst b/Misc/NEWS.d/next/Library/2025-06-22-18-57-19.gh-issue-109263.f92V95.rst new file mode 100644 index 00000000000000..6b96b5b9b2a0de --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-22-18-57-19.gh-issue-109263.f92V95.rst @@ -0,0 +1,2 @@ +Starting a process from spawn context in :mod:`multiprocessing` no longer +sets the start method globally. diff --git a/Python/bytecodes.c b/Python/bytecodes.c index d21c17d072c3f9..950b9f08f2ead1 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -975,9 +975,9 @@ dummy_func( } macro(BINARY_OP_SUBSCR_TUPLE_INT) = - _GUARD_TOS_INT + _GUARD_NOS_TUPLE + unused/5 + _BINARY_OP_SUBSCR_TUPLE_INT; + _GUARD_TOS_INT + _GUARD_NOS_TUPLE + unused/5 + _BINARY_OP_SUBSCR_TUPLE_INT + _POP_TOP_INT + POP_TOP; - op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res)) { + op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res, ts, ss)) { PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st); PyObject *tuple = PyStackRef_AsPyObjectBorrow(tuple_st); @@ -991,9 +991,10 @@ dummy_func( STAT_INC(BINARY_OP, hit); PyObject *res_o = PyTuple_GET_ITEM(tuple, index); assert(res_o != NULL); - PyStackRef_CLOSE_SPECIALIZED(sub_st, _PyLong_ExactDealloc); res = PyStackRef_FromPyObjectNew(res_o); - DECREF_INPUTS(); + ts = tuple_st; + ss = sub_st; + INPUTS_DEAD(); } op(_GUARD_NOS_DICT, (nos, unused -- nos, unused)) { diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index f64747d6f27f2a..dc21a4aa90cf46 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -4888,12 +4888,14 @@ break; } - case _BINARY_OP_SUBSCR_TUPLE_INT_r21: { + case _BINARY_OP_SUBSCR_TUPLE_INT_r23: { CHECK_CURRENT_CACHED_VALUES(2); assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE()); _PyStackRef sub_st; _PyStackRef tuple_st; _PyStackRef res; + _PyStackRef ts; + _PyStackRef ss; _PyStackRef _stack_item_0 = _tos_cache0; _PyStackRef _stack_item_1 = _tos_cache1; sub_st = _stack_item_1; @@ -4920,23 +4922,13 @@ STAT_INC(BINARY_OP, hit); PyObject *res_o = PyTuple_GET_ITEM(tuple, index); assert(res_o != NULL); - PyStackRef_CLOSE_SPECIALIZED(sub_st, _PyLong_ExactDealloc); res = PyStackRef_FromPyObjectNew(res_o); - stack_pointer[0] = tuple_st; - stack_pointer += 1; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyStackRef tmp = tuple_st; - tuple_st = res; - stack_pointer[-1] = tuple_st; - PyStackRef_CLOSE(tmp); - stack_pointer = _PyFrame_GetStackPointer(frame); + ts = tuple_st; + ss = sub_st; + _tos_cache2 = ss; + _tos_cache1 = ts; _tos_cache0 = res; - _tos_cache1 = PyStackRef_ZERO_BITS; - _tos_cache2 = PyStackRef_ZERO_BITS; - SET_CURRENT_CACHED_VALUES(1); - stack_pointer += -1; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); + SET_CURRENT_CACHED_VALUES(3); assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE()); break; } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index baf199969de94e..5f326d5c3caffc 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1024,6 +1024,8 @@ _PyStackRef tuple_st; _PyStackRef sub_st; _PyStackRef res; + _PyStackRef ts; + _PyStackRef ss; // _GUARD_TOS_INT { value = stack_pointer[-1]; @@ -1067,15 +1069,24 @@ STAT_INC(BINARY_OP, hit); PyObject *res_o = PyTuple_GET_ITEM(tuple, index); assert(res_o != NULL); - PyStackRef_CLOSE_SPECIALIZED(sub_st, _PyLong_ExactDealloc); res = PyStackRef_FromPyObjectNew(res_o); + ts = tuple_st; + ss = sub_st; + } + // _POP_TOP_INT + { + value = ss; + assert(PyLong_CheckExact(PyStackRef_AsPyObjectBorrow(value))); + PyStackRef_CLOSE_SPECIALIZED(value, _PyLong_ExactDealloc); + } + // _POP_TOP + { + value = ts; + stack_pointer[-2] = res; stack_pointer += -1; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); _PyFrame_SetStackPointer(frame, stack_pointer); - _PyStackRef tmp = tuple_st; - tuple_st = res; - stack_pointer[-1] = tuple_st; - PyStackRef_CLOSE(tmp); + PyStackRef_XCLOSE(value); stack_pointer = _PyFrame_GetStackPointer(frame); } DISPATCH(); diff --git a/Python/jit.c b/Python/jit.c index 4ce90edf73a8cb..5ca9313aadfb30 100644 --- a/Python/jit.c +++ b/Python/jit.c @@ -151,6 +151,8 @@ typedef struct { uintptr_t instruction_starts[UOP_MAX_TRACE_LENGTH]; } jit_state; +static size_t _Py_jit_shim_size = 0; + // Warning! AArch64 requires you to get your hands dirty. These are your gloves: // value[value_start : value_start + len] @@ -676,6 +678,7 @@ _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction trace[], siz * We compile this once only as it effectively a normal * function, but we need to use the JIT because it needs * to understand the jit-specific calling convention. + * Don't forget to call _PyJIT_Fini later! */ static _PyJitEntryFuncPtr compile_shim(void) @@ -717,6 +720,7 @@ compile_shim(void) jit_free(memory, total_size); return NULL; } + _Py_jit_shim_size = total_size; return (_PyJitEntryFuncPtr)memory; } @@ -739,6 +743,7 @@ _Py_LazyJitShim( return _Py_jit_entry(executor, frame, stack_pointer, tstate); } +// Free executor's memory allocated with _PyJIT_Compile void _PyJIT_Free(_PyExecutorObject *executor) { @@ -754,4 +759,22 @@ _PyJIT_Free(_PyExecutorObject *executor) } } +// Free shim memory allocated with compile_shim +void +_PyJIT_Fini(void) +{ + PyMutex_Lock(&lazy_jit_mutex); + unsigned char *memory = (unsigned char *)_Py_jit_entry; + size_t size = _Py_jit_shim_size; + if (size) { + _Py_jit_entry = _Py_LazyJitShim; + _Py_jit_shim_size = 0; + if (jit_free(memory, size)) { + PyErr_FormatUnraisable("Exception ignored while " + "freeing JIT entry code"); + } + } + PyMutex_Unlock(&lazy_jit_mutex); +} + #endif // _Py_JIT diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index b40b597643dc94..c53a2fb7570c0d 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -335,7 +335,7 @@ dummy_func(void) { i = sub_st; } - op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res)) { + op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res, ts, ss)) { assert(sym_matches_type(tuple_st, &PyTuple_Type)); if (sym_is_const(ctx, sub_st)) { assert(PyLong_CheckExact(sym_get_const(ctx, sub_st))); @@ -354,6 +354,8 @@ dummy_func(void) { else { res = sym_new_not_null(ctx); } + ts = tuple_st; + ss = sub_st; } op(_TO_BOOL, (value -- res)) { diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index a17a5688847e07..9f1337e7ef3b92 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -836,6 +836,8 @@ JitOptRef sub_st; JitOptRef tuple_st; JitOptRef res; + JitOptRef ts; + JitOptRef ss; sub_st = stack_pointer[-1]; tuple_st = stack_pointer[-2]; assert(sym_matches_type(tuple_st, &PyTuple_Type)); @@ -855,9 +857,13 @@ else { res = sym_new_not_null(ctx); } - CHECK_STACK_BOUNDS(-1); + ts = tuple_st; + ss = sub_st; + CHECK_STACK_BOUNDS(1); stack_pointer[-2] = res; - stack_pointer += -1; + stack_pointer[-1] = ts; + stack_pointer[0] = ss; + stack_pointer += 1; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); break; } diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 2527dca71d774e..45b585faf9c980 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -35,6 +35,9 @@ #include "pycore_uniqueid.h" // _PyObject_FinalizeUniqueIdPool() #include "pycore_warnings.h" // _PyWarnings_InitState() #include "pycore_weakref.h" // _PyWeakref_GET_REF() +#ifdef _Py_JIT +#include "pycore_jit.h" // _PyJIT_Fini() +#endif #include "opcode.h" @@ -2267,6 +2270,7 @@ _Py_Finalize(_PyRuntimeState *runtime) /* Print debug stats if any */ _PyEval_Fini(); + /* Flush sys.stdout and sys.stderr (again, in case more was printed) */ if (flush_std_files() < 0) { status = -1; @@ -2346,6 +2350,10 @@ _Py_Finalize(_PyRuntimeState *runtime) finalize_interp_clear(tstate); +#ifdef _Py_JIT + /* Free JIT shim memory */ + _PyJIT_Fini(); +#endif #ifdef Py_TRACE_REFS /* Display addresses (& refcnts) of all objects still alive. diff --git a/Tools/tsan/suppressions_free_threading.txt b/Tools/tsan/suppressions_free_threading.txt index e8b1501c34bfc1..e2cf6d58b0cfd9 100644 --- a/Tools/tsan/suppressions_free_threading.txt +++ b/Tools/tsan/suppressions_free_threading.txt @@ -26,3 +26,6 @@ thread:pthread_create # PyObject_Realloc internally does memcpy which isn't atomic so can race # with non-locking reads. See #132070 race:PyObject_Realloc + +# gh-143100: set_swap_bodies in setobject.c calls memcpy, which isn't atomic +race:set_swap_bodies