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

Notify crash handler about GPU reset exception #1350

Draft
wants to merge 2 commits into
base: staging
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion obs-studio-server/source/nodeobs_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
#endif

#ifdef WIN32
#include <comdef.h>
#include <audiopolicy.h>
#include <mmdeviceapi.h>

Expand All @@ -99,7 +100,14 @@
#define GBYTE (1024ULL * 1024ULL * 1024ULL)
#define TBYTE (1024ULL * 1024ULL * 1024ULL * 1024ULL)

enum crashHandlerCommand { REGISTER = 0, UNREGISTER = 1, REGISTERMEMORYDUMP = 2, CRASHWITHCODE = 3 };
enum crashHandlerCommand
{
REGISTER = 0,
UNREGISTER = 1,
REGISTERMEMORYDUMP = 2,
CRASHWITHCODE = 3,
HANDLEOUTOFGPU = 4
};

struct NodeOBSLogParam final {
std::fstream logStream;
Expand Down Expand Up @@ -699,6 +707,34 @@ std::vector<char> crashedProcess(uint32_t crash_id)
return buffer;
}

#if defined(_WIN32)
static std::vector<char> handleOutOfGPU(int category, unsigned long long code)
{
std::vector<char> buffer;

// Prepare
std::uint8_t messageAction = crashHandlerCommand::HANDLEOUTOFGPU;
std::uint64_t messageCode(code);
std::wstring messageCodeDesc = (code) ? _com_error(static_cast<HRESULT>(code)).ErrorMessage() : L"Undefined error";
std::uint32_t messageCodeDescSize = (messageCodeDesc.size() + 1) * sizeof(wchar_t);

buffer.resize(sizeof(messageAction) + sizeof(messageCode) + sizeof(std::uint32_t) + messageCodeDescSize);

// Pack
uint32_t offset = 0;
memcpy(buffer.data(), &messageAction, sizeof(messageAction));
offset++;
memcpy(buffer.data() + offset, &messageCode, sizeof(messageCode));
offset += sizeof(messageCode);
memcpy(buffer.data() + offset, &messageCodeDescSize, sizeof(messageCodeDescSize));
offset += sizeof(messageCodeDescSize);
memcpy(buffer.data() + offset, messageCodeDesc.data(), messageCode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a typo here. Last parameter should be size, but it is messageCode

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be messageCodeDescSize

offset += messageCodeDescSize;

return buffer;
}
#endif

#ifdef WIN32
std::wstring crash_handler_pipe;

Expand Down Expand Up @@ -748,6 +784,14 @@ void writeCrashHandler(std::vector<char> buffer)
}
#endif

static void handleGSError(void* param, int category, unsigned long long code)
{
#if defined(_WIN32)
writeCrashHandler(handleOutOfGPU(category, code));
#endif
blog(LOG_ERROR, ">>> PROCESSED A CRITICAL ERROR");
}

static bool checkIfDebugLogsEnabled(const std::string &appdata)
{
#if defined(_DEBUG)
Expand Down Expand Up @@ -909,6 +953,12 @@ void OBS_API::OBS_API_initAPI(void *data, const int64_t id, const std::vector<ip
SetPrivilegeForGPUPriority();
#endif

blog(LOG_INFO, ">>> BEFORE CALLBACK");

obs_set_gs_error_handler(handleGSError, nullptr);

blog(LOG_INFO, ">>> AFTER CALLBACK");

osn::Source::initialize_global_signals();

cpuUsageInfo = os_cpu_usage_info_start();
Expand Down