-
Notifications
You must be signed in to change notification settings - Fork 784
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
Throw NotSupportedException when using SetErrorStatusOnException
in Mono Runtime and Native AOT environment.
#5374
Throw NotSupportedException when using SetErrorStatusOnException
in Mono Runtime and Native AOT environment.
#5374
Conversation
…/opentelemetry-dotnet into yunl/ExceptionProcessor
this.fnGetExceptionPointers = Marshal.GetExceptionPointers; | ||
if (RuntimeFeature.IsDynamicCodeSupported) | ||
{ | ||
throw new NotSupportedException($"'{typeof(Marshal).FullName}.GetExceptionPointers' is not supported when running native AOT."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to make this a build time error? I think that will help the developers to discover the issue earlier.
If throwing is the only possible course of action, maybe this should be "fixed" on the .NET runtime side.
The only reason to add this check here is that it will consistently throw between F5 debugging (where we emulate I wonder if we should instead "break" this API during F5 debugging instead - same way we break Reflection.Emit or COM interop when Then OpenTelemetry doesn't need to overapproximate the conditions when @eerhardt @AaronRobinsonMSFT @jkotas @agocke any opinion? |
I agree that it is a better experience to get an exception during F5 development when For my education, what would it take to actually support the API in native AOT? Or provide some other API that would allow opentelemetry to work on all platforms. (See #1853 and #1858 for the scenario that this code is trying to enable.) |
@@ -19,7 +21,14 @@ internal sealed class ExceptionProcessor : BaseProcessor<Activity> | |||
public ExceptionProcessor() | |||
{ | |||
#if NET6_0_OR_GREATER || NETFRAMEWORK | |||
this.fnGetExceptionPointers = Marshal.GetExceptionPointers; | |||
if (RuntimeFeature.IsDynamicCodeSupported) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (RuntimeFeature.IsDynamicCodeSupported) | |
bool getExceptionPointersSupported = true; | |
try { Marshal.GetExceptionPointers(); } catch { getExceptionPointersSupported = false; } | |
if (!getExceptionPointersSupported) | |
throw new throw new NotSupportedException($"'{typeof(Marshal).FullName}.GetExceptionPointers' is not supported."); |
Instead of calling RuntimeFeature.IsDynamicCodeSupported
, can this just call the API to see whether it is supported?
Note that this API is also not supported in Mono (irrespective of whether IsDynamicCodeSupported is true or false), so it would cover the Mono.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what would it take to actually support the API in native AOT?
Yes, it should be possible to simulated the API in native AOT. The API would need to allocate a piece of memory with the same lifetime as the current exception in flight (if it was not previously allocated), fill it with gunk, and return the pointer to it. The "new" exception handling that is being enabled for CoreCLR and that is not internally based on SEH proves that it is possible.
(Also, as I have said earlier - introducing a new API that is explicitly designed for this scenario would be the proper solution.)
Thank you! I agree above is the proper way to handle the situation. Shall I log this feature request in the runtime repo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall I log this feature request in the runtime repo?
Yes, please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've filed a request to implement the API in runtime: dotnet/runtime#98878.
Chatted with @eerhardt, @jkotas and @reyang offline.
While the above API is being implemented, we may do the followings to avoid crashing the consumer's app:
- Annotate
SetErrorStatusOnException()
with [RequiresDynamicCode] to serve as a compile time warning for the consumer. One caveat of this approach is IsDynamicCode does not really explain the nature of this issue and may further confuse the consumer. - Use try/catch to swallow the exception if users hit
System.PlatformNotSupportedException
upon callingMarshal.GetExceptionPointer()
. Store the state inExceptionProcessor()
and dump the Message "Exception processor not supported" insideOnEnd(Activity activity)
.
Not sure which one is a better stopgap of this issue?
cc @MichalStrehovsky, @agocke
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed in today's SIG meeting.
I'll go with adding the compile time warning as was mentioned in 1. above.
Plus, guarding the extension method
opentelemetry-dotnet/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderSdk.cs
Line 144 in e7cbbbb
public TracerProviderBuilder SetErrorStatusOnException(bool enabled) |
with a try/catch on Marshal.GetExceptionPointers to avoid adding useless Processor at runtime (if user ever decided to suppress the compile time AOT warning.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need to add try/catch
block. We just have to call Marshal.GetExceptionPointers()
once in the ExceptionProcessor
ctor and rethrow the exception with our custom message.
@CodeBlanch Just to confirm this is what you suggested, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OK with either re-throwing or just silently failing & not adding the processor. Probably re-throwing is better to alert the users 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on re-throwing the exception at start-up.
I do not think that it is worth it. OpenTelemetry is not using for what the API is meant for. This API exists to support
I expect that OpenTelemetry would still want to do something to avoid crashing, so this would not help. Check my suggestion https://github.com/open-telemetry/opentelemetry-dotnet/pull/5374/files#r1497791686
Yes, it should be possible to simulated the API in native AOT. The API would need to allocate a piece of memory with the same lifetime as the current exception in flight (if it was not previously allocated), fill it with gunk, and return the pointer to it. The "new" exception handling that is being enabled for CoreCLR and that is not internally based on SEH proves that it is possible. (Also, as I have said earlier - introducing a new API that is explicitly designed for this scenario would be the proper solution.) |
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #5374 +/- ##
==========================================
+ Coverage 83.38% 84.52% +1.14%
==========================================
Files 297 284 -13
Lines 12531 12105 -426
==========================================
- Hits 10449 10232 -217
+ Misses 2082 1873 -209
Flags with carried forward coverage won't be shown. Click here to find out more.
|
src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
ExceptionProcessor
in native AOT.ExceptionProcessor
in Mono Runtime and Native AOT enviornment.
src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
ExceptionProcessor
in Mono Runtime and Native AOT enviornment.SetErrorStatusOnException
in Mono Runtime and Native AOT environment.
src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
…s.cs Co-authored-by: Reiley Yang <[email protected]>
Mitigates #5358
Design discussion issue #
Changes
Throw NotSupportedException when using ExceptionProcessor in Mono Runtime and Native AOT environment.
Merge requirement checklist
CHANGELOG.md
files updated for non-trivial changes