Filter from SetUnhandledExceptionFilter not capturing C++ exceptions from threads started with _beginthread on windows server 2022

Malcolm Davey 0 Reputation points
2024-09-11T01:28:04.4433333+00:00

We have found a the change in behaviour of the SetUnhandledExceptionFilter API call. On windows server 2016, this enables any uncaught C++ exceptions to be handled by a registered filter, including on our worker threads. In our filter callback we have alternative handling, including stopping the applications from terminating, but on windows server 2022, our filter doesn't get called for C++ exceptions on our worker threads, but the application is terminated instead.

There is no mention of a change in behaviour of this on certain windows versions in the windows API documentation.

Some investigation has shown this seems to be related to different versions of UCRT.dll (universal C run time) between different windows server OS versions.

And also in relation to any threads created using _beginthread/_beginthreadex It seems this has been changed to _beginthread to now be noexcept() in behaviour - similar to std::thread behaviour. But the problem is that this is not based which version of the C++ compiler/toolset or C/C++ libraries that we compile/link against, but what the OS shipped UCRT.DLL is compiler against, and so relates to the windows OS version.

We are needing to upgrade some servers to windows server 2022 from 2016, but this is changing the behaviour of out existing applications, that rely on the SetUnhandledExceptionFilter behaviour to catch any C++ exceptions. And so this is blocking our migration. This applications need to be running continually (except for allowed shutdown times on weekends), and so crashes/shutdown are major incidents.

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,332 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,592 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,693 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Darshida Das 15 Reputation points
    2024-09-11T11:20:26.31+00:00

    #include <windows.h>

    #include <stdio.h>

    #include <eh.h>

    // Define a custom exception filter function

    LONG WINAPI CustomUnhandledExceptionFilter(EXCEPTION_POINTERS* ExceptionInfo) {

    // Handle exceptions here
    
    // For example, log the exception or perform custom actions
    
    printf("Unhandled exception occurred!\n");
    
    return EXCEPTION_EXECUTE_HANDLER;
    

    }

    int main() {

    // Set the custom unhandled exception filter
    
    SetUnhandledExceptionFilter(CustomUnhandledExceptionFilter);
    
    // Create a worker thread
    
    HANDLE thread = (HANDLE)_beginthread([](void* param) {
    
        // Worker thread code
    
        // For example, simulate an exception
    
        throw std::runtime_error("Simulated exception");
    
    }, 0, nullptr);
    
    WaitForSingleObject(thread, INFINITE);
    
    CloseHandle(thread);
    
    return 0;
    

    }

    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.