Skip to content

Commit

Permalink
Revert Async changes to SNIPacket to fix deadlock issues (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra authored and stephentoub committed Jan 15, 2020
1 parent b8f8815 commit 3d7fcaf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,10 @@
<Compile Include="System\Data\SqlClient\SqlConnectionStringBuilder.NetCoreApp.cs" />
<Compile Include="System\Data\SqlClient\SqlDelegatedTransaction.NetCoreApp.cs" />
<Compile Include="System\Data\SqlClient\TdsParser.NetCoreApp.cs" />
<Compile Include="System\Data\SqlClient\SNI\SNIPacket.NetCoreApp.cs" />
</ItemGroup>
<ItemGroup Condition="'$(IsPartialFacadeAssembly)' != 'true' and '$(OSGroup)' != 'AnyOS' and '$(TargetsNetCoreApp)' != 'true'">
<Compile Include="System\Data\SqlClient\SqlDelegatedTransaction.NetStandard.cs" />
<Compile Include="System\Data\SqlClient\TdsParser.NetStandard.cs" />
<Compile Include="System\Data\SqlClient\SNI\SNIPacket.NetStandard.cs" />
</ItemGroup>
<!-- Manage the SNI toggle for Windows netstandard and UWP -->
<ItemGroup Condition="('$(TargetFramework)' == 'netstandard2.0' or '$(TargetsNetCoreApp)' == 'true') and '$(TargetsWindows)' == 'true'">
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace System.Data.SqlClient.SNI
{
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 @@ -173,6 +173,46 @@ public void Release()
_completionCallback = null;
}

/// <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>
/// Read data from a stream synchronously
/// </summary>
Expand All @@ -182,6 +222,33 @@ public void ReadFromStream(Stream stream)
_dataLength = stream.Read(_data, _headerLength, _dataCapacity);
}

/// <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>
/// Write data to a stream synchronously
/// </summary>
Expand Down

0 comments on commit 3d7fcaf

Please sign in to comment.