need help checking for stack overflow in Win32

Mark Hammond MHammond at skippinet.com.au
Tue Jun 1 19:13:30 EDT 1999


Actually, I was about to post this yesterday, and to prove my point I wrote:

def die():
    die()

Expecting to see the crash.  I was then going to go into a rant about how
win32api.Apply would actually let you catch this (and any other Win32)
exception as a temporary solution.

But it worked correctly - ie, Python raised a RuntimeError: Max Recursion.

So I guessed that 1.5.2 had fixed this, presumably by reducing the recursion
count for Win32.  Apparently not!

Thomas' reply is correct, and indeed a number of years ago I did play with
this.  I even reported my results, but no one seemed interested, so I
dropped it (waiting for someone else to complain :-)

It still seems to me that this is not the ideal good solution.  In my simple
example above, we did not die.  But make anything less contrived, and we
have C frames with larger allocations on the stack, and we will die.
Whatever number we pick, we would need to make it quite a bit lower than the
"normal" case, to handle the fact that some C functions may allocate from
bytes to kilobytes on the stack.

A better mechanism would be to use Win32 exception handlers.  Then you dont
need artificial recursion checks at all - leave it to the OS.  However, it
isnt clear to me where this exception handler should be placed, nor indeed
if setting up a Win32 exception handler inside the VM would have a
significant performance hit.  A plus would be we could also catch Access
Violations (Im not sure what we would do with them - but a traceback would
be nicer than Dr Watson when dealing with buggy extensions!)

Thomas' solution is simple, and requires either a simple source file change,
or using the MSVC GUI to adjust the stack size.  If you would like to
experiment with a Win32 exception handler then I would be happy to see where
that leads us...

Mark.

Guido van Rossum wrote in message <5lk8to3su3.fsf at eric.cnri.reston.va.us>...
>I could use some help from someone with a little time, some knowledge
>of Python internals, and some Win32 C expertise.  On Win32 platforms,
>infinite recursion causes an Application Error (a fatal windows
>error).  There is some machinery in Python to check for this
>condition, but apparently it doesn't work on Win32.  Please help!
>
>--Guido van Rossum (home page: http://www.python.org/~guido/)






More information about the Python-list mailing list