Skip to content

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

Closed as not planned

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

  1. dmachaj commented on Jan 6, 2025

    @dmachaj
    Contributor

    It has been more than 2 years since I added that integration so my memory is very hazy. I think I recall it double-logging in some cases like the one you highlight, but that was difficult or impossible to avoid without losing logging in other cases. I decided it would be better to double-log in some cases instead of failing to log in others.

    As for the additional comment I think WIL can base its exception type on multiple different things (such as in C++/CX code it derives from Platform::Exception instead). I'm not sure how reasonable it would be to implement but I think a more ideal fix would be to allow cppwinrt to understand the wil::ResultException type in its ABI boundary handler. That way it can pull out the HRESULT and message directly without having to change WIL itself.

  2. arthuraraujo-msft commented on Jan 6, 2025

    @arthuraraujo-msft
    Author

    It has been more than 2 years since I added that integration so my memory is very hazy. I think I recall it double-logging in some cases like the one you highlight, but that was difficult or impossible to avoid without losing logging in other cases. I decided it would be better to double-log in some cases instead of failing to log in others.

    Since the same winrt::throw_hresult() leads to the originate() call, perhaps a new parameter could be passed to the hresult_error constructor indicating there's no need to log a second time. Or just dropping the logging in winrt::throw_hresult() since the only hresult that doesn't create an hresult_error is error_bad_alloc.

    As for the additional comment I think WIL can base its exception type on multiple different things (such as in C++/CX code it derives from Platform::Exception instead). I'm not sure how reasonable it would be to implement but I think a more ideal fix would be to allow cppwinrt to understand the wil::ResultException type in its ABI boundary handler. That way it can pull out the HRESULT and message directly without having to change WIL itself.

    That sounds interesting. Would it be some sort of extension point in winrt::hresult_error::to_hresult()? Is that what gets used for the boundary handler? I see that function has an optional external handler winrt_to_hresult_handler but that doesn't use the HRESULT for conversion.

    I used the integration a little more and realized that the handler does pick up the right HRESULT. It does that by calling throw and catching what is already in the throw context, then extracting the HRESULT from the wil exception. I probably hadn't included it in all needed files. We do lose the message across API boundaries because of the generic try ... catch(...) { return winrt::to_hresult(); } that C++/WinRT adds to API boundaries as winrt::to_hresult only returns an HRESULT and not an hresult_error with a message. So the message is only ever useful for logging at the point of throwing.

    I noticed C++/WinRT uses Get and SetErrorInfo() to store/retrieve the messages when the WIL hook is not enabled and opened wil #495 so WIL can also make use of that to transmit the messages.

    Using the result_originate.h WIL header picks up the message as expected. So, the only remaining problem is the double logging.

  3. github-actions commented on Jan 27, 2025

    @github-actions

    This issue is stale because it has been open 10 days with no activity. Remove stale label or comment or this will be closed in 5 days.

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