Skip to content

Commit

Permalink
add dedicate sync object and user assertions in place of exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraith2 committed Jun 25, 2021
1 parent 282f985 commit 1ad1379
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Microsoft.Data.SqlClient.SNI
/// </summary>
internal class SNIMarsConnection
{
private readonly object _sync;
private readonly Guid _connectionId;
private readonly Dictionary<int, SNIMarsHandle> _sessions;
private SNIHandle _lowerHandle;
Expand All @@ -26,14 +27,15 @@ internal class SNIMarsConnection

public int ProtocolVersion => _lowerHandle.ProtocolVersion;

public object DemuxerSync => this;
public object DemuxerSync => _sync;

/// <summary>
/// Constructor
/// </summary>
/// <param name="lowerHandle">Lower handle</param>
public SNIMarsConnection(SNIHandle lowerHandle)
{
_sync = new object();
_connectionId = Guid.NewGuid();
_sessions = new Dictionary<int, SNIMarsHandle>();
_demuxState = DemuxState.Header;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ internal sealed class SNIMarsHandle : SNIHandle
public override void Dispose()
{
// SendControlPacket will lock so make sure that it cannot deadlock by failing to enter the DemuxerLock
if (_connection != null && Monitor.IsEntered(_connection.DemuxerSync))
{
throw new InvalidOperationException("SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
}
Debug.Assert(_connection != null && Monitor.IsEntered(_connection.DemuxerSync), "SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className);
try
{
Expand Down Expand Up @@ -301,10 +298,7 @@ private uint SendPendingPackets()
/// <returns>SNI error code</returns>
public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = null)
{
if (Monitor.IsEntered(_connection.DemuxerSync))
{
throw new InvalidOperationException("SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
}
Debug.Assert(_connection != null && Monitor.IsEntered(_connection.DemuxerSync), "SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className);
try
{
Expand Down Expand Up @@ -438,10 +432,7 @@ public void HandleSendComplete(SNIPacket packet, uint sniErrorCode)
/// <param name="highwater">Send highwater mark</param>
public void HandleAck(uint highwater)
{
if (Monitor.IsEntered(_connection.DemuxerSync))
{
throw new InvalidOperationException("SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
}
Debug.Assert(_connection != null && Monitor.IsEntered(_connection.DemuxerSync), "SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className);
try
{
Expand All @@ -468,10 +459,7 @@ public void HandleAck(uint highwater)
/// <param name="header">SMUX header</param>
public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header)
{
if (Monitor.IsEntered(_connection.DemuxerSync))
{
throw new InvalidOperationException("SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
}
Debug.Assert(_connection != null && Monitor.IsEntered(_connection.DemuxerSync), "SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className);
try
{
Expand Down

0 comments on commit 1ad1379

Please sign in to comment.