Repository: JuliaLang/julia
File: src/stackwalk.c (C)

Code before:
    if (NotificationReason == LDR_DLL_NOTIFICATION_REASON_LOADED) {
        const LDR_DLL_LOADED_NOTIFICATION_DATA *data = &NotificationData->Loaded;
        event->FullDllName = _wcsdup(data->FullDllName->Buffer);
        event->BaseDllName = _wcsdup(data->BaseDllName->Buffer);
        event->DllBase = (uintptr_t)data->DllBase;
        event->SizeOfImage = data->SizeOfImage;
    }
    else if (NotificationReason == LDR_DLL_NOTIFICATION_REASON_UNLOADED) {
        event->DllBase = (uintptr_t)NotificationData->Unloaded.DllBase;
    }

    // This lock guards both the data structure, as well as the possibility
    // that a DLL is about to unloaded that is concurrently being processed
    // by `SymLoadModuleExW`
    uv_mutex_lock(&jl_dll_notify_lock);

    event->NotificationReason = NotificationReason;
    event->next = dll_notify_queue;
    dll_notify_queue = event;

    if (uv_mutex_trylock(&jl_in_stackwalk) != 0) {
        uv_mutex_unlock(&jl_dll_notify_lock);
        return; // lock unavailable, process later
    }

    jl_profile_process_dll_events();

    uv_mutex_unlock(&jl_dll_notify_lock);
    uv_mutex_unlock(&jl_in_stackwalk);
}

// Initialize stackwalk infrastructure (DLL tracking and profiling)
void jl_init_stackwalk(void)
{
    uv_mutex_init(&jl_in_stackwalk);
    uv_mutex_init(&jl_dll_notify_lock);
    SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES | SYMOPT_IGNORE_CVREC);
    if (!SymInitialize(GetCurrentProcess(), "", 1))
        jl_safe_printf("WARNING: failed to initialize stack walk info\n");
    LdrRegisterDllNotification(0, dll_notification_callback, NULL, &dll_notification_cookie);
}

// Finalize stackwalk infrastructure
void jl_fin_stackwalk(void)
{
    if (dll_notification_cookie) {
        LdrUnregisterDllNotification(dll_notification_cookie);
        dll_notification_cookie = NULL;
    }
}


Review comment (suggestion):
It feels overly complicated to me for these events to get processed in two completely different places. And notably this one seems like wasted work since it is unreliable
```suggestion
```

Code after:
    if (NotificationReason == LDR_DLL_NOTIFICATION_REASON_LOADED) {
        const LDR_DLL_LOADED_NOTIFICATION_DATA *data = &NotificationData->Loaded;
        event->FullDllName = _wcsdup(data->FullDllName->Buffer);
        event->BaseDllName = _wcsdup(data->BaseDllName->Buffer);
        event->DllBase = (uintptr_t)data->DllBase;
        event->SizeOfImage = data->SizeOfImage;
    }
    else if (NotificationReason == LDR_DLL_NOTIFICATION_REASON_UNLOADED) {
        event->DllBase = (uintptr_t)NotificationData->Unloaded.DllBase;
    }

    // This lock guards both the data structure, as well as the possibility
    // that a DLL is about to unloaded that is concurrently being processed
    // by `SymLoadModuleExW`
    uv_mutex_lock(&jl_dll_notify_lock);

    event->NotificationReason = NotificationReason;
    event->next = dll_notify_queue;
    dll_notify_queue = event;

    uv_mutex_unlock(&jl_dll_notify_lock);
    return; // process later
}

// Initialize stackwalk infrastructure (DLL tracking and profiling)
void jl_init_stackwalk(void)
{
    uv_mutex_init(&jl_in_stackwalk);
    uv_mutex_init(&jl_dll_notify_lock);
    SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES | SYMOPT_IGNORE_CVREC);
    if (!SymInitialize(GetCurrentProcess(), "", 1))
        jl_safe_printf("WARNING: failed to initialize stack walk info\n");
    LdrRegisterDllNotification(0, dll_notification_callback, NULL, &dll_notification_cookie);
}

// Finalize stackwalk infrastructure
void jl_fin_stackwalk(void)
{
    // To avoid deadlocks (due to suspending the main thread during `ExitProcess()`)
    // and other misbehavior (due to missed DLL notifications during exit), take the
    // profile lock here to effectively disable any active profiling threads.
    jl_lock_profile_wr();
    if (dll_notification_cookie) {
        LdrUnregisterDllNotification(dll_notification_cookie);
        dll_notification_cookie = NULL;
    }
}

// Set the abort_profile_ptr in TLS
#ifdef _CPU_X86_64_
JL_DLLEXPORT void jl_set_profile_abort_ptr(_Atomic(int) *abort_ptr) JL_NOTSAFEPOINT