GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GH-139653: Only raise an exception (or fatal error) when the stack pointer is about to overflow the stack. by markshannon · Pull Request #141711 · python/cpython

Conversation

@markshannon

@markshannon markshannon commented Nov 18, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Only raises if the stack pointer is both below the limit and above the stack base. This prevents false positives for user-space threads, as the stack pointer will be outside those bounds if the stack has been swapped.

  • Issue: Python 3.14 stack overflow detection is incompatible with C++ Boost make_fcontext() coroutines #139653

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@markshannon
Only raise an exception (or fatal error) when the stack pointer is ab…
41e9404
…out to overflow the stack.

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.
@markshannon
markshannon requested a review from AA-Turner as a code owner November 18, 2025 11:44
@bedevere-app bedevere-app Bot added the awaiting core review label Nov 18, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Nov 18, 2025
Python 3.14 stack overflow detection is incompatible with C++ Boost make_fcontext() coroutines #139653
Open
@markshannon markshannon added 3.14 bugs and security fixes needs backport to 3.14 bugs and security fixes and removed 3.14 bugs and security fixes labels Nov 18, 2025
encukou
encukou reviewed Nov 18, 2025
View reviewed changes
Comment thread Python/ceval.c
# endif
uintptr_t here_addr = _Py_get_machine_stack_pointer();
uintptr_t top_addr = _Py_SIZE_ROUND_UP(here_addr, 4096);
// Add some space for caller function then round to minimum page size

@encukou encukou Nov 18, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment.
Both “space for caller function” and “minimum page size” are guesses; what happens if they're wrong?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@markshannon markshannon Nov 18, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing bad. We only use the upper limit (for stack growing down) for reporting stack use in case of an overflow.
If we are wrong the report value will be off by a few kb, but it is already an estimate. We are assuming that there isn't much stack above the call to create the thread state.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@encukou encukou Nov 18, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to avoid calculation, and use here_addr as is? With the Py_C_STACK_SIZE guesses, 4k indeed doesn't matter.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@markshannon markshannon Nov 18, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a perfect guess, but I don't see any reason to make it worse.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@encukou encukou Nov 18, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I see is unnecessary complexity: adding 60 bytes of faux precision to a guess that's in the order of megabytes is rather confusing. These look like more numbers to tweak if you get spurious overflow check failures

If this needs to go in, could you add a comment that this guess can be wildly wrong?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@markshannon markshannon Nov 18, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Python/ceval.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@encukou encukou added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 18, 2025
@bedevere-bot

bedevere-bot commented Nov 18, 2025

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @encukou for commit 41e9404 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F141711%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 18, 2025
efimov-mikhail
efimov-mikhail reviewed Nov 18, 2025
View reviewed changes

@efimov-mikhail efimov-mikhail left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noticed some typos.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Include/internal/pycore_ceval.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Include/internal/pycore_ceval.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread InternalDocs/stack_protection.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread InternalDocs/stack_protection.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

encukou
encukou reviewed Nov 18, 2025
View reviewed changes
Comment thread Python/ceval.c Outdated

/* The function _Py_EnterRecursiveCallTstate() only calls _Py_CheckRecursiveCall()
if the recursion_depth reaches recursion_limit. */
if the stack poineter is between the stack base and c_stack_hard_limit. */

@encukou encukou Nov 18, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if the stack poineter is between the stack base and c_stack_hard_limit. */
if the stack pointer is between the stack base and c_stack_hard_limit. */

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Python/ceval.c
# endif
uintptr_t here_addr = _Py_get_machine_stack_pointer();
uintptr_t top_addr = _Py_SIZE_ROUND_UP(here_addr, 4096);
// Add some space for caller function then round to minimum page size

@encukou encukou Nov 18, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I see is unnecessary complexity: adding 60 bytes of faux precision to a guess that's in the order of megabytes is rather confusing. These look like more numbers to tweak if you get spurious overflow check failures

If this needs to go in, could you add a comment that this guess can be wildly wrong?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@markshannon
Fix typo and clarify
1e7ae53
encukou
encukou approved these changes Nov 18, 2025
View reviewed changes

@encukou encukou left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Nov 18, 2025
efimov-mikhail
efimov-mikhail approved these changes Nov 18, 2025
View reviewed changes
chris-eibl
chris-eibl reviewed Nov 18, 2025
View reviewed changes
Comment thread Include/internal/pycore_ceval.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread InternalDocs/stack_protection.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

@markshannon
A couple more typos
1de4a56
@markshannon
markshannon merged commit c25a070 into python:main Nov 19, 2025
52 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Nov 19, 2025

Copy link
Copy Markdown

Thanks @markshannon for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Nov 19, 2025
@markshannon
markshannon deleted the conservative-stack-check branch November 19, 2025 10:16
@miss-islington-app

miss-islington-app Bot commented Nov 19, 2025

Copy link
Copy Markdown

Sorry, @markshannon, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker c25a070759952b13f97ecc37ca2991c2669aee47 3.14
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned markshannon Nov 19, 2025
markshannon added 2 commits November 20, 2025 04:19
@markshannon
Update comment
7dcdf04
@markshannon
Fix typos
b28b230
hroncok pushed a commit to hroncok/cpython that referenced this pull request Nov 24, 2025
@markshannon @hroncok
pythonGH-139653: Only raise an exception (or fatal error) when the st…
30cdcac
…ack pointer is about to overflow the stack. (pythonGH-141711)

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.

(cherry picked from commit c25a070)
@hroncok hroncok mentioned this pull request Nov 24, 2025
[3.14] GH-139653: Only raise an exception (or fatal error) when the stack pointer is about to overflow the stack. (GH-141711) #141892
Closed
@bedevere-app

bedevere-app Bot commented Nov 24, 2025

Copy link
Copy Markdown

GH-141892 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 24, 2025
@vstinner vstinner mentioned this pull request Nov 24, 2025
[3.14] gh-139653: Add PyUnstable_ThreadState_SetStackProtection() (GH-139668) #141661
Merged
encukou pushed a commit to encukou/cpython that referenced this pull request Nov 25, 2025
@markshannon @encukou
[3.14] pythonGH-139653: Only raise an exception (or fatal error) when…
6adfd35
… the stack pointer is about to overflow the stack. (pythonGH-141711)

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.

Cherry-picked from commit c25a070
encukou pushed a commit to encukou/cpython that referenced this pull request Nov 25, 2025
@markshannon @encukou
[3.14] pythonGH-139653: Only raise an exception (or fatal error) when…
0728b62
… the stack pointer is about to overflow the stack. (pythonGH-141711)

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.

Cherry-picked from commit c25a070
@bedevere-app

bedevere-app Bot commented Nov 25, 2025

Copy link
Copy Markdown

GH-141944 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

encukou added a commit that referenced this pull request Nov 26, 2025
@encukou @markshannon
[3.14] GH-139653: Only raise an exception (or fatal error) when the s…
69021e9
…tack pointer is about to overflow the stack. (GH-141711) (GH-141944)

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.

Cherry-picked from commit c25a070

Co-authored-by: Mark Shannon <[email protected]>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
@markshannon @StanFromIreland
pythonGH-139653: Only raise an exception (or fatal error) when the st…
341306c
…ack pointer is about to overflow the stack. (pythonGH-141711)

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.
ashm-dev pushed a commit to ashm-dev/cpython that referenced this pull request Dec 8, 2025
@markshannon @ashm-dev
pythonGH-139653: Only raise an exception (or fatal error) when the st…
a019bca
…ack pointer is about to overflow the stack. (pythonGH-141711)

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@chris-eibl chris-eibl chris-eibl left review comments
@encukou encukou encukou approved these changes
@efimov-mikhail efimov-mikhail efimov-mikhail approved these changes
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@markshannon markshannon

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@markshannon @bedevere-bot @encukou @efimov-mikhail @chris-eibl
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.