Skip to content

Commit

Permalink
Fix CreateMonitorSocket causing NetMQContext dispose hang. Closes zer…
Browse files Browse the repository at this point in the history
  • Loading branch information
drewnoakes committed Jun 2, 2015
1 parent 47061ff commit 0ec48df
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/NetMQ.Tests/MonitorPollTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,16 @@ public void MonitorDisposeProperlyWhenDisposedAfterMonitoredTcpSocket()
}
// NOTE If this test fails, it will hang because context.Dispose will block
}

[Test]
public void CreateMonitorSocket_ShouldntHangContextDispose_GitHubIssue223()
{
var context = NetMQContext.Create();

using (context.CreateMonitorSocket("inproc://monitor1234"))
{}

Assert.True(Task.Factory.StartNew(() => context.Dispose()).Wait(1000));
}
}
}
8 changes: 6 additions & 2 deletions src/NetMQ/Monitoring/NetMQMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,19 @@ public NetMQMonitor([NotNull] NetMQContext context, [NotNull] NetMQSocket monito
/// </remarks>
/// <param name="socket">The socket to monitor.</param>
/// <param name="endpoint">a string denoting the endpoint which will be the monitoring address</param>
public NetMQMonitor([NotNull] NetMQSocket socket, [NotNull] string endpoint)
/// <param name="ownsSocket">
/// A flag indicating whether ownership of <paramref name="socket"/> is transferred to the monitor.
/// If <c>true</c>, disposing the monitor will also dispose <paramref name="socket"/>.
/// </param>
public NetMQMonitor([NotNull] NetMQSocket socket, [NotNull] string endpoint, bool ownsSocket = false)
{
Endpoint = endpoint;
Timeout = TimeSpan.FromSeconds(0.5);
m_monitoringSocket = socket;

m_monitoringSocket.ReceiveReady += Handle;

m_ownsMonitoringSocket = false;
m_ownsMonitoringSocket = ownsSocket;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/NetMQ/NetMQContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public NetMQMonitor CreateMonitorSocket([NotNull] string endpoint)
throw new ArgumentException("Unable to monitor to an empty endpoint.", "endpoint");
}

return new NetMQMonitor(CreatePairSocket(), endpoint);
return new NetMQMonitor(CreatePairSocket(), endpoint, ownsSocket: true);
}

#endregion
Expand Down

0 comments on commit 0ec48df

Please sign in to comment.