[Python-Dev] stack check on Unix: any suggestions?

M.-A. Lemburg mal@lemburg.com
Wed, 30 Aug 2000 12:42:39 +0200


Fredrik Lundh wrote:
> 
> mal wrote:
> > See my comments in the patch manager... the patch looks fine
> > except for two things: getrlimit() should be tested for
> > usability in the configure script and the call frequency
> > of PyOS_CheckStack() should be lowered to only use it for
> > potentially recursive programs.
> 
> the latter would break windows and mac versions of Python,
> where Python can run on very small stacks (not to mention
> embedded systems...)
> 
> for those platforms, CheckStack is designed to work with an
> 8k safety margin (PYOS_STACK_MARGIN)

Ok, I don't mind calling it every ten levels deep, but I'd
rather not have it start at level 0. The reason is
that many programs probably don't make much use of
recursion anyway and have a maximum call depth of around
10-50 levels (Python programs usually using shallow class hierarchies).
These programs should not be bothered by calling PyOS_CheckStack()
all the time. Recursive programs will easily reach the 100 mark -- 
those should call PyOS_CheckStack often enough to notice the 
stack problems.

So the check would look something like this:

if (tstate->recursion_depth >= 50 &&
    tstate->recursion_depth%10 == 0 &&
    PyOS_CheckStack()) {
                PyErr_SetString(PyExc_MemoryError, "Stack overflow");
                return NULL;
        }

> :::
> 
> one way to address this is to introduce a scale factor, so that
> you can add checks based on the default 8k limit, but auto-
> magically apply them less often platforms where the safety
> margin is much larger...
> 
> /* checkstack, but with a "scale" factor */
> #if windows or mac
> /* default safety margin */
> #define PYOS_CHECKSTACK(v, n)\
>     (((v) % (n) == 0) && PyOS_CheckStack())
> #elif linux
> /* at least 10 times the default safety margin */
> #define PYOS_CHECKSTACK(v, n)\
>     (((v) % ((n)*10) == 0) && PyOS_CheckStack())
> #endif
> 
>  if (PYOS_CHECKSTACK(tstate->recursion_depth, 10)
>     ...

I'm not exactly sure how large the safety margin is with
Martin's patch, but this seems a good idea.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/