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

Avoid use Task.Delay().Wait() #43

Merged
merged 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion src/DotNetty.Common/Concurrency/XThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,16 @@ public void Start(object parameter)

public static void Sleep(int millisecondsTimeout)
{
Task.Delay(millisecondsTimeout).Wait();
Thread.Sleep(millisecondsTimeout);
}

/// <exception cref="T:System.OperationCanceledException"><paramref name="cancellationToken" /> was canceled.</exception>
public static void Sleep(int millisecondsTimeout, CancellationToken cancellationToken)
{
using (var ev = new ManualResetEventSlim())
{
ev.Wait(millisecondsTimeout, cancellationToken);
}
}

public int Id => _threadId;
Expand Down
6 changes: 2 additions & 4 deletions src/DotNetty.Common/Utilities/HashedWheelTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,13 @@ TimeSpan WaitForNextTick()
: currentTime;
}

Task delay = null;
try
{
long sleepTimeMs = sleepTime.Ticks / TimeSpan.TicksPerMillisecond; // we've already rounded so no worries about the remainder > 0 here
Debug.Assert(sleepTimeMs <= int.MaxValue);
delay = Task.Delay((int)sleepTimeMs, _owner.CancellationToken);
delay.Wait();
XThread.Sleep((int)sleepTimeMs, _owner.CancellationToken);
}
catch (AggregateException) when (delay is object && delay.IsCanceled)
catch (OperationCanceledException)
{
if (Volatile.Read(ref _owner.v_workerState) == WorkerStateShutdown)
{
Expand Down