Repository navigation
Wrap all accesses to cancellation token source in PackageItemViewModel in try-catch blocks to handle ObjectDisposedException gracefully - #6918
Conversation
…ocks to handle ObjectDisposedException gracefully
| try | ||
| { | ||
| cancellationToken = _cancellationTokenSource.Token; | ||
| } | ||
| catch (ObjectDisposedException) | ||
| { | ||
| // If the CancellationTokenSource is disposed, return empty collection | ||
| return Array.Empty<VersionInfoContextInfo>(); | ||
| } |
There was a problem hiding this comment.
I think we could centralize this pattern in a helper
There was a problem hiding this comment.
We can also null out the CancellationTokenSource to avoid the try catching ugliness.
| CancellationToken cancellationToken; | ||
| try | ||
| { | ||
| cancellationToken = _cancellationTokenSource.Token; |
There was a problem hiding this comment.
Could we maybe add bool _disposed = false to the PackageItemViewMidel and when the object is disposed set it true. That would allow methods that used the cancelation token to just check if _disposed and return an empty collection.
| private async Task RunOperationAsync(Func<CancellationToken, Task> func) | ||
| { | ||
| CancellationToken cancellationToken = _cancellationTokenSource.Token; | ||
| CancellationToken cancellationToken; | ||
| try | ||
| { | ||
| cancellationToken = _cancellationTokenSource.Token; |
There was a problem hiding this comment.
Did you investigate changing the method signatures (all the methods affected in this PR) so that the cancellation token is passed in, rather than getting it from the token source?
Even if this try-catch makes the fault telemetry go away, I'm concerned that we still have a code correctness issue.
PM UI closing is one reason PM UI disposes cancellation token sources, but another reason is when there's logic when we swap a token source with a new one (possibly when switching tabs, or on the browse tab when the search query changes). If there's a timing issue where async code uses _cancellationTokenSource after it's been disposed, I think it's possible it also accesses it after it's been swapped.
I've personally experienced issues when typing a search query immediately after opening PM UI and the search results that get displayed are wrong. Either showing the package source's top packages, as if I didn't type a search query at all, or the results clearly show that it only used the first few letters of the search query I ended up typing. It never occurred to me before, but now I wonder if it's related to async work scheduled that reads _cancellatinTokenSource.Token after the swap already happened.
As you know, the UI always runs callbacks (button clicks, search query box text changing) on the UI thread in synchronous methods. If these "entry point" methods are the only places that _cancellationTokenSource.Token is accessed, then I think we can be confident that there won't be "stale" async work that keeps running when we logically expect it to be cancelled. I expect most of these "entry point" methods will use JTF.RunAsync. If they capture the cancellation token before running JTF, and all async code gets passed the token, then we should solve the fault telemetry, but also fix correctness bugs.
There was a problem hiding this comment.
My vote is that if the code is good enough, we get this merged to fix the problem. I'd love to have more time to make the more in depth fix but since this is PM UI, I lean towards just getting the root issue solved so we can move on.
There was a problem hiding this comment.
I agree with Andy that we should try to capture the token, and minimize the amount of places with use the CTS.
For context, what Andy is referring to is https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource?view=net-9.0#thread-safety
Unfortunately I'm not sure that's a general solution because sometimes the calls are coming from the package manager control, sometimes from somewhere else.
fwiw, I don't care about fixing the telemetry issue that much, if all we're gonna do is hide the functional problem below.
If you'd rather not do the work that @zivkan is suggesting, I think we should just stop disposing instead. It does the same clean-up and we don't seem to be using CreateLinkedTokenSource that can cause some things to not be cleaned up.
See related aspnet/AspNetKatana#109
There was a problem hiding this comment.
fwiw, I don't care about fixing the telemetry issue that much, if all we're gonna do is hide the functional problem below.
If you'd rather not do the work that @zivkan is suggesting, I think we should just stop disposing instead.
I agree that I'm less concerned about the exception and one of the other options sounds better than hiding it.
| private async Task RunOperationAsync(Func<CancellationToken, Task> func) | ||
| { | ||
| CancellationToken cancellationToken = _cancellationTokenSource.Token; | ||
| CancellationToken cancellationToken; | ||
| try | ||
| { | ||
| cancellationToken = _cancellationTokenSource.Token; |
There was a problem hiding this comment.
I agree with Andy that we should try to capture the token, and minimize the amount of places with use the CTS.
For context, what Andy is referring to is https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource?view=net-9.0#thread-safety
Unfortunately I'm not sure that's a general solution because sometimes the calls are coming from the package manager control, sometimes from somewhere else.
fwiw, I don't care about fixing the telemetry issue that much, if all we're gonna do is hide the functional problem below.
If you'd rather not do the work that @zivkan is suggesting, I think we should just stop disposing instead. It does the same clean-up and we don't seem to be using CreateLinkedTokenSource that can cause some things to not be cleaned up.
See related aspnet/AspNetKatana#109
| catch (ObjectDisposedException) | ||
| { | ||
| // If the CancellationTokenSource is disposed, return empty collection | ||
| return Array.Empty<VersionInfoContextInfo>(); |
There was a problem hiding this comment.
Could this cause correctness issues?
Wouldn't it be more accurate throw an exception and tear it down instead of return a result potentially indicating things are fine?
| try | ||
| { | ||
| cancellationToken = _cancellationTokenSource.Token; | ||
| } | ||
| catch (ObjectDisposedException) | ||
| { | ||
| // If the CancellationTokenSource is disposed, return empty collection | ||
| return Array.Empty<VersionInfoContextInfo>(); | ||
| } |
There was a problem hiding this comment.
We can also null out the CancellationTokenSource to avoid the try catching ugliness.
donnie-msft
left a comment
There was a problem hiding this comment.
Great to see someone moving the needle on this type of issue! Left a +1 on a comment with two alternate approaches that sound better to me.
|
This PR has been automatically marked as stale because it has no activity for 7 days. It will be closed if no further activity occurs within another 7 days of this comment. If it is closed, you may reopen it anytime when you're ready again, as long as you don't delete the branch. |
Wrap all accesses to
_cancellationTokenSource.Tokenin try-catch blocks to handle ObjectDisposedException gracefullyBug
Fixes: https://github.com/NuGet/Client.Engineering/issues/3499
Description
The PackageItemViewModel class was throwing ObjectDisposedException when code attempted to access the CancellationTokenSource after the ViewModel had been disposed. This exception was being logged as a fault in telemetry.
Root Cause
The issue occurred when:
Dispose()method was called, which disposed the_cancellationTokenSource_cancellationTokenSource.TokenTokenproperty of a disposed CancellationTokenSource throwsObjectDisposedExceptionSolution
The fix wraps all accesses to
_cancellationTokenSource.Tokenin try-catch blocks to handle ObjectDisposedException gracefully. When the CancellationTokenSource is disposed:This approach ensures that:
PR Checklist
Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc.