-
Notifications
You must be signed in to change notification settings - Fork 754
Poller.remove socket disposed fix #835
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 11 commits
3b8dbb9
d0f2725
53e1833
68429d7
dea90f4
6b344ca
c879804
54c2931
bc362a9
98a0f00
a3819e1
4f3b1e6
33816cb
bda8b3f
cb16939
544bb94
87b7649
92163b1
51457d1
fce2bb0
38910d8
3bacfb8
455f6b0
52fa20e
884d4ea
9f17258
3ea0693
72ebdbc
c14f9f9
0a767c6
fa5e49b
83a34b6
217e4f4
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using System.Linq; | ||
using System.Net.Sockets; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
using NetMQ.Core.Utils; | ||
#if !NET35 | ||
|
@@ -112,21 +113,47 @@ protected override void QueueTask(Task task) | |
m_tasksQueue.Enqueue(task); | ||
} | ||
|
||
public void Run([NotNull] Action action) | ||
public Task Run([NotNull] Action action) | ||
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. Breaking public API is not a good idea, and goes against the C4.1 process that this project uses. I agree that returning a task would be better. It'd be better to create overloads: public Task RunAsync([NotNull] Action action);
public Task RunAsync([NotNull] Func<CancellationToken> action, CancellationToken cancellationToken); 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. 100% agree, but was trying to avoid that because I originally thought only the Why is The only reason I can see is to work around the original thread race bug when calling 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.
Regardless, it's public API and must be preserved. 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.
Got that.
I think a public I suggest that the |
||
{ | ||
Task t; | ||
|
||
if (!IsRunning || CanExecuteTaskInline) | ||
{ | ||
action(); | ||
|
||
t = FromResult<object>(null); | ||
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. Rather than instantiate a new completed task for each invocation, cache an instance in a 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. This same pattern is also in existing overloaded method 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. A completed task is safe to share. Use The benefit of doing so is that this code path will not perform any heap allocations. |
||
} | ||
else | ||
new Task(action).Start(this); | ||
{ | ||
t = new Task(action); | ||
t.Start(this); | ||
Comment on lines
+135
to
+136
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.
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. ahh.... yes, I was wishing 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. Even if 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. Sry, I said |
||
} | ||
|
||
return t; | ||
} | ||
|
||
/// <summary> | ||
/// Provides a completed task with the result for a syncronously run action. | ||
/// this only needed for .NET40. Depricated by <see cref="Task.FromResult()"/> in 4.5+ | ||
/// </summary> | ||
/// <typeparam name="TResult"></typeparam> | ||
/// <param name="result"></param> | ||
/// <returns></returns> | ||
private static Task<TResult> FromResult<TResult>(TResult result) | ||
{ | ||
var tcs = new TaskCompletionSource<TResult>(); | ||
tcs.SetResult(result); | ||
return tcs.Task; | ||
} | ||
jasells marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#else | ||
private void Run(Action action) | ||
{ | ||
action(); | ||
} | ||
#endif | ||
|
||
#endregion | ||
#endregion | ||
jasells marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public NetMQPoller() | ||
{ | ||
|
@@ -225,6 +252,11 @@ public void Remove(ISocketPollable socket) | |
{ | ||
if (socket == null) | ||
throw new ArgumentNullException(nameof(socket)); | ||
|
||
// JASells: not sure I agree with this thow. | ||
// If trying to remove a disposed socket, why complain? It *might* work. The issue | ||
// is if the poller's thread tries to actually service the socket before the remove call... | ||
|
||
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. I'd remove this from the code and discuss in an issue/PR instead where conversation can happen more easily. The rationale here was to help people discover when they're tearing things down in the wrong order, as that is a common cause of deadlocks and other problems. If you're going to fail some of the time, it's better to fail all of the time as it makes it easier to write code correctly. 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. Yes, this is a note to do that =) |
||
if (socket.IsDisposed) | ||
throw new ArgumentException("Must not be disposed.", nameof(socket)); | ||
CheckDisposed(); | ||
|
@@ -243,33 +275,18 @@ public void Remove(ISocketPollable socket) | |
socket.Socket.EventsChanged -= OnSocketEventsChanged; | ||
m_sockets.Remove(socket.Socket); | ||
m_isPollSetDirty = true; | ||
}); | ||
}) | ||
.Wait(); | ||
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. Calling I'd suggest making a As an aside, it's better to use 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. Lol. @ first i tried 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. The problem likely was that you cannot 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. I think the latest addresses this by adding new |
||
// keep the API syncronous by blocking the calling thread via Wait(), else RemoveThrowsIfSocketAlreadyDisposed() test fails | ||
} | ||
|
||
public void RemoveAndDispose<T>(T socket) where T : ISocketPollable, IDisposable | ||
{ | ||
if (socket == null) | ||
throw new ArgumentNullException(nameof(socket)); | ||
if (socket.IsDisposed) | ||
throw new ArgumentException("Must not be disposed.", nameof(socket)); | ||
CheckDisposed(); | ||
|
||
Run(() => | ||
{ | ||
// Ensure the socket wasn't disposed while this code was waiting to be run on the poller thread | ||
if (socket.IsDisposed) | ||
throw new InvalidOperationException( | ||
$"{nameof(NetMQPoller)}.{nameof(RemoveAndDispose)} was called from a non-poller thread, " + | ||
"so ran asynchronously. " + | ||
$"The {socket.GetType().Name} being removed was disposed while the remove " + | ||
$"operation waited to start on the poller thread. When using {nameof(RemoveAndDispose)} " + | ||
"you should not dispose the pollable object ."); | ||
//call the remove method | ||
Remove(socket); | ||
|
||
socket.Socket.EventsChanged -= OnSocketEventsChanged; | ||
m_sockets.Remove(socket.Socket); | ||
m_isPollSetDirty = true; | ||
socket.Dispose(); | ||
}); | ||
//dispose of socket | ||
socket.Dispose(); | ||
jasells marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public void Remove([NotNull] NetMQTimer timer) | ||
|
@@ -280,7 +297,7 @@ public void Remove([NotNull] NetMQTimer timer) | |
|
||
timer.When = -1; | ||
|
||
Run(() => m_timers.Remove(timer)); | ||
Run(() => m_timers.Remove(timer)).Wait(); | ||
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. Again, synchronous waits are not safe. This would have to be a |
||
} | ||
|
||
public void Remove([NotNull] Socket socket) | ||
|
@@ -293,7 +310,8 @@ public void Remove([NotNull] Socket socket) | |
{ | ||
m_pollinSockets.Remove(socket); | ||
m_isPollSetDirty = true; | ||
}); | ||
}) | ||
.Wait(); | ||
jasells marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
#endregion | ||
|
Uh oh!
There was an error while loading. Please reload this page.