Joh*_*ohn 3 c# objectdisposedexception async-await amazon-cloudwatch aws-lambda
我在 AWS Lambda 的 CloudWatch 日志中注意到了此异常。
一切似乎都已得到处理,因此我认为这是在 Lambda 完成执行后创建的 AWS 代码(而不是我编写的代码)中的异常。
由于它在功能上有效,所以我一直忽略它,但我担心可能存在我没有注意到的问题。
Lambda 通过“TaskScheduler.UnobservedTaskException”事件遇到 UnobservedTaskException:
{
"errorType": "AggregateException",
"errorMessage": "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. (Cannot access a disposed object.\nObject name: 'System.Net.Sockets.UdpClient'.)",
"cause": {
"errorType": "ObjectDisposedException",
"errorMessage": "Cannot access a disposed object.\nObject name: 'System.Net.Sockets.UdpClient'.",
"stackTrace": [
"at System.Net.Sockets.UdpClient.EndReceive(IAsyncResult asyncResult, IPEndPoint& remoteEP)",
"at System.Net.Sockets.UdpClient.<>c.<ReceiveAsync>b__56_1(IAsyncResult asyncResult)",
"at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)"
]
},
"causes": [ {
"errorType": "ObjectDisposedException",
"errorMessage": "Cannot access a disposed object.\nObject name: 'System.Net.Sockets.UdpClient'.",
"stackTrace": [
"at System.Net.Sockets.UdpClient.EndReceive(IAsyncResult asyncResult, IPEndPoint& remoteEP)",
"at System.Net.Sockets.UdpClient.<>c.<ReceiveAsync>b__56_1(IAsyncResult asyncResult)",
"at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)"
]
}]
}
Run Code Online (Sandbox Code Playgroud)
lambda 代码非常简单:它SNS messages使用Dapper.
我认为我async在 Fucntion 处理程序中的操作可能存在一些问题。有任何想法吗?
public class Function
{
private static string _connectionString;
public async Task<IEnumerable<InsertSnsResult>> FunctionHandler(SNSEvent @event, ILambdaContext context)
{
try
{
context.Logger.LogLine("Adding SNS Messages");
_connectionString = _connectionString ?? await DecryptHelper.DecryptEnvironmentVariableAsync("ConnectionString").ConfigureAwait(false);
var handler = new AddSnsMessageHandler(new SnsMessagesRepository(_connectionString, context.Logger));
return await handler.AddSnsEvents(@event).ConfigureAwait(false);
}
catch (Exception e)
{
context.Logger.LogLine(e.Message);
throw;
}
finally
{
context.Logger.LogLine("Finished SNS Adding Messages");
}
}
}
Run Code Online (Sandbox Code Playgroud)
[编辑]
这里要明确的是,这个异常不会被 try/catch 块捕获。如果是的话,那就不是一个了UnobservedTaskException。这就是为什么我很难找到问题的根源。
这是存储库代码
public async Task<List<InsertSnsResult>> InsertSnsMessages(IEnumerable<SnsEvent> records)
{
using (var connection = new SqlConnection(_connectionString))
{
await connection.OpenAsync().ConfigureAwait(false);
var results = new List<InsertSnsResult>();
foreach (var record in records)
{
try
{
await connection.ExecuteAsync(InsertEventCommand, record).ConfigureAwait(false);
results.Add(new InsertSnsResult(record.CorrelationId, true));
}
catch (Exception ex)
{
_logger.LogLine($"InsertSns failed for {record.Id}. {ex.Message}");
results.Add(new InsertSnsResult(record.CorrelationId, false));
}
}
return results;
}
}
Run Code Online (Sandbox Code Playgroud)
日志消息很简单并解释了发生的情况:
由于它在功能上有效,所以我一直忽略它
我曾经听说,当一家工厂着火并被烧毁时,平均有七种不同的安全系统被人们忽视或禁用。改掉这种认为它有效,所以它一定是安全的习惯。也许没什么,但我会认为这些消息表明存在严重问题,直到我有其他证据为止。
| 归档时间: |
|
| 查看次数: |
1864 次 |
| 最近记录: |