Skip to content

Commit 1e4724e

Browse files
committed
Refactor to insert FreeGcHandle
1 parent 4ab9be1 commit 1e4724e

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ protected TdsParserStateObject(TdsParser parser, TdsParserStateObject physicalCo
6767
// General methods //
6868
/////////////////////
6969

70-
protected abstract void FreeGcHandle(int remaining, bool release);
71-
7270
internal abstract uint EnableSsl(ref uint info, bool tlsFirst, string serverCertificateFilename);
7371

7472
internal abstract uint WaitForSSLHandShakeToComplete(out int protocolVersion);
@@ -81,7 +79,9 @@ internal int DecrementPendingCallbacks(bool release)
8179
{
8280
int remaining = Interlocked.Decrement(ref _pendingCallbacks);
8381
SqlClientEventSource.Log.TryAdvancedTraceEvent("TdsParserStateObject.DecrementPendingCallbacks | ADV | State Object Id {0}, after decrementing _pendingCallbacks: {1}", _objectID, _pendingCallbacks);
82+
8483
FreeGcHandle(remaining, release);
84+
8585
// NOTE: TdsParserSessionPool may call DecrementPendingCallbacks on a TdsParserStateObject which is already disposed
8686
// This is not dangerous (since the stateObj is no longer in use), but we need to add a workaround in the assert for it
8787
Debug.Assert((remaining == -1 && SessionHandle.IsNull) || (0 <= remaining && remaining < 3), $"_pendingCallbacks values is invalid after decrementing: {remaining}");

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal partial class TdsParserStateObject
2727
internal SNIPacket _sniAsyncAttnPacket = null; // Packet to use to send Attn
2828

2929
// Async variables
30-
private GCHandle _gcHandle; // keeps this object alive until we're closed.
30+
protected GCHandle _gcHandle; // keeps this object alive until we're closed.
3131

3232
// Used for blanking out password in trace.
3333
internal int _tracePasswordOffset = 0;
@@ -112,11 +112,7 @@ internal int DecrementPendingCallbacks(bool release)
112112
int remaining = Interlocked.Decrement(ref _pendingCallbacks);
113113
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.TdsParserStateObject.DecrementPendingCallbacks|ADV> {0}, after decrementing _pendingCallbacks: {1}", ObjectID, _pendingCallbacks);
114114

115-
if ((0 == remaining || release) && _gcHandle.IsAllocated)
116-
{
117-
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.TdsParserStateObject.DecrementPendingCallbacks|ADV> {0}, FREEING HANDLE!", ObjectID);
118-
_gcHandle.Free();
119-
}
115+
FreeGcHandle(remaining, release);
120116

121117
// NOTE: TdsParserSessionPool may call DecrementPendingCallbacks on a TdsParserStateObject which is already disposed
122118
// This is not dangerous (since the stateObj is no longer in use), but we need to add a workaround in the assert for it

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObjectNative.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using System.Diagnostics;
88
using System.Net;
99
using System.Runtime.CompilerServices;
10+
using System.Runtime.ConstrainedExecution;
11+
using System.Runtime.InteropServices;
1012
using System.Threading.Tasks;
1113
using Interop.Windows.Sni;
1214
using Microsoft.Data.Common;
@@ -186,6 +188,16 @@ protected override void RemovePacketFromPendingList(PacketHandle ptr)
186188
}
187189
}
188190

191+
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
192+
protected override void FreeGcHandle(int remaining, bool release)
193+
{
194+
if ((0 == remaining || release) && _gcHandle.IsAllocated)
195+
{
196+
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.TdsParserStateObject.DecrementPendingCallbacks|ADV> {0}, FREEING HANDLE!", ObjectID);
197+
_gcHandle.Free();
198+
}
199+
}
200+
189201
internal override bool IsFailedHandle() => _sessionHandle.Status != TdsEnums.SNI_SUCCESS;
190202

191203
internal override bool IsPacketEmpty(PacketHandle readPacket)

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,8 @@ internal abstract void CreatePhysicalSNIHandle(
514514

515515
protected abstract bool CheckPacket(PacketHandle packet, TaskCompletionSource<object> source);
516516

517+
protected abstract void FreeGcHandle(int remaining, bool release);
518+
517519
internal abstract bool IsFailedHandle();
518520

519521
internal abstract bool IsPacketEmpty(PacketHandle readPacket);

0 commit comments

Comments
 (0)