Skip to content

Commit

Permalink
Fix JoinableTaskContext.serializedTasks leak
Browse files Browse the repository at this point in the history
The collection of joinable tasks for which an ID is requested grew forever. The code that should purge completed tasks from the collection was never executing because the `TaskId` property is programmed to return `null` for completed tasks, and we query the property directly *after* setting the task as completed.

The fix then is simple: fetch the TaskId value *first* and *then* set the `JoinableTask` as completed.
  • Loading branch information
AArnott committed Aug 13, 2024
1 parent 2f9e9df commit a47707d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Microsoft.VisualStudio.Threading/JoinableTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -927,10 +927,14 @@ internal void Complete(Task wrappedTask)
{
if (!this.IsCompleteRequested)
{
// This must be done *before* setting IsCompleteRequested so that the TaskId property will still return the value we need.
ulong? taskId = this.token?.TaskId;

this.IsCompleteRequested = true;
if (this.token?.TaskId is ulong taskId)

if (taskId.HasValue)
{
this.JoinableTaskContext.RemoveSerializableIdentifier(taskId);
this.JoinableTaskContext.RemoveSerializableIdentifier(taskId.Value);
}

if (this.mainThreadQueue is object)
Expand Down

0 comments on commit a47707d

Please sign in to comment.