Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Release 1.1] Revert Async changes in SNIPacket to fix deadlock issues + Update SNI Package Reference #425

Merged
merged 3 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -341,7 +341,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient.SNI">
<Version>1.1.0</Version>
<Version>1.1.*</Version>
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
</PackageReference>
<PackageReference Include="Microsoft.Identity.Client">
<Version>3.0.8</Version>
Expand Down
5 changes: 1 addition & 4 deletions tools/specs/Microsoft.Data.SqlClient.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ Microsoft.Data.SqlClient.SqlTransaction
Microsoft.Data.SqlClient.SqlParameterCollection
Microsoft.Data.SqlClient.SqlClientFactory

When running on Windows, this library has a dependency on Microsoft.Data.SqlClient.SNI on .NET Framework and runtime.native.System.Data.SqlClient.sni on .NET Core, which requires the Microsoft Visual C++ Redistributable to be installed:
https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads

When using NuGet 3.x this package requires at least version 3.4.</description>
<releaseNotes>https://go.microsoft.com/fwlink/?linkid=2090501</releaseNotes>
<copyright>© Microsoft Corporation. All rights reserved.</copyright>
<tags>sqlclient microsoft.data.sqlclient</tags>
<dependencies>
<group targetFramework="net46">
<dependency id="System.Data.Common" version="4.3.0" />
<dependency id="Microsoft.Data.SqlClient.sni" version="1.1.0" />
<dependency id="Microsoft.Data.SqlClient.sni" version="[1.1.0, )" />
<dependency id="Microsoft.Identity.Client" version="3.0.8" />
<dependency id="Microsoft.IdentityModel.Protocols.OpenIdConnect" version="5.5.0" />
<dependency id="Microsoft.IdentityModel.JsonWebTokens" version="5.5.0" />
Expand Down