Skip to content

Commit

Permalink
NetMQPoller.Dispose throws if inner socket is disposed
Browse files Browse the repository at this point in the history
  • Loading branch information
drewnoakes committed Apr 29, 2017
1 parent 5086f42 commit b5f3492
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/NetMQ.Tests/NetMQPollerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,24 @@ public void RemoveSocket()
}
}

[Test]
public void DisposeThrowsIfSocketAlreadyDisposed()
{
var socket = new RouterSocket();

var poller = new NetMQPoller { socket };

// Dispose the socket.
// It is incorrect to have a disposed socket in a poller.
// Disposed sockets can throw into the poller's thread.
socket.Dispose();

// Dispose throws if a polled socket is disposed
var ex = Assert.Throws<NetMQException>(() => poller.Dispose());

Assert.AreEqual("Invalid state detected: NetMQPoller contains a disposed NetMQSocket. Sockets must be either removed before being disposed, or disposed after the poller is disposed.", ex.Message);
}

[Test]
public void SimpleTimer()
{
Expand Down
5 changes: 5 additions & 0 deletions src/NetMQ/NetMQPoller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ private void CheckDisposed()
/// <remarks>
/// Note that you cannot dispose the poller on the poller's thread. Doing so results in a deadlock.
/// </remarks>
/// <exception cref="NetMQException">A socket in the poller has been disposed.</exception>
public void Dispose()
{
// Attempting to dispose from the poller thread would cause a deadlock.
Expand All @@ -625,7 +626,11 @@ public void Dispose()
#endif

foreach (var socket in m_sockets)
{
if (socket.IsDisposed)
throw new NetMQException($"Invalid state detected: {nameof(NetMQPoller)} contains a disposed {nameof(NetMQSocket)}. Sockets must be either removed before being disposed, or disposed after the poller is disposed.");
socket.EventsChanged -= OnSocketEventsChanged;
}

m_disposeState = (int)DisposeState.Disposed;
}
Expand Down

0 comments on commit b5f3492

Please sign in to comment.