Skip to content

Bug: double logging with WIL with winrt::throw_hresult #1465

Description

Version

2.0.240405.15

Summary

When using wil's cppwinrt.h header, the callback handler winrt_throw_hresult_handler is defined and hooked into the C++/WinRT error handling.
This handler was originally hooked into winrt::throw_hresult() directly in #843, but #1185 later also added it to winrt::hresult_error::originate().

In the WIL definition, the code will only log the error, and not throw exceptions. So, when one calls winrt::throw_hresult(), the code will use the WIL handler, but continue to throw hresult_error(...). This call will construct an error and eventually invoke its originate() member, which will do a second WIL logging invocation.

Reproducible example

#include <wil/cppwinrt.h>
#include <winrt/base.h>

#include <iostream>

int main()
{
  wil::SetResultLoggingCallback([](const wil::FailureInfo& failure) noexcept {
      std::wcout << L"Failure" << failure.hr << std::endl;
  });
  winrt::throw_result(WEB_E_JSON_VALUE_NOT_FOUND);
}

Expected behavior

A single logging invocation to WIL.

Actual behavior

Two lines get logged.

Additional comments

I also felt like the WIL integration was a bit messy. We can't really use the THROW_HR macros because they throw the WIL exceptions based on std::exception, which C++/WinRT will interpret as E_FAIL through the ABI boundary. It would be better if WIL was also able to throw the hresult_errors directly.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions