Skip to content

Commit 5d0ca7d

Browse files
[Release 1.1] Revert Async changes in SNIPacket to fix deadlock issues + Update SNI Package Reference (#425)
1 parent d4d621c commit 5d0ca7d

File tree

8 files changed

+78
-264
lines changed

8 files changed

+78
-264
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ For the .NET Framework driver on Windows, a package reference to [Microsoft.Data
2828

2929
For the .NET Core driver on Windows, a package reference to [runtime.native.System.Data.SqlClient.sni](https://www.nuget.org/packages/runtime.native.System.Data.SqlClient.sni/) loads `arm64`, `x64` and `x86` native `SNI.dll` libraries into the client's build directories.
3030

31-
**Note**
32-
When referencing the native `SNI.dll` on Windows, the Microsoft Visual C++ Redistributable is required to be installed: [The latest supported Visual C++ downloads](https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads)
33-
34-
3531
## Helpful Links
3632

3733
| Topic | Link to File |

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
<Compile Include="Microsoft\Data\SqlClient\EnclaveDelegate.NetCoreApp.cs" />
4545
<Compile Include="Microsoft\Data\SqlClient\SqlDelegatedTransaction.NetCoreApp.cs" />
4646
<Compile Include="Microsoft\Data\SqlClient\TdsParser.NetCoreApp.cs" />
47-
<Compile Include="Microsoft\Data\SqlClient\SNI\SNIPacket.NetCoreApp.cs" />
4847
<Compile Include="Microsoft\Data\SqlClient\AzureAttestationBasedEnclaveProvider.NetCoreApp.cs" />
4948
<Compile Include="Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProvider.NetCoreApp.cs" />
5049
<Compile Include="Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProviderBase.NetCoreApp.cs" />
@@ -56,7 +55,6 @@
5655
<ItemGroup Condition="'$(IsPartialFacadeAssembly)' != 'true' AND '$(OSGroup)' != 'AnyOS' AND '$(TargetGroup)' == 'netstandard'">
5756
<Compile Include="Microsoft\Data\SqlClient\SqlDelegatedTransaction.NetStandard.cs" />
5857
<Compile Include="Microsoft\Data\SqlClient\TdsParser.NetStandard.cs" />
59-
<Compile Include="Microsoft\Data\SqlClient\SNI\SNIPacket.NetStandard.cs" />
6058
</ItemGroup>
6159
<ItemGroup Condition="'$(IsPartialFacadeAssembly)' != 'true' AND '$(OSGroup)' != 'AnyOS'">
6260
<Compile Include="Microsoft\Data\SqlClient\Server\ITypedGetters.cs" />

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.NetCoreApp.cs

Lines changed: 0 additions & 125 deletions
This file was deleted.

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.NetStandard.cs

Lines changed: 0 additions & 120 deletions
This file was deleted.

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

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
using System;
66
using System.Buffers;
77
using System.IO;
8+
using System.Threading;
9+
using System.Threading.Tasks;
810

911
namespace Microsoft.Data.SqlClient.SNI
1012
{
1113
/// <summary>
1214
/// SNI Packet
1315
/// </summary>
14-
internal partial class SNIPacket : IDisposable, IEquatable<SNIPacket>
16+
internal class SNIPacket : IDisposable, IEquatable<SNIPacket>
1517
{
1618
private byte[] _data;
1719
private int _length;
@@ -251,6 +253,46 @@ public void ReadFromStream(Stream stream)
251253
_length = stream.Read(_data, 0, _capacity);
252254
}
253255

256+
/// <summary>
257+
/// Read data from a stream asynchronously
258+
/// </summary>
259+
/// <param name="stream">Stream to read from</param>
260+
/// <param name="callback">Completion callback</param>
261+
public void ReadFromStreamAsync(Stream stream, SNIAsyncCallback callback)
262+
{
263+
bool error = false;
264+
265+
stream.ReadAsync(_data, 0, _capacity, CancellationToken.None).ContinueWith(t =>
266+
{
267+
Exception e = t.Exception?.InnerException;
268+
if (e != null)
269+
{
270+
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, SNICommon.InternalExceptionError, e);
271+
error = true;
272+
}
273+
else
274+
{
275+
_length = t.Result;
276+
277+
if (_length == 0)
278+
{
279+
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, SNICommon.ConnTerminatedError, string.Empty);
280+
error = true;
281+
}
282+
}
283+
284+
if (error)
285+
{
286+
Release();
287+
}
288+
289+
callback(this, error ? TdsEnums.SNI_ERROR : TdsEnums.SNI_SUCCESS);
290+
},
291+
CancellationToken.None,
292+
TaskContinuationOptions.DenyChildAttach,
293+
TaskScheduler.Default);
294+
}
295+
254296
/// <summary>
255297
/// Write data to a stream synchronously
256298
/// </summary>
@@ -260,6 +302,33 @@ public void WriteToStream(Stream stream)
260302
stream.Write(_data, 0, _length);
261303
}
262304

305+
/// <summary>
306+
/// Write data to a stream asynchronously
307+
/// </summary>
308+
/// <param name="stream">Stream to write to</param>
309+
/// <param name="callback">SNI Asynchronous Callback</param>
310+
/// <param name="provider">SNI provider identifier</param>
311+
/// <param name="disposeAfterWriteAsync">Bool flag to decide whether or not to dispose after Write Async operation</param>
312+
public async void WriteToStreamAsync(Stream stream, SNIAsyncCallback callback, SNIProviders provider, bool disposeAfterWriteAsync = false)
313+
{
314+
uint status = TdsEnums.SNI_SUCCESS;
315+
try
316+
{
317+
await stream.WriteAsync(_data, 0, _length, CancellationToken.None).ConfigureAwait(false);
318+
}
319+
catch (Exception e)
320+
{
321+
SNILoadHandle.SingletonInstance.LastError = new SNIError(provider, SNICommon.InternalExceptionError, e);
322+
status = TdsEnums.SNI_ERROR;
323+
}
324+
callback(this, status);
325+
326+
if (disposeAfterWriteAsync)
327+
{
328+
Dispose();
329+
}
330+
}
331+
263332
/// <summary>
264333
/// Get hash code
265334
/// </summary>

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484
</ItemGroup>
8585
<ItemGroup>
8686
<None Include="Microsoft.Data.SqlClientKey.snk" />
87-
<None Include="packages.config" />
8887
</ItemGroup>
8988
<ItemGroup>
9089
<Compile Include="Microsoft\Data\Interop\SNINativeMethodWrapper.cs" />
@@ -340,8 +339,14 @@
340339
</COMReference>
341340
</ItemGroup>
342341
<ItemGroup>
342+
<PackageReference Include="System.Security.Cryptography.Algorithms">
343+
<Version>4.3.1</Version>
344+
</PackageReference>
345+
<PackageReference Include="System.Security.Cryptography.Primitives">
346+
<Version>4.3.0</Version>
347+
</PackageReference>
343348
<PackageReference Include="Microsoft.Data.SqlClient.SNI">
344-
<Version>1.1.0</Version>
349+
<Version>[1.1.0,1.2.0)</Version>
345350
</PackageReference>
346351
<PackageReference Include="Microsoft.Identity.Client">
347352
<Version>3.0.8</Version>

0 commit comments

Comments
 (0)