Skip to content

Commit

Permalink
Revert Async changes to SNIPacket to fix deadlock issues (#349)
Browse files Browse the repository at this point in the history
Fixes #262 by reverting dotnet/corefx#34184 until PR #335 is ready with complete fix.
  • Loading branch information
cheenamalhotra authored Dec 13, 2019
1 parent c95b0b3 commit 4eb89ef
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 248 deletions.
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 @@ -6,13 +6,15 @@
using System.Buffers;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Data.SqlClient.SNI
{
/// <summary>
/// SNI Packet
/// </summary>
internal sealed partial class SNIPacket
internal sealed class SNIPacket
{
private int _dataLength; // the length of the data in the data segment, advanced by Append-ing data, does not include smux header length
private int _dataCapacity; // the total capacity requested, if the array is rented this may be less than the _data.Length, does not include smux header length
Expand Down Expand Up @@ -185,6 +187,46 @@ public void ReadFromStream(Stream stream)
_dataLength = stream.Read(_data, _headerLength, _dataCapacity);
}

/// <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, _dataCapacity, 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
{
_dataLength = t.Result;
if (_dataLength == 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 @@ -194,6 +236,33 @@ public void WriteToStream(Stream stream)
stream.Write(_data, _headerLength, _dataLength);
}

/// <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, _dataLength, 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

0 comments on commit 4eb89ef

Please sign in to comment.