Skip to content

Too many UnobservedTaskException #192

@nelsoncvjr-stone

Description

@nelsoncvjr-stone

Application info

Framework: .NET 8.0
Package: DistributedLock.SqlServer 1.0.3

I have the following code:

LockManager

public async ValueTask<LockHandlerToken> TryAcquireLockAsync(string lockName, CancellationToken cancellationToken)
{
    var distributedLock = new SqlDistributedLock(lockName, _connectionString);

    var handler = await distributedLock.TryAcquireAsync(TimeSpan.FromSeconds(5), cancellationToken);

    if (handler != null)
    {
        return new LockHandlerToken(handler, handler.HandleLostToken);
    }

    return LockHandlerToken.None;
}

The LockHandlerToken only wrap the LockHandle provided by your lib as IDisposable (in this case SqlDistributedLockHandle).

Controller's action

using var lockHandlerToken = await _runManager.TryAcquireLockAsync(JobName, cancellationToken);

if (!lockHandlerToken.IsAcquired())
{
    // my logger
    return;
}

// do something

Using TaskScheduler.UnobservedTaskException callback I handling the exceptions.

I would like to know if this behavior is by design/expected by lib or not?

Exception

{
    "ClassName": "AggregateException",
    "FullClassName": "System.AggregateException",
    "InnerError": {
        "ClassName": "SqlException",
        "FullClassName": "Microsoft.Data.SqlClient.SqlException",
        "InnerError": null,
        "Message": "The request failed to run because the batch is aborted, this can be caused by abort signal sent from client, or another request is running in the same session, which makes the session busy.\r\nOperation cancelled by user.",
        "Source": "Core Microsoft SqlClient Data Provider",
        "StackTrace": "   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)\r\n   at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)\r\n   at Microsoft.Data.SqlClient.SqlCommand.CompleteAsyncExecuteReader(Boolean isInternal, Boolean forDescribeParameterEncryption)\r\n   at Microsoft.Data.SqlClient.SqlCommand.InternalEndExecuteNonQuery(IAsyncResult asyncResult, Boolean isInternal, String endMethod)\r\n   at Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult asyncResult)\r\n   at Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryAsync(IAsyncResult asyncResult)\r\n   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)\r\n--- End of stack trace from previous location ---\r\n   at Medallion.Threading.Internal.Data.DatabaseCommand.InternalExecuteAndPropagateCancellationAsync[TState,TResult](TState state, Func`3 executeAsync, CancellationToken cancellationToken, Boolean isConnectionMonitoringQuery) in /_/DistributedLock.Core/Internal/Data/DatabaseCommand.cs:line 167\r\n   at Medallion.Threading.Internal.Data.DatabaseCommand.ExecuteAsync[TResult](Func`3 executeAsync, Func`2 executeSync, CancellationToken cancellationToken, Boolean disallowAsyncCancellation, Boolean isConnectionMonitoringQuery) in /_/DistributedLock.Core/Internal/Data/DatabaseCommand.cs:line 92\r\n   at Medallion.Threading.SqlServer.SqlDatabaseConnection.SleepAsync(TimeSpan sleepTime, CancellationToken cancellationToken, Func`3 executor) in /_/src/DistributedLock.SqlServer/SqlDatabaseConnection.cs:line 65"
    },
    "Message": "A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (The request failed to run because the batch is aborted, this can be caused by abort signal sent from client, or another request is running in the same session, which makes the session busy.\r\nOperation cancelled by user.)",
    "Source": null,
    "StackTrace": null
}

More readable stacktrace:

at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
at Microsoft.Data.SqlClient.SqlCommand.CompleteAsyncExecuteReader(Boolean isInternal, Boolean forDescribeParameterEncryption)
at Microsoft.Data.SqlClient.SqlCommand.InternalEndExecuteNonQuery(IAsyncResult asyncResult, Boolean isInternal, String endMethod)
at Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult asyncResult)
at Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryAsync(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location ---
at Medallion.Threading.Internal.Data.DatabaseCommand.InternalExecuteAndPropagateCancellationAsync[TState,TResult](TState state, Func`3 executeAsync, CancellationToken cancellationToken, Boolean isConnectionMonitoringQuery) in /_/DistributedLock.Core/Internal/Data/DatabaseCommand.cs:line 167
at Medallion.Threading.Internal.Data.DatabaseCommand.ExecuteAsync[TResult](Func`3 executeAsync, Func`2 executeSync, CancellationToken cancellationToken, Boolean disallowAsyncCancellation, Boolean isConnectionMonitoringQuery) in /_/DistributedLock.Core/Internal/Data/DatabaseCommand.cs:line 92
at Medallion.Threading.SqlServer.SqlDatabaseConnection.SleepAsync(TimeSpan sleepTime, CancellationToken cancellationToken, Func`3 executor) in /_/src/DistributedLock.SqlServer/SqlDatabaseConnection.cs:line 65

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions