This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Implement stack overflow protection for supported platforms
Type: Stage:
Components: Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Mark.Shannon Nosy List: Mark.Shannon, brandtbucher, corona10, gregory.p.smith, ronaldoussoren, steven.daprano
Priority: normal Keywords:

Created on 2022-03-04 16:54 by Mark.Shannon, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg414538 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2022-03-04 16:54
https://github.com/python/steering-council/issues/102 (definitely not PEP 651 ;))

We should implement efficient stack checks on those platforms that allow us to introspect stack extents.
Windows and posix systems allow us to do this.

C allows addresses of stack variables to be taken. This means that C stacks cannot be moved.
In theory they might be discontinuous, although I suspect that they are always contiguous.

My plan is to maintain a per-thread "safe region" of 32or 64k. We can check if the current stack pointer (or near enough) is in that region cheaply.
If we are not in that region, we can ask the O/S for a stack limit to determine a new "safe region". If we cannot find a safe region, then we raise a MemoryError.

Personally I'd prefer a new exception `StackOverflow` to `MemoryError` but, thanks to stackoverflow.com, it is now impossible for new programmers to do a web search to determine what a "stack overflow" is.
So, I guess MemoryError will have to do.
msg414553 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2022-03-04 22:31
> Personally I'd prefer a new exception `StackOverflow` to `MemoryError`

+1 on a new exception (presumably a subclass of MemoryError).

How about using OverflowedStack instead?

The situation is not quite as bad as you suggest. Googling for "stack overflow" alone (with a space and no other qualifications):

* on Bing, scroll halfway down the first page of results to find the "People also ask..." 

  How do you get a stack overflow?
  How to prevent a stack overflow error?

* also on Bing at the bottom of the first page of results is a question on stackoverflow.com asking what causes memory stack overflows;

* on DuckDuckGo, the first page of search results fails to suggest anything useful;

* on Google itself, on the first page is the People Also Ask

  What causes stack overflows?

* as well as a link to Wikipedia's page on stack overflows.

I expect that going forward, "python stack overflow exception" will be sufficient to hit the Python docs somewhere on the first page.

Besides, presumably this OverflowedStack exception is likely to be rare, so I expect that few people will need to google it.
msg415143 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2022-03-14 14:27
issue33955 is an older issue about implementing the current functionality for this on macOS, which has an API for querying stack limits.
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 91079
2022-03-14 14:27:17ronaldoussorensetnosy: + ronaldoussoren
messages: + msg415143
2022-03-04 22:31:03steven.dapranosetnosy: + steven.daprano
messages: + msg414553
2022-03-04 17:02:22corona10setnosy: + corona10
2022-03-04 16:56:56brandtbuchersetnosy: + brandtbucher
2022-03-04 16:54:19Mark.Shannoncreate