Thread overview
"catch" not catching the correct exception
Jul 26, 2018
Shachar Shemesh
Jul 26, 2018
rikki cattermole
Jul 26, 2018
Shachar Shemesh
Jul 26, 2018
Seb
Jul 26, 2018
Shachar Shemesh
Jul 26, 2018
kinke
July 26, 2018
Under mecca, we're using GC free exceptions.

I have code that uses mkEx (https://github.com/weka-io/mecca/blob/master/src/mecca/lib/exception.d#L307). Essentially, it constructs an exception in place inside a fiber-local buffer. The construction code seems correct, and the parent seems set correctly (Exception).

When thrown, however, a catch for Exception does not catch it. It is caught if we try to catch Throwable, and also if we try to catch FiberInterrupt (a class deriving directly from Throwable).

Thinking the buffer might have gotten corrupted by one of the scope exits, I've added the following code:

    catch (Exception ex) {
        // Code that should have run goes here
    }
    catch(Throwable ex) {
        META!"XXX NONONONONO caught %s of type %s son of %s"(cast(void*)ex, ex.classinfo.name, ex.classinfo.base.name);
        LOG_EXCEPTION(ex);
        throw ex;
    }

When run, I get the following log:
META XXX NONONONONO caught 0x00007F1C616F39B0 of type weka.bucket.exceptions.NotBucketLeader son of object.Exception


It seems immediately obvious that the problem is not with using a static buffer for the exception (at least, it does not seem to be corrupted), but something else. Somehow, the catch matching does not work.

At which point I'm stuck. I don't know how D's catch matching works, so I don't know where to continue looking.

The problem does not happen consistently, but does happen frequently enough for me to be able to reproduce it when needed.

I need suggestions on how to debug this.

Thank you,
Shachar
July 26, 2018
Hmm, sounds like the vtable and hence TypeInfo part of the reference is getting corrupted.

Have you checked that the type matches where you throw it?
July 26, 2018
On Thursday, 26 July 2018 at 05:52:51 UTC, Shachar Shemesh wrote:
> At which point I'm stuck. I don't know how D's catch matching works, so I don't know where to continue looking.

https://github.com/dlang/druntime/blob/master/src/rt/dwarfeh.d

You still use druntime, right?
July 26, 2018
On 26/07/18 09:22, rikki cattermole wrote:
> Hmm, sounds like the vtable and hence TypeInfo part of the reference is getting corrupted.
> 
> Have you checked that the type matches where you throw it?

Is that what "classinfo" returns? Because if so, it's printed by the logs I pasted in my question, and is correct (and, in any case, shows that Exception is the parent).

Shachar
July 26, 2018
On 26/07/18 10:31, Seb wrote:
> On Thursday, 26 July 2018 at 05:52:51 UTC, Shachar Shemesh wrote:
>> At which point I'm stuck. I don't know how D's catch matching works, so I don't know where to continue looking.
> 
> https://github.com/dlang/druntime/blob/master/src/rt/dwarfeh.d
> 
> You still use druntime, right?

Yes.

/****************************************
 * Called when fibers switch contexts.
 * Params:
 *      newContext = stack to switch to
 * Returns:
 *      previous value of stack
 */
extern(C) void* _d_eh_swapContextDwarf(void* newContext) nothrow @nogc
{
    auto old = ExceptionHeader.stack;
    ExceptionHeader.stack = cast(ExceptionHeader*)newContext;
    return old;
}

Mecca doesn't call that. Should it? Can that be the problem?

Shachar
July 26, 2018
On Thursday, 26 July 2018 at 07:38:08 UTC, Shachar Shemesh wrote:
> Mecca doesn't call that. Should it? Can that be the problem?

Very likely so. It's (normally) used in core.thread.Thread's push/popContext() when switching into a fiber.