-
Notifications
You must be signed in to change notification settings - Fork 755
Pr/check destination null #1093
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
base: master
Are you sure you want to change the base?
Changes from all commits
8459b0b
33d1e9b
fb96e78
47bdc29
ad997b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace NetMQ.Tests | ||
{ | ||
internal class TaskUtils | ||
{ | ||
internal static async Task PollUntil(Func<bool> condition, TimeSpan timeout) | ||
{ | ||
var cts = new CancellationTokenSource(); | ||
cts.CancelAfter(timeout); | ||
|
||
await PollUntil(condition, cts.Token); | ||
} | ||
|
||
internal static async Task PollUntil(Func<bool> condition, CancellationToken ct = default) | ||
{ | ||
try | ||
{ | ||
while (!condition()) | ||
{ | ||
await Task.Delay(25, ct).ConfigureAwait(true); | ||
} | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
// Task was cancelled. Ignore exception and return. | ||
} | ||
} | ||
|
||
internal static bool WaitAll(IEnumerable<Task> tasks, TimeSpan timeout) | ||
{ | ||
PollUntil(() => tasks.All(t => t.IsCompleted), timeout).Wait(); | ||
return tasks.All(t => t.Status == TaskStatus.RanToCompletion); | ||
} | ||
|
||
internal static void WaitAll(IEnumerable<Task> tasks) | ||
{ | ||
PollUntil(() => tasks.All(t => t.IsCompleted), Timeout.InfiniteTimeSpan).Wait(); | ||
} | ||
|
||
internal static bool Wait(Task task, TimeSpan timeout) | ||
{ | ||
PollUntil(() => task.IsCompleted, timeout).Wait(); | ||
return task.Status == TaskStatus.RanToCompletion; | ||
} | ||
|
||
internal static void Wait(Task task) | ||
{ | ||
PollUntil(() => task.IsCompleted, Timeout.InfiniteTimeSpan).Wait(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1261,8 +1261,7 @@ private void ProcessCommands(int timeout, bool throttle, CancellationToken cance | |
// Process all the commands available at the moment. | ||
while (found) | ||
{ | ||
Assumes.NotNull(command.Destination); | ||
command.Destination.ProcessCommand(command); | ||
command.Destination?.ProcessCommand(command); | ||
found = m_mailbox.TryRecv(0, out command); | ||
Comment on lines
-1264
to
1265
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto, why is this safe? |
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,17 +180,25 @@ public bool CheckRead() | |
/// <returns><c>true</c> if the read succeeded, otherwise <c>false</c>.</returns> | ||
public bool TryRead([MaybeNullWhen(returnValue: false)] out T value) | ||
{ | ||
// Try to prefetch a value. | ||
if (!CheckRead()) | ||
try | ||
{ | ||
// Try to prefetch a value. | ||
if (!CheckRead()) | ||
{ | ||
value = default(T); | ||
return false; | ||
} | ||
|
||
// There was at least one value prefetched. | ||
// Return it to the caller. | ||
value = m_queue.Pop(); | ||
return true; | ||
} | ||
catch | ||
{ | ||
value = default(T); | ||
return false; | ||
} | ||
|
||
// There was at least one value prefetched. | ||
// Return it to the caller. | ||
value = m_queue.Pop(); | ||
return true; | ||
Comment on lines
-183
to
-193
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding |
||
} | ||
|
||
/// <summary> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you help me understand why this is needed? The previous code was expecting this to always return true. Is there a race? Why is this a safe change?