Skip to content
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

fix: Task Scheduler Queue Task called when SychronizationContext disposed #3451

Merged
merged 4 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Paramore.Brighter/Tasks/BrighterSynchronizationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ public override void Post(SendOrPostCallback callback, object? state)

//just execute inline
// current thread already owns the context, so just execute inline to prevent deadlocks
//if (BrighterSynchronizationHelper.Current == SynchronizationHelper)
//SynchronizationHelper.ExecuteImmediately(contextCallback, state);
//else
if (BrighterSynchronizationHelper.Current == SynchronizationHelper)
SynchronizationHelper.ExecuteImmediately(contextCallback, state);
else
base.Post(callback, state);

}
Expand Down
30 changes: 28 additions & 2 deletions src/Paramore.Brighter/Tasks/BrighterTaskScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace Paramore.Brighter.Tasks;
Expand Down Expand Up @@ -60,13 +61,14 @@ protected override void QueueTask(Task task)

var queued = _synchronizationHelper.Enqueue((Task)task, false);
if (!queued)
{
{
Debug.IndentLevel = 1;
Debug.WriteLine($"BrighterTaskScheduler: QueueTask Failed to queue task {task.ToString()} on {System.Threading.Thread.CurrentThread.ManagedThreadId}");
Debug.IndentLevel = 0;
new Thread(TryExecuteNewThread) { IsBackground = true }.Start(task);
}
}

/// <summary>
/// Attempts to execute the specified task on the current thread.
/// </summary>
Expand Down Expand Up @@ -103,4 +105,28 @@ public void DoTryExecuteTask(Task task)

TryExecuteTask(task);
}

/// <summary>
/// In a new thread, attempts to execute the specified task.
/// </summary>
/// <remarks>
/// This is a little "Hail Mary" to try and execute a task that has failed to queue because we have already completed the synchronization context.
/// Seems to be caused by an Exection Context that has our scheduler as the default, as it fools ConfigureAwait
/// </remarks>
/// <param name="obj"></param>
/// <exception cref="ArgumentNullException"></exception>
private void TryExecuteNewThread(object? obj)
{
var task = obj as Task;
if (task == null)
{
throw new ArgumentNullException(nameof(obj));
}

Debug.IndentLevel = 1;
Debug.WriteLine($"BrighterTaskScheduler: Use TryExecuteNewThread for {task} on thread {System.Threading.Thread.CurrentThread.ManagedThreadId}");
Debug.IndentLevel = 0;

TryExecuteTask(task);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
#endregion

using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Paramore.Brighter.ServiceActivator;
using Paramore.Brighter.Tasks;
using Xunit;

namespace Paramore.Brighter.Core.Tests.SynchronizationContext;
namespace Paramore.Brighter.Core.Tests.Tasks;

public class BrighterSynchronizationContextsTests
{
Expand Down Expand Up @@ -123,6 +121,20 @@ public void Run_AsyncTaskWithResult_BlockingCode_Still_Ends()
resumed.Should().BeTrue();
result.Should().Be(17);
}

[Fact]
public void Run_AsyncTaskWithResultAndConfigurateAwait_BlockingCode_Still_Ends()
{
bool resumed = false;
var result = BrighterSynchronizationHelper.Run(async () =>
{
await Task.Delay(50).ConfigureAwait(false);
resumed = true;
return 17;
});
resumed.Should().BeTrue();
result.Should().Be(17);
}

[Fact]
public void Run_AsyncTaskWithResult_ContainsMultipleAsyncTasks_Still_Ends()
Expand Down
Loading