[Stackless] Windows SEH problems and patch

Richard Tew richard.m.tew at gmail.com
Tue Mar 27 14:16:23 CEST 2007


This email is just to record a patch which should be looked at for
integration with the later versions of Python, most likely by myself.
Keep in mind that here at CCP we still use 2.3 and the version of
Stackless which that had for anyone who takes a look at the patch.

What follows are Kristján Valur's notes on the problem for context,
and the attached patch solves the problem.

Until now, we have not been compiling our code with C exceptions
enabled.  But now with VS2005, it is all but impossible to disable.

On windows, each thread has a Thread Information Block, accessible
through the fs register:

typedef struct _NT_TIB {
    PVOID StackBase;
    PVOID StackLimit;
    PVOID SubSystemTib;
    union {
        PVOID FiberData;
        DWORD Version;
    PVOID ArbitraryUserPointer;
    struct _NT_TIB *Self;

typedef NT_TIB *PNT_TIB;

 it appears that C code register exception unwinding handlers in the

I have a strange error on startup now and I think it is due to this:
C code that is switching stacks (by using PyChannel_Send(), etc)

will keep the same ExceptionList linked list of handlers after the
stack switch, even though it makes no sense on the new stack.

Also, code that has registered a number of handlers, (through
recursion) may context switch to a different stack, which then

Proceeds to unwind, but will unwind the wrong handlers.  Clearly, each
stack should have its own linked list of exception handlers.

 My idea would be to create a new TIB, which is a copy of the current
TIB when a new tasklet is created (well, on the first hard switch.)
In the new tasklet,

I would set up top level SEH (structured exception handlers) using the
new TIB for the duration of the tasklet, and would then

Switch the fs register contents when there is a hard switch.

 I just am not certain where I should install my SEH handler.
Normally this is done with code like

__try {
__catch (filter()) {

 But this means that I must guard a subroutine call…  I really don't
want to start to manually insert handlers using compiler intrinsic…
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ms_seh_23.patch
Type: application/octet-stream
Size: 2311 bytes
Desc: not available
URL: <http://www.stackless.com/pipermail/stackless/attachments/20070327/1281c6e0/attachment.obj>
-------------- next part --------------
Stackless mailing list
Stackless at stackless.com

More information about the Stackless mailing list