Skip to content

Commit 1ad1379

Browse files
committed
add dedicate sync object and user assertions in place of exceptions
1 parent 282f985 commit 1ad1379

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Microsoft.Data.SqlClient.SNI
1414
/// </summary>
1515
internal class SNIMarsConnection
1616
{
17+
private readonly object _sync;
1718
private readonly Guid _connectionId;
1819
private readonly Dictionary<int, SNIMarsHandle> _sessions;
1920
private SNIHandle _lowerHandle;
@@ -26,14 +27,15 @@ internal class SNIMarsConnection
2627

2728
public int ProtocolVersion => _lowerHandle.ProtocolVersion;
2829

29-
public object DemuxerSync => this;
30+
public object DemuxerSync => _sync;
3031

3132
/// <summary>
3233
/// Constructor
3334
/// </summary>
3435
/// <param name="lowerHandle">Lower handle</param>
3536
public SNIMarsConnection(SNIHandle lowerHandle)
3637
{
38+
_sync = new object();
3739
_connectionId = Guid.NewGuid();
3840
_sessions = new Dictionary<int, SNIMarsHandle>();
3941
_demuxState = DemuxState.Header;

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ internal sealed class SNIMarsHandle : SNIHandle
5656
public override void Dispose()
5757
{
5858
// SendControlPacket will lock so make sure that it cannot deadlock by failing to enter the DemuxerLock
59-
if (_connection != null && Monitor.IsEntered(_connection.DemuxerSync))
60-
{
61-
throw new InvalidOperationException("SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
62-
}
59+
Debug.Assert(_connection != null && Monitor.IsEntered(_connection.DemuxerSync), "SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
6360
long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className);
6461
try
6562
{
@@ -301,10 +298,7 @@ private uint SendPendingPackets()
301298
/// <returns>SNI error code</returns>
302299
public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = null)
303300
{
304-
if (Monitor.IsEntered(_connection.DemuxerSync))
305-
{
306-
throw new InvalidOperationException("SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
307-
}
301+
Debug.Assert(_connection != null && Monitor.IsEntered(_connection.DemuxerSync), "SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
308302
long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className);
309303
try
310304
{
@@ -438,10 +432,7 @@ public void HandleSendComplete(SNIPacket packet, uint sniErrorCode)
438432
/// <param name="highwater">Send highwater mark</param>
439433
public void HandleAck(uint highwater)
440434
{
441-
if (Monitor.IsEntered(_connection.DemuxerSync))
442-
{
443-
throw new InvalidOperationException("SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
444-
}
435+
Debug.Assert(_connection != null && Monitor.IsEntered(_connection.DemuxerSync), "SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
445436
long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className);
446437
try
447438
{
@@ -468,10 +459,7 @@ public void HandleAck(uint highwater)
468459
/// <param name="header">SMUX header</param>
469460
public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header)
470461
{
471-
if (Monitor.IsEntered(_connection.DemuxerSync))
472-
{
473-
throw new InvalidOperationException("SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
474-
}
462+
Debug.Assert(_connection != null && Monitor.IsEntered(_connection.DemuxerSync), "SNIMarsHandle.HandleRecieveComplete should be be called while holding the SNIMarsConnection.DemuxerSync because it can cause deadlocks");
475463
long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className);
476464
try
477465
{

0 commit comments

Comments
 (0)