-
Notifications
You must be signed in to change notification settings - Fork 759
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) | ||
|
||
| { | ||
| Task t; | ||
|
|
||
| if (!IsRunning || CanExecuteTaskInline) | ||
| { | ||
| action(); | ||
|
|
||
| t = FromResult<object>(null); | ||
|
Member
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
Author
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
Member
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
Member
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.
Author
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
Member
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
Author
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.
Outdated
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... | ||
|
|
||
|
||
| 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(); | ||
|
||
| // 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.
Outdated
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(); | ||
|
||
| } | ||
|
|
||
| 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.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| #endregion | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.