Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setUnhandledExceptionHook hook not called in library #2941

Closed
josephdwyer opened this issue May 3, 2019 · 2 comments

Comments

@josephdwyer
Copy link

commented May 3, 2019

This example demonstrates the hook set with setUnhandledExceptionHook not being called when the kotlin native library is being consumed from c.

knlibrary.kt

package knlibrary

import kotlin.native.*
import kotlin.native.concurrent.*

fun test() {
    println("Hello Kotlin/Native!")

    setUnhandledExceptionHook({
        throwable: Throwable -> OnUnhandledException(throwable)
    }.freeze())

    CausesError()

    println("Should not get here")
}

fun OnUnhandledException(t: Throwable) {
    println("EXCEPTION: ${t.message}")
    t.printStackTrace()
}

fun CausesError() {
    val result = 1 / 0
}

consumer.c

#include <stdio.h>
#include "knlibrary_api.h"

int main()
{
   printf("Hello, C!\n");

   knlibrary_ExportedSymbols* lib = knlibrary_symbols();
   lib->kotlin.root.knlibrary.test();

   return 0;
}

Compile the example:

kotlinc-native knlibrary.kt -produce dynamic -o knlibrary.dll

Using instructions from here to consume the library.
Note: We have to rename the dll because consumer.exe looks for the name with _symbols for whatever reason.

lib.exe /def:knlibrary_symbols.def /out:knlibrary_symbols.lib
cl.exe consumer.c knlibrary_symbols.lib
MOVE knlibrary.dll knlibrary_symbols.dll
consumer.exe

Program output:

>consumer.exe
Hello, C!
Hello Kotlin/Native!

Expected: output includes stack trace

@olonho

This comment has been minimized.

Copy link
Contributor

commented May 13, 2019

Problem is reproducible only if host executable is compiled with MSVC, if it is compiled with clang - top level handler still catches the exception. #2946 fixes the issue for MSVC hosted executables as well.

@olonho

This comment has been minimized.

Copy link
Contributor

commented May 14, 2019

Fixed in master and will be available in 1.3.40.

@olonho olonho closed this May 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.