CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

Lost exception info (and exception type) across ABI boundary

Open Sergio0694 opened this issue 1 year ago • 5 comments

Describe the bug

I have a managed WinRT type, CanvasEffect (a class implementing the ICanvasImage WinRT interface from Win2D), that also implements a classic COM interface, ICanvasImageInterop. This type can thrown an InvalidOperationException (here) from one of the implemented interface methods. CsWinRT correctly handles this in the marshalling stub, catches the exception and calls ExceptionHelpers.SetExceptionInfo, but on the other side it seems like some information is lost. Specifically, while I am seeing the same HRESULT being thrown (-2146233079), I'm getting a COMException, rather than InvalidOperationException.

Note: that HRESULT is COR_E_INVALIDOPERATION, defined here.

I noticed this because I have a unit test, CanvasEffect_EffectNotRegisteringAnOutputNode, which is specifically checking for that exception type, and I noticed that while it used to work correctly, it's now failing, as the resulting exception is just a COMException.

Note that the original exception message is not present in the exception data either, it just seems to be lost:

image

To Reproduce

  1. Clone https://github.com/Sergio0694/ComputeSharp
  2. Run the CanvasEffect_EffectNotRegisteringAnOutputNode test

Expected behavior

The test should (presumably?) re-throw the InvalidOperationException, ideally with the original message too.

Version Info

  • Tested with .NET 7 and .NET 8, same behavior
  • CsWinRT 2.0.3 and 2.0.4, same behavior
  • Win2D 1.1.0

Sergio0694 avatar Nov 20 '23 18:11 Sergio0694

We might need to add that error code to the mapping here and also look at why we aren't bringing back the old exception from the error info itself or why it is getting lost. I assume none of this is cross processes and is within the same process?

manodasanW avatar Nov 20 '23 19:11 manodasanW

"I assume none of this is cross processes and is within the same process?"

Yup! This is all in-proc, it's just a simple app using Win2D and doing interop with some custom types.

I can put together a simpler repo if it helps, unless just reproing from that repo directly is ok, let me know what you prefer 🙂

Sergio0694 avatar Nov 20 '23 20:11 Sergio0694

"I assume none of this is cross processes and is within the same process?"

Yup! This is all in-proc, it's just a simple app using Win2D and doing interop with some custom types.

I can put together a simpler repo if it helps, unless just reproing from that repo directly is ok, let me know what you prefer 🙂

Small repro is always useful I think?

jlaanstra avatar Nov 20 '23 21:11 jlaanstra

Did it work before #1335?

jlaanstra avatar Nov 20 '23 21:11 jlaanstra