Skip to content

Commit eb7a9da

Browse files
[6.1] Revert partial packet fixup and replay improvements (#3556)
* Revert 1af7327 * Revert "Fix TryReadPlpBytes throws ArgumentException (#3470) (#3474)" This reverts commit 0a55896. * Revert 44762d9 * Revert "Improve async string perf and fix reading chars with initial offset. (#3377)" This reverts commit 05df554. * Revert "Refine handling of moving between replay and continue states (#3337)" This reverts commit 265f522. * Revert "Fix SqlCached buffer async read with continue edge case. (#3329)" This reverts commit c3857b1. * Revert "Add `async` snapshot continue capability for multipacket fields (#3161)" This reverts commit 33364e7. * Revert "Add partial packet detection and fixup (#2714)" This reverts commit 8d5e4f2. * Remove methods previously moved to common file. * Supply byte buffer to vector read. * Minor compilation fixes that were missed in the reverts. * Remove partial packet context switch helpers. * Remove accidental duplication of SqlDataReader * Revert len change * Undo buff rental in netfx to simplify 6.0 diff. * Fix missed rented buff code. --------- Co-authored-by: Cheena Malhotra <[email protected]>
1 parent 7dd1219 commit eb7a9da

22 files changed

+445
-3156
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,6 @@
354354
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\OnChangedEventHandler.cs">
355355
<Link>Microsoft\Data\SqlClient\OnChangedEventHandler.cs</Link>
356356
</Compile>
357-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Packet.cs">
358-
<Link>Microsoft\Data\SqlClient\Packet.cs</Link>
359-
</Compile>
360357
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ParameterPeekAheadValue.cs">
361358
<Link>Microsoft\Data\SqlClient\ParameterPeekAheadValue.cs</Link>
362359
</Compile>
@@ -762,9 +759,6 @@
762759
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObject.cs">
763760
<Link>Microsoft\Data\SqlClient\TdsParserStateObject.cs</Link>
764761
</Compile>
765-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObject.Multiplexer.cs">
766-
<Link>Microsoft\Data\SqlClient\TdsParserStateObject.Multiplexer.cs</Link>
767-
</Compile>
768762
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStaticMethods.cs">
769763
<Link>Microsoft\Data\SqlClient\TdsParserStaticMethods.cs</Link>
770764
</Compile>

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.Windows.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ internal void PostReadAsyncForMars()
2626

2727
_pMarsPhysicalConObj.IncrementPendingCallbacks();
2828
SessionHandle handle = _pMarsPhysicalConObj.SessionHandle;
29-
// we do not need to consider partial packets when making this read because we
30-
// expect this read to pend. a partial packet should not exist at setup of the
31-
// parser
32-
Debug.Assert(_physicalStateObj.PartialPacket==null);
3329
temp = _pMarsPhysicalConObj.ReadAsync(handle, out error);
3430

3531
Debug.Assert(temp.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");

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

Lines changed: 87 additions & 265 deletions
Large diffs are not rendered by default.

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

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,22 +167,14 @@ private void ReadSniError(TdsParserStateObject stateObj, uint error)
167167
stateObj.SendAttention(mustTakeWriteLock: true);
168168

169169
PacketHandle syncReadPacket = default;
170-
bool readFromNetwork = true;
171170
RuntimeHelpers.PrepareConstrainedRegions();
172171
bool shouldDecrement = false;
173172
try
174173
{
175174
Interlocked.Increment(ref _readingCount);
176175
shouldDecrement = true;
177-
readFromNetwork = !PartialPacketContainsCompletePacket();
178-
if (readFromNetwork)
179-
{
180-
syncReadPacket = ReadSyncOverAsync(stateObj.GetTimeoutRemaining(), out error);
181-
}
182-
else
183-
{
184-
error = TdsEnums.SNI_SUCCESS;
185-
}
176+
177+
syncReadPacket = ReadSyncOverAsync(stateObj.GetTimeoutRemaining(), out error);
186178

187179
Interlocked.Decrement(ref _readingCount);
188180
shouldDecrement = false;
@@ -195,7 +187,7 @@ private void ReadSniError(TdsParserStateObject stateObj, uint error)
195187
}
196188
else
197189
{
198-
Debug.Assert(!readFromNetwork || !IsValidPacket(syncReadPacket), "unexpected syncReadPacket without corresponding SNIPacketRelease");
190+
Debug.Assert(!IsValidPacket(syncReadPacket), "unexpected syncReadPacket without corresponding SNIPacketRelease");
199191
fail = true; // Subsequent read failed, time to give up.
200192
}
201193
}
@@ -206,7 +198,7 @@ private void ReadSniError(TdsParserStateObject stateObj, uint error)
206198
Interlocked.Decrement(ref _readingCount);
207199
}
208200

209-
if (readFromNetwork && !IsPacketEmpty(syncReadPacket))
201+
if (!IsPacketEmpty(syncReadPacket))
210202
{
211203
// Be sure to release packet, otherwise it will be leaked by native.
212204
ReleasePacket(syncReadPacket);
@@ -247,9 +239,60 @@ private void ReadSniError(TdsParserStateObject stateObj, uint error)
247239
AssertValidState();
248240
}
249241

250-
private uint GetSniPacket(PacketHandle packet, ref uint dataSize)
242+
public void ProcessSniPacket(PacketHandle packet, uint error)
251243
{
252-
return SniPacketGetData(packet, _inBuff, ref dataSize);
244+
if (error != 0)
245+
{
246+
if ((_parser.State == TdsParserState.Closed) || (_parser.State == TdsParserState.Broken))
247+
{
248+
// Do nothing with callback if closed or broken and error not 0 - callback can occur
249+
// after connection has been closed. PROBLEM IN NETLIB - DESIGN FLAW.
250+
return;
251+
}
252+
253+
AddError(_parser.ProcessSNIError(this));
254+
AssertValidState();
255+
}
256+
else
257+
{
258+
uint dataSize = 0;
259+
260+
uint getDataError = SniPacketGetData(packet, _inBuff, ref dataSize);
261+
262+
if (getDataError == TdsEnums.SNI_SUCCESS)
263+
{
264+
if (_inBuff.Length < dataSize)
265+
{
266+
Debug.Assert(true, "Unexpected dataSize on Read");
267+
throw SQL.InvalidInternalPacketSize(StringsHelper.GetString(Strings.SqlMisc_InvalidArraySizeMessage));
268+
}
269+
270+
_lastSuccessfulIOTimer._value = DateTime.UtcNow.Ticks;
271+
_inBytesRead = (int)dataSize;
272+
_inBytesUsed = 0;
273+
274+
if (_snapshot != null)
275+
{
276+
_snapshot.AppendPacketData(_inBuff, _inBytesRead);
277+
if (_snapshotReplay)
278+
{
279+
_snapshot.MoveNext();
280+
#if DEBUG
281+
_snapshot.AssertCurrent();
282+
#endif
283+
}
284+
}
285+
286+
SniReadStatisticsAndTracing();
287+
SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParser.ReadNetworkPacketAsyncCallback | INFO | ADV | State Object Id {0}, Packet read. In Buffer: {1}, In Bytes Read: {2}", ObjectID, _inBuff, _inBytesRead);
288+
289+
AssertValidState();
290+
}
291+
else
292+
{
293+
throw SQL.ParsingError(ParsingErrorState.ProcessSniPacketFailed);
294+
}
295+
}
253296
}
254297

255298
private void SetBufferSecureStrings()
@@ -321,7 +364,7 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
321364
bool processFinallyBlock = true;
322365
try
323366
{
324-
Debug.Assert((packet.Type == 0 && PartialPacketContainsCompletePacket()) || (CheckPacket(packet, source) && source != null), "AsyncResult null on callback");
367+
Debug.Assert(CheckPacket(packet, source) && source != null, "AsyncResult null on callback");
325368

326369
if (_parser.MARSOn)
327370
{

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,6 @@
459459
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\OnChangedEventHandler.cs">
460460
<Link>Microsoft\Data\SqlClient\OnChangedEventHandler.cs</Link>
461461
</Compile>
462-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Packet.cs">
463-
<Link>Microsoft\Data\SqlClient\Packet.cs</Link>
464-
</Compile>
465462
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\PacketHandle.Windows.cs">
466463
<Link>Microsoft\Data\SqlClient\PacketHandle.Windows.cs</Link>
467464
</Compile>
@@ -855,9 +852,6 @@
855852
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObject.cs">
856853
<Link>Microsoft\Data\SqlClient\TdsParserStateObject.cs</Link>
857854
</Compile>
858-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObject.Multiplexer.cs">
859-
<Link>Microsoft\Data\SqlClient\TdsParserStateObject.Multiplexer.cs</Link>
860-
</Compile>
861855
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs">
862856
<Link>Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs</Link>
863857
</Compile>

0 commit comments

Comments
 (0)