@@ -26,6 +26,11 @@ inputs:
2626 venv :
2727 description : Directory for the venv. Defaults to a new directory under RUNNER_TEMP.
2828 default : " "
29+ stack-reserve :
30+ description : >-
31+ Windows only. Raise the base interpreter's main-thread stack reserve to at
32+ least this many bytes. Empty (default) leaves the interpreter unchanged.
33+ default : " "
2934
3035outputs :
3136 python-path :
@@ -123,3 +128,41 @@ runs:
123128 PYTHON : ${{ steps.venv.outputs.python-path }}
124129 run : |
125130 "${PYTHON}" -m pip install -U pip
131+
132+ - name : Raise the interpreter's stack reserve
133+ if : runner.os == 'Windows' && inputs.stack-reserve != ''
134+ shell : bash --noprofile --norc -xeuo pipefail {0}
135+ env :
136+ PYTHON : ${{ steps.venv.outputs.python-path }}
137+ STACK_RESERVE : ${{ inputs.stack-reserve }}
138+ run : |
139+ # sys.stdout.write avoids a trailing CR from Windows Python's CRLF.
140+ base="$("${PYTHON}" -c 'import sys; sys.stdout.write(sys._base_executable)')"
141+ # Windows cannot overwrite a running executable. Prepare a copy and
142+ # replace the original after the patching interpreter exits.
143+ uv run --no-project --isolated --with pefile==2024.8.26 --python "${base}" python - "${base}" "${STACK_RESERVE}" <<'EOF'
144+ import sys
145+
146+ import pefile
147+
148+ path, reserve = sys.argv[1], int(sys.argv[2])
149+ if reserve <= 0:
150+ raise ValueError("stack-reserve must be a positive number of bytes")
151+ with pefile.PE(path) as pe:
152+ pe.OPTIONAL_HEADER.SizeOfStackReserve = max(pe.OPTIONAL_HEADER.SizeOfStackReserve, reserve)
153+ pe.write(path + ".stack-reserve")
154+ EOF
155+ mv -f -- "${base}.stack-reserve" "${base}"
156+ # Verify the running interpreter's stack, rather than just its header.
157+ "${base}" - "${STACK_RESERVE}" <<'EOF'
158+ import ctypes
159+ import sys
160+
161+ lo, hi = ctypes.c_size_t(), ctypes.c_size_t()
162+ get_limits = ctypes.windll.kernel32.GetCurrentThreadStackLimits
163+ get_limits.argtypes = [ctypes.POINTER(ctypes.c_size_t), ctypes.POINTER(ctypes.c_size_t)]
164+ get_limits.restype = None
165+ get_limits(ctypes.byref(lo), ctypes.byref(hi))
166+ print("main-thread stack reserve:", hi.value - lo.value)
167+ sys.exit(hi.value - lo.value < int(sys.argv[1]))
168+ EOF
0 commit comments