You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: double logging with WIL with winrt::throw_hresult #1465
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.
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.
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.
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.
Activity
dmachaj commentedon Jan 6, 2025
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::Exceptioninstead). 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 thewil::ResultExceptiontype in its ABI boundary handler. That way it can pull out the HRESULT and message directly without having to change WIL itself.arthuraraujo-msft commentedon Jan 6, 2025
Since the same
winrt::throw_hresult()leads to theoriginate()call, perhaps a new parameter could be passed to thehresult_errorconstructor indicating there's no need to log a second time. Or just dropping the logging inwinrt::throw_hresult()since the only hresult that doesn't create anhresult_erroris error_bad_alloc.That sounds interesting. Would it be some sort of extension point inwinrt::hresult_error::to_hresult()? Is that what gets used for the boundary handler? I see that function has an optional external handlerwinrt_to_hresult_handlerbut 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
throwand 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 generictry ... catch(...) { return winrt::to_hresult(); }that C++/WinRT adds to API boundaries aswinrt::to_hresultonly returns an HRESULT and not anhresult_errorwith 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.hWIL header picks up the message as expected. So, the only remaining problem is the double logging.github-actions commentedon Jan 27, 2025
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.