boost::context and SEH

I have been using boost::context library to implement coroutines in my project since this library was officially released with boost 1.52. We desperately needed an option to replace win32 fibers (there is always at least a couple of points to justify such move), and boost::context came in just right.

It all worked fine until lately, when we discovered a very odd thing that was happening only on Windows Server 2008. If an exception was raised from within the context, the application was immideately closed by the OS. Even though that exception was handled in place.

As it turned, Windows Server 2008 has so-called SEH overwrite protection mechanism enabled by default. In shirt, every time exception is raised, the OS first checks that nobody messed with exception registration pointers. Each thread, or, broadly speaking, each parallel stack has its own exception registration information (pointer to which appears to be on the top of the thread info block or at FS:[0]). That registration record contains a pointer to exception handler and a pointer to the previous record in a chain (so, you can have a list of exception handlers). When user code wants to raise an exception, SEHOP protection mechanism of Windows Server 2008 walks back this list to make sure it always ends with a handler provided by the OS itself.

And here is the problem – with boost::context library you end up with parallel execution stacks, each of which having different thread information blocks (TIBs). And, while this library correctly initialized the TIB, in version 1.52 it didn’t bother replicating list of handlers from the original thread. Basically, it’s prev pointer was pointing somewere but definitely not where OS would expect it to point. It does not like it at all, and bang, your process is shot.

This SEHOP mechanism is explained in details on technet, and a lot of useful infomration about SEH could be found on microsoft website.

Apparently, a number of people implementing custom coroutine mechanism run into this issue before. Boost 1.53 has updated and fixed version of boost::context library that addresses exactly this problem.

BTW, SEHOP is included in Vista and Windows 7, is disabled by default, but can be enabled. List of known issues is quite impressive though:

After you enable SEHOP, existing versions of Cygwin, Skype, and Armadillo-protected applications may not work correctly.

  1. I have stumbled upon the same problem: when an exception is thrown in a context, does not matter if handled or unhandled the application crashes. You wrote that this issue was fixed in boost 1.53. I use 1.57, but still I have the same problem. Is there maybe some special define to activate the fix?

    • G’day,
      well, this exact issue should have been fixed. I no longer work in the company where I investigated that problem but I think we verified the fix worked on 2008 server.
      On other windows versions exceptions work just fine as long as they are caught inside the context. I used exception mechanism extensively and haven’t had a single problem.
      Setting up a trap for all uncaught exceptions in the coroutine on Windows involves a bit of trickery but it’s relatively simple.

  2. Thanks for the answer! I have solved the problem another way: for each new context I want to create I start a new thread. Then I park this thread in a boost context. So during the normal operation I have such a context which came from a thread where the TIBs are also fine. When I want to dispose the context then I just simply switch the parked thread back to it and it automatically rewinds the stack too. Then the parking context can be thrown away without any problem.

    • Wow. That’s a hell of a workaround!
      But I suspect your problem worth reporting to Oliver (maintainer of boost::context), it could well be that you have stumbled upon yet another “undocumented feature”.
      BTW, what windows version are you on and did you enable any special option in compiler flags? I’d love to try it myself.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

10 visitors online now
3 guests, 7 bots, 0 members
Max visitors today: 17 at 10:59 am UTC
This month: 17 at 03-02-2021 10:59 am UTC
This year: 52 at 02-17-2021 03:45 am UTC
All time: 332 at 11-22-2019 03:23 am UTC