Skip to content

Unobserved task exception bugfix #254

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ protected override async Task WaitForNotifications(
sqlCommand.CommandTimeout = 0;
this.WriteTraceMessage(TraceLevel.Verbose, "Executing WAITFOR command.");

using (var sqlDataReader = await sqlCommand.ExecuteReaderAsync(cancellationToken).WithCancellation(cancellationToken))
using (var sqlDataReader = await sqlCommand.ExecuteReaderAsync(cancellationToken))
{
if (_throwExceptionInWaitForNotificationsPoint3) throw new Exception();

Expand Down
99 changes: 99 additions & 0 deletions TableDependency.SqlClient.Test/Issue253Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.Data.SqlClient;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TableDependency.SqlClient.Test
{
[TestClass]
public class Issue253Test : Base.SqlTableDependencyBaseTest
{
private class Issue253Model
{
public string Id { get; set; }
}

private static readonly string TableName = nameof(Issue253Model);

private Exception _unobservedTaskException;

[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
using (var sqlConnection = new SqlConnection(ConnectionStringForTestUser))
{
sqlConnection.Open();
using (var sqlCommand = sqlConnection.CreateCommand())
{
sqlCommand.CommandText = $"IF OBJECT_ID('[{TableName}]', 'U') IS NOT NULL DROP TABLE [dbo].[{TableName}]";
sqlCommand.ExecuteNonQuery();
sqlCommand.CommandText = $"CREATE TABLE [{TableName}]([Id] [int] NULL)";
sqlCommand.ExecuteNonQuery();
}
}
}

[ClassCleanup]
public static void ClassCleanup()
{
using (var sqlConnection = new SqlConnection(ConnectionStringForTestUser))
{
sqlConnection.Open();
using (var sqlCommand = sqlConnection.CreateCommand())
{
sqlCommand.CommandText = $"IF OBJECT_ID('{TableName}', 'U') IS NOT NULL DROP TABLE [{TableName}];";
sqlCommand.ExecuteNonQuery();
}
}
}

[TestCategory("SqlServer")]
[TestMethod]
public void Test()
{
try
{
string naming;

using (var tableDependency = new SqlTableDependency<Issue253Model>(ConnectionStringForTestUser, tableName: TableName))
{
tableDependency.OnChanged += (o, args) => { };
tableDependency.Start();
naming = tableDependency.DataBaseObjectsNamingConvention;

Thread.Sleep(5000);

TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
try
{
tableDependency.Stop();

Thread.Sleep(5000);

GC.Collect();
GC.WaitForPendingFinalizers();
}
finally
{
TaskScheduler.UnobservedTaskException -= TaskSchedulerOnUnobservedTaskException;
}
}

Assert.IsTrue(base.AreAllDbObjectDisposed(naming));
Assert.IsTrue(base.CountConversationEndpoints(naming) == 0);
}
catch (Exception exception)
{
Assert.Fail(exception.Message);
}

Assert.IsNull(_unobservedTaskException);
}

private void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
_unobservedTaskException = e.Exception;
}
}
}
49 changes: 0 additions & 49 deletions TableDependency.SqlClient/Extensions/TaskExtensions.cs

This file was deleted.

2 changes: 1 addition & 1 deletion TableDependency.SqlClient/SqlTableDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ protected virtual async Task WaitForNotifications(
sqlCommand.CommandTimeout = 0;
this.WriteTraceMessage(TraceLevel.Verbose, "Executing WAITFOR command.");

using (var sqlDataReader = await sqlCommand.ExecuteReaderAsync(cancellationToken).WithCancellation(cancellationToken))
using (var sqlDataReader = await sqlCommand.ExecuteReaderAsync(cancellationToken))
{
while (sqlDataReader.Read())
{
Expand Down