Skip to content

Commit

Permalink
[Release 1.1] Revert Async changes in SNIPacket to fix deadlock issue…
Browse files Browse the repository at this point in the history
…s + Update SNI Package Reference (#425)
  • Loading branch information
cheenamalhotra authored Feb 13, 2020
1 parent d4d621c commit 5d0ca7d
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 264 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ For the .NET Framework driver on Windows, a package reference to [Microsoft.Data

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.

**Note**
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)


## Helpful Links

| Topic | Link to File |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
<Compile Include="Microsoft\Data\SqlClient\EnclaveDelegate.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlDelegatedTransaction.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParser.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\SqlClient\SNI\SNIPacket.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\SqlClient\AzureAttestationBasedEnclaveProvider.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProvider.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProviderBase.NetCoreApp.cs" />
Expand All @@ -56,7 +55,6 @@
<ItemGroup Condition="'$(IsPartialFacadeAssembly)' != 'true' AND '$(OSGroup)' != 'AnyOS' AND '$(TargetGroup)' == 'netstandard'">
<Compile Include="Microsoft\Data\SqlClient\SqlDelegatedTransaction.NetStandard.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParser.NetStandard.cs" />
<Compile Include="Microsoft\Data\SqlClient\SNI\SNIPacket.NetStandard.cs" />
</ItemGroup>
<ItemGroup Condition="'$(IsPartialFacadeAssembly)' != 'true' AND '$(OSGroup)' != 'AnyOS'">
<Compile Include="Microsoft\Data\SqlClient\Server\ITypedGetters.cs" />
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using System;
using System.Buffers;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Data.SqlClient.SNI
{
/// <summary>
/// SNI Packet
/// </summary>
internal partial class SNIPacket : IDisposable, IEquatable<SNIPacket>
internal class SNIPacket : IDisposable, IEquatable<SNIPacket>
{
private byte[] _data;
private int _length;
Expand Down Expand Up @@ -251,6 +253,46 @@ public void ReadFromStream(Stream stream)
_length = stream.Read(_data, 0, _capacity);
}

/// <summary>
/// Read data from a stream asynchronously
/// </summary>
/// <param name="stream">Stream to read from</param>
/// <param name="callback">Completion callback</param>
public void ReadFromStreamAsync(Stream stream, SNIAsyncCallback callback)
{
bool error = false;

stream.ReadAsync(_data, 0, _capacity, CancellationToken.None).ContinueWith(t =>
{
Exception e = t.Exception?.InnerException;
if (e != null)
{
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, SNICommon.InternalExceptionError, e);
error = true;
}
else
{
_length = t.Result;

if (_length == 0)
{
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, SNICommon.ConnTerminatedError, string.Empty);
error = true;
}
}

if (error)
{
Release();
}

callback(this, error ? TdsEnums.SNI_ERROR : TdsEnums.SNI_SUCCESS);
},
CancellationToken.None,
TaskContinuationOptions.DenyChildAttach,
TaskScheduler.Default);
}

/// <summary>
/// Write data to a stream synchronously
/// </summary>
Expand All @@ -260,6 +302,33 @@ public void WriteToStream(Stream stream)
stream.Write(_data, 0, _length);
}

/// <summary>
/// Write data to a stream asynchronously
/// </summary>
/// <param name="stream">Stream to write to</param>
/// <param name="callback">SNI Asynchronous Callback</param>
/// <param name="provider">SNI provider identifier</param>
/// <param name="disposeAfterWriteAsync">Bool flag to decide whether or not to dispose after Write Async operation</param>
public async void WriteToStreamAsync(Stream stream, SNIAsyncCallback callback, SNIProviders provider, bool disposeAfterWriteAsync = false)
{
uint status = TdsEnums.SNI_SUCCESS;
try
{
await stream.WriteAsync(_data, 0, _length, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception e)
{
SNILoadHandle.SingletonInstance.LastError = new SNIError(provider, SNICommon.InternalExceptionError, e);
status = TdsEnums.SNI_ERROR;
}
callback(this, status);

if (disposeAfterWriteAsync)
{
Dispose();
}
}

/// <summary>
/// Get hash code
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
</ItemGroup>
<ItemGroup>
<None Include="Microsoft.Data.SqlClientKey.snk" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="Microsoft\Data\Interop\SNINativeMethodWrapper.cs" />
Expand Down Expand Up @@ -340,8 +339,14 @@
</COMReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Security.Cryptography.Algorithms">
<Version>4.3.1</Version>
</PackageReference>
<PackageReference Include="System.Security.Cryptography.Primitives">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Data.SqlClient.SNI">
<Version>1.1.0</Version>
<Version>[1.1.0,1.2.0)</Version>
</PackageReference>
<PackageReference Include="Microsoft.Identity.Client">
<Version>3.0.8</Version>
Expand Down
Loading

0 comments on commit 5d0ca7d

Please sign in to comment.