Repository: JuliaLang/julia
File: src/signals-win.c (C)

Code before:
        if (sig == SIGSEGV && jl_get_safe_restore()) {
            signal(sig, (void (__cdecl *)(int))crt_sig_handler);
            jl_sig_throw();
        }
        memset(&Context, 0, sizeof(Context));
        RtlCaptureContext(&Context);

        ios_t s;
        ios_mem(&s, 1024);
        if (sig == SIGILL)
            jl_fprint_sigill(&s, &Context);
        jl_fprint_critical_error(&s, sig, 0, &Context, jl_get_current_task());

        // First write to stderr
        ios_write_direct(ios_stderr, &s);

        // Then write to Application log
        HANDLE event_source = RegisterEventSourceW(NULL, L"julia");
        if (event_source != INVALID_HANDLE_VALUE) {
            ios_putc('\0', &s);
            const wchar_t *strings[] = { ios_utf8_to_wchar(s.buf) };
            ReportEventW(
                event_source, EVENTLOG_ERROR_TYPE, /* category */ 0, /* event_id */ (DWORD)0xE0000000L,
               /* user_sid */ NULL, /* n_strings */ 1, /* data_size */ 0, strings, /* data */ NULL
            );
            MessageBoxW(NULL, /* message */ L"error: libjulia received a fatal signal.\n\n"
                                            L"See Application log in Event Viewer for more information.",
                        /* title */ L"fatal error in libjulia", MB_OK | MB_ICONEXCLAMATION | MB_SYSTEMMODAL);
        }
        raise(sig);
    }
}

// StackOverflowException needs extra stack space to record the backtrace
// so we keep one around, shared by all threads
static uv_mutex_t backtrace_lock;
static win32_ucontext_t collect_backtrace_fiber;
static win32_ucontext_t error_return_fiber;
static PCONTEXT stkerror_ctx;
static jl_ptls_t stkerror_ptls;
static int have_backtrace_fiber;
static void JL_NORETURN start_backtrace_fiber(void)
{
    // collect the backtrace
    stkerror_ptls->bt_size =
        rec_backtrace_ctx(stkerror_ptls->bt_data, JL_MAX_BT_SIZE, stkerror_ctx,
                          NULL /*current_task?*/);
    // switch back to the execution fiber
    jl_setcontext(&error_return_fiber);
    abort();
}

Review comment (bug):
I know very little about Windows, so my apologies if these questions are dumb, but: 1. Is this call async, or does it block? 
2. Is this really safe to call here? In particular, 
3. I am slightly worried about situations I experienced in an entirely different context were broadly similar code could lead to hundreds of dialog boxes spamming a user (when code that launched a ton of subprocesses to process data had them all fail simultaneously -- e.g. because a network connection dropped).

Anyway, none of this is meant as argument against this PR (which to me, as a non-Windows user, seems quite useful). Feel free to ignore me :-)

Code after:
        if (sig == SIGSEGV) { // restarting jl_ or profile
            jl_jmp_buf *saferestore = jl_get_safe_restore();
            if (saferestore) {
                signal(sig, (void (__cdecl *)(int))crt_sig_handler);
                jl_longjmp(*saferestore, 1);
                return;
            }
        }
        memset(&Context, 0, sizeof(Context));
        RtlCaptureContext(&Context);

        ios_t s;
        ios_mem(&s, 0);
        if (sig == SIGILL)
            jl_fprint_sigill(&s, &Context);
        jl_fprint_critical_error(&s, sig, 0, &Context, jl_get_current_task());

        // First write to stderr
        ios_write_direct(ios_safe_stderr, &s);

        // Then write to Application log
        HANDLE event_source = RegisterEventSourceW(NULL, L"julia");
        if (event_source != INVALID_HANDLE_VALUE) {
            ios_putc('\0', &s);
            const wchar_t *strings[] = { ios_utf8_to_wchar(s.buf) };
            ReportEventW(
                event_source, EVENTLOG_ERROR_TYPE, /* category */ 0, /* event_id */ (DWORD)0xE0000000L,
               /* user_sid */ NULL, /* n_strings */ 1, /* data_size */ 0, strings, /* data */ NULL
            );
            free(strings[0]);

            if (jl_options.alert_on_critical_error) {
                MessageBoxW(NULL, /* message */ L"error: libjulia received a fatal signal.\n\n"
                                                L"See Application log in Event Viewer for more information.",
                            /* title */ L"fatal error in libjulia", MB_OK | MB_ICONEXCLAMATION | MB_SYSTEMMODAL);
            }
        }
        raise(sig);
    }
}

// StackOverflowException needs extra stack space to record the backtrace
// so we keep one around, shared by all threads
static uv_mutex_t backtrace_lock;
static win32_ucontext_t collect_backtrace_fiber;
static win32_ucontext_t error_return_fiber;
static PCONTEXT stkerror_ctx;
static jl_ptls_t stkerror_ptls;
static int have_backtrace_fiber;
static void JL_NORETURN start_backtrace_fiber(void)
{