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

Fixes corrupted connection issue when an exception occurs during RPC execution with TVP types #1068

Merged
merged 3 commits into from
May 17, 2021
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
2 changes: 1 addition & 1 deletion build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<FunctionalTests Include="**/tools/Microsoft.DotNet.XUnitExtensions/Microsoft.DotNet.XUnitExtensions.csproj" />
<FunctionalTests Include="**/tools/CoreFx.Private.TestUtilities/CoreFx.Private.TestUtilities.csproj" />
<FunctionalTests Include="**/ManualTests/SQL/UdtTest/UDTs/Address/Address.csproj" />
<FunctionalTests Include="**/Microsoft.Data.SqlClient.Tests.csproj" />
<FunctionalTests Include="**/FunctionalTests/Microsoft.Data.SqlClient.Tests.csproj" />
JRahnama marked this conversation as resolved.
Show resolved Hide resolved

<ManualTests Condition="$(ReferenceType.Contains('NetStandard'))" Include="**/NSLibrary/Microsoft.Data.SqlClient.NSLibrary.csproj" />
<ManualTests Include="**/ManualTests/SQL/UdtTest/UDTs/Address/Address.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9106,13 +9106,7 @@ internal Task TdsExecuteRPC(SqlCommand cmd, _SqlRPC[] rpcArray, int timeout, boo
}
catch (Exception e)
{
if (!ADP.IsCatchableExceptionType(e))
{
throw;
}

FailureCleanup(stateObj, e);

throw;
}
FinalizeExecuteRPC(stateObj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10441,14 +10441,7 @@ internal Task TdsExecuteRPC(SqlCommand cmd, _SqlRPC[] rpcArray, int timeout, boo
}
catch (Exception e)
{
// UNDONE - should not be catching all exceptions!!!
if (!ADP.IsCatchableExceptionType(e))
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
{
throw;
}

FailureCleanup(stateObj, e);

throw;
}
FinalizeExecuteRPC(stateObj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Transactions;
using Microsoft.Data.SqlClient.Server;
using Xunit;
using System.Linq;

namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
Expand Down Expand Up @@ -82,6 +83,97 @@ public void TestPacketNumberWraparound()
Assert.True(enumerator.MaxCount == enumerator.Count);
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
public void TestConnectionIsSafeToReuse()
{
using SqlConnection connection = new(DataTestUtility.TCPConnectionString);

// Bad Scenario - exception expected.
try
{
List<A> list = new()
{
new A(0),
null,
new A(2),
new A(3),
new A(4),
new A(5)
};

IEnumerable<int> Ids = list.Select(x => x.id.Value).Distinct();

var sqlParam = new SqlParameter("ids", SqlDbType.Structured)
{
TypeName = "dbo.TableOfIntId",
SqlValue = Ids.Select(x =>
{
SqlDataRecord rec = new(new[] { new SqlMetaData("Id", SqlDbType.Int) });
rec.SetInt32(0, x);
return rec;
})
};

var parameters = new List<SqlParameter>() { sqlParam };
const string SQL = @"SELECT * FROM information_schema.COLUMNS cols INNER JOIN @ids Ids on Ids.id = cols.ORDINAL_POSITION";
using SqlCommand cmd = new(SQL, connection);
cmd.CommandTimeout = 100;
AddCommandParameters(cmd, parameters);
new SqlDataAdapter(cmd).Fill(new("BadFunc"));
}
catch
{
// Ignore this exception as it's deliberately introduced.
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
}

// Good Scenario - No failure expected.
try
{
const string SQL = @"SELECT * FROM information_schema.tables WHERE TABLE_NAME = @TableName";
var parameters = new List<SqlParameter>() { new SqlParameter("@TableName", "Temp") };
using SqlCommand cmd = new(SQL, connection);
cmd.CommandTimeout = 100;
AddCommandParameters(cmd, parameters);
new SqlDataAdapter(cmd).Fill(new("GoodFunc"));
}
catch (Exception e)
{
Assert.False(true, $"Unexpected error occurred: {e.Message}");
}
}

private class A
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
{
public A(int? v)
{
id = v;
}
public int? id { get; set; }
}

static internal void AddCommandParameters(SqlCommand command, IEnumerable parameters)
{
if (parameters == null)
return;

foreach (SqlParameter p in parameters)
{
if (p == null)
continue;

if (p.Value == null)
{
var clone = (SqlParameter)((ICloneable)p).Clone();
clone.Value = DBNull.Value;
command.Parameters.Add(clone);
}
else
{
command.Parameters.Add(p);
}
}
}

public TvpTest()
{
_connStr = DataTestUtility.TCPConnectionString;
Expand Down Expand Up @@ -693,7 +785,7 @@ private bool AllowableDifference(byte[] source, object result, StePermutation me
// allowable max-length adjustments
if (maxLength == resultBytes.Length)
{ // a bit optimistic, but what the heck.
// truncation
// truncation
if (maxLength <= source.Length)
{
returnValue = true;
Expand Down