-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Bogus null checks in MeterListener.SetMeasurementEventCallback<T> #68025
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Did you want to resolve this, in my PR? or you want to create another PR? |
I don't want to create a PR. I filed this as a new issue rather than as a comment in your PR, because this seems like a bug that should be fixed even if your PR adding support for |
Alternatively, it could be changed to throw ArgumentNullException, but then the parameter should not be marked as nullable. |
@KalleOlaviNiemitalo I think this decision break API, so we have to handle nullability |
@tommcdon If you agree that this issue is bug, So I think, we have to resolve this for Are you agree with me? |
I am going to fix this as originally suggested by @KalleOlaviNiemitalo in the description. The fix will be in .NET 7.0. I don't think we need to port this in .NET 6.0 as the workaround for users is they can check null in their code before calling. The users can recycle the listener too if needed. |
@tarekgh I wrote my comment on the issue, is there any wrong? |
Sorry, I commented by mistake here 😄 Thanks for submitting the PR. I have added a comment on the PR. |
@tarekgh I think, because .NET 6 is LTS and this bug is not change any API, so we can release this for .NET 6, without any cost. and demonstrates Microsoft 's commitment to support LTS .NET |
Usually, we do the servicing when we see any customer/user is blocked by that. I already mentioned some trivial workarounds if anyone wants to avoid this issue. Let's wait to see if more people run into this issue and then we can consider porting it. Thanks for your feedback. |
Description
If the
public void SetMeasurementEventCallback<T>(MeasurementCallback<T>? measurementCallback)
method of System.Diagnostics.Metrics.MeterListener is called with a null callback, then it throws an InvalidOperationException that claims that the type is unsupported, even if MeterListener actually supports the type.Reproduction Steps
Expected behavior
listener.SetMeasurementEventCallback<int>(null);
should discard any previously set callback forint
and succeed.Actual behavior
Self-contradictory exception:
Regression?
Not a regression; the original implementation in #52685 already did this.
Known Workarounds
Provide a no-op callback instead of
null
.Configuration
.NET SDK 6.0.202, runtime 6.0.4. Windows 10 Version 21H2 on x64.
It is not specific to this configuration.
Other information
All the
measurementCallback is null
checks in MeterListener.SetMeasurementEventCallback are currently redundant because they are only evaluated if a condition likemeasurementCallback is MeasurementCallback<byte> byteCallback
matches, which can't happen ifmeasurementCallback
is null. Perhaps the method should instead checktypeof(T) == typeof(byte)
etc. and then cast(MeasurementCallback<byte>)(object)measurementCallback
.runtime/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/MeterListener.cs
Lines 113 to 152 in be98e88
Noticed while looking at #68022.
The text was updated successfully, but these errors were encountered: