Skip to content

Excessive stack usage for clang-cl debug builds on Windows #157789

Description

@chris-eibl

test.test_call.TestRecursion.test_margin_is_sufficient fails on the AMD64 Windows Server 2025 Clang 3.14 buildbot.

The table lists safe_margin before it is multiplied, because 3.14 and main use different extra percents

cpython/Lib/test/test_call.py

Lines 1105 to 1107 in 5b34363

# Add an (arbitrary) extra 25% for safety
safe_margin = safe_margin * 5 / 4
self.assertLess(safe_margin, _testinternalcapi.get_stack_margin())

and _testinternalcapi.get_stack_margin(), which is actually just _PyOS_STACK_MARGIN_BYTES.

branch compiler build safe_margin _PyOS_STACK_MARGIN_BYTES
main MSC v.1951 debug 20080 32768
main clang-cl 22.1.3 debug 26032 32768
main clang-cl 22.1.3 release 1200 16384
3.14 MSC v.1944 debug 23168 32768
3.14 clang-cl 22.1.3 debug 33920 32768
3.14 clang-cl 22.1.3 release 992 16384
3.14 clang-cl 22.1.3 debug with -Og 1536 32768

Using -Og drastically improves stack usage.

Linked PRs

Activity

  1. self-assigned this
    on Sep 19, 2026
  2. zooba commented on Sep 21, 2026

    @zooba
    Member

    I'm neutral either way on this, but any reason we shouldn't adjust the limits instead of the compiler options?

    Historically, the debug stack size becoming excessive has been a sign that we're doing silly stuff with local variables. Since ceval has been hand-micro-optimised to the extreme, there's not much we can do about it, but sudden jumps are worth treating as a warning sign that something has changed.

  3. dqqFA8995 commented on Sep 21, 2026

    @dqqFA8995
  4. chris-eibl commented on Sep 21, 2026

    @chris-eibl
    MemberAuthor

    Yupp, adjusting _PyOS_STACK_MARGIN_BYTES by increasing 1

    #if (defined(Py_DEBUG) \
    || defined(_Py_ADDRESS_SANITIZER) \
    || defined(_Py_THREAD_SANITIZER)) \
    || defined(_Py_UNDEFINED_BEHAVIOR_SANITIZER)
    # define _PyOS_LOG2_STACK_MARGIN 12

    to 13 would be fine as well. @markshannon wdyt?

    Footnotes

    1. only in case of clang-cl? and maybe only on 3.14? ↩

  5. chris-eibl commented on Sep 22, 2026

    @chris-eibl
    MemberAuthor

    The reason I've chosen the -Og approach is that AFAICT this is almost always used in CI and the buildbots in case of debug builds due to

    cpython/configure.ac

    Lines 2384 to 2386 in a321390

    PYDEBUG_CFLAGS="-O0"
    AS_VAR_IF([ac_cv_cc_supports_og], [yes],
    [PYDEBUG_CFLAGS="-Og"])

    where ac_cv_cc_supports_og (supporting -Og) is true for gcc and clang.

  6. zooba commented on Sep 22, 2026

    @zooba
    Member

    AFAICT this is almost always used in CI and the buildbots in case of debug builds ...

    Ah, compatibility with the Makefile is a good reason.

    Though it's probably also worth making sure that option flows through sysconfig as well (if that variable does, it usually doesn't exist on Windows and so may not be there for clang-cl either, in which case you can ignore it).

  7. chris-eibl commented on Sep 22, 2026

    @chris-eibl
    MemberAuthor

    Building current main on WSL using

    configure --with-pydebug
    

    yields -Og in CFLAGS

    $ ./python -c 'import sysconfig;print(sysconfig.get_config_vars()["CFLAGS"])'
    -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer  -fno-strict-overflow -Wsign-compare -g -Og -Wall
    
    and a lot more vars that contain -Og;

    LIBEXPAT_CFLAGS
    LIBHACL_CFLAGS
    MODULE__BLAKE2_CFLAGS
    MODULE__HMAC_CFLAGS
    MODULE__MD5_CFLAGS
    MODULE__SHA1_CFLAGS
    MODULE__SHA2_CFLAGS
    MODULE__SHA3_CFLAGS
    OPT
    PY_BUILTIN_MODULE_CFLAGS
    PY_CFLAGS
    PY_CORE_CFLAGS
    PY_STDMODULE_CFLAGS
    

    But for Windows none of them exist:

    pprint.pprint(sysconfig.get_config_vars())
    {'ABIFLAGS': '_d',
     'BINDIR': 'E:\\cpython_clang\\PCbuild\\amd64',
     'BINLIBDEST': 'E:\\cpython_clang\\Lib',
     'EXE': '.exe',
     'EXT_SUFFIX': '_d.cp316-win_amd64.pyd',
     'INCLUDEPY': 'E:\\cpython_clang\\Include',
     'LDLIBRARY': 'python316_d.dll',
     'LIBDEST': 'E:\\cpython_clang\\Lib',
     'LIBDIR': 'E:\\cpython_clang\\libs',
     'LIBRARY': 'python316_d.dll',
     'Py_DEBUG': 1,
     'Py_GIL_DISABLED': 0,
     'SOABI': 'cp316-win_amd64',
     'TZPATH': '',
     'VERSION': '316',
     'VPATH': '..\\..',
     'abi_thread': '',
     'abiflags': '',
     'base': 'E:\\cpython_clang',
     'exec_prefix': 'E:\\cpython_clang',
     'implementation': 'Python',
     'implementation_lower': 'python',
     'installed_base': 'E:\\cpython_clang',
     'installed_platbase': 'E:\\cpython_clang',
     'platbase': 'E:\\cpython_clang',
     'platlibdir': 'DLLs',
     'prefix': 'E:\\cpython_clang',
     'projectbase': 'E:\\cpython_clang',
     'py_version': '3.16.0a0',
     'py_version_nodot': '316',
     'py_version_nodot_plat': '316',
     'py_version_short': '3.16',
     'srcdir': 'E:\\cpython_clang',
     'userbase': 'C:\\Users\\suc\\AppData\\Roaming\\Python'}
    

    The closest might be 'Py_DEBUG': 1. Is there any other place in sysconfig to look for compiler flags in case of Windows?

  8. zooba commented on Sep 22, 2026

    @zooba
    Member

    The closest might be 'Py_DEBUG': 1.

    Yeah, not really close enough.

    Is there any other place in sysconfig to look for compiler flags in case of Windows?

    No, and generally speaking anything that might get built on Windows is going to be ABI compatible without specific compiler settings, so they aren't necessary (the debug/non-debug CRT issue isn't really ABI-related, but in any case all the choices are viable so we couldn't really provide them anyway).

    I think you're justified in just setting the argument without updating anything else.

  9. chris-eibl commented on Oct 4, 2026

    @chris-eibl
    MemberAuthor

    Is #157791 good to merge then? Thoguh I'd feel better with an approval, cc @zware.

  10. added 2 commits that reference this issue on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions