serilog-sinks-seq icon indicating copy to clipboard operation
serilog-sinks-seq copied to clipboard

`Log.CloseAndFlush()` deadlock on AWS Lambda

Open nikoudel opened this issue 3 years ago • 1 comments

Log.CloseAndFlush() seems to be deadlocking when run in AWS Lambda and my Seq server instance goes down for some (irrelevant) reason making Seq a single point of failure for the whole microservice gang. This is how it looks like in the logs:

LoggerTimeout

This only happens when running in AWS Lambda environment. In a console app execution continues after a graceful timeout of ~4 seconds. I suspect it might be related to this and followed this advice to make it behave better:

...
finally
{
    Task.Run(Log.CloseAndFlush).Wait(1000);
}

It works but still looks like a hack.

nikoudel avatar Feb 08 '22 18:02 nikoudel

Thanks for the report, we'll dig in 👍

nblumhardt avatar Feb 08 '22 22:02 nblumhardt

We're affected by the same problem. CloseAndFlush causes in non-debug environments to deadlock. One of our engineers pointed out that a lock and tasks cause tasks running in a sync context - which then deadlocks by accident.

This only happens with a config in which the seq sink is added, the others we use (console, eventlog) are unaffected.

mio-moto avatar Sep 09 '22 07:09 mio-moto

Hi @DarkMio - thanks for reaching out. Starting work on a proper fix for this now via https://github.com/serilog/serilog/pull/1750.

nblumhardt avatar Sep 09 '22 09:09 nblumhardt

Very cool, that was just in time. Thanks @nblumhardt!

mio-moto avatar Sep 09 '22 10:09 mio-moto

Hi @DarkMio; v5.2.0 is out now and includes this fix. I'll close the ticket as completed but if you notice any ongoing issues please let us know!

nblumhardt avatar Sep 13 '22 05:09 nblumhardt

Ah sorry, also, for this to work you'll need to asynchronously dispose the Serilog Logger. If you're using var logger = new LoggerConfiguration().CreateLogger() that maps to await using; otherwise, using the Log class it's await Log.CloseAndFlushAsync().

If you're on Serilog.AspNetCore and configuring Serilog in the UseSerilog(lc => ...) callback, you'll need:

https://github.com/serilog/serilog-extensions-hosting/issues/67

nblumhardt avatar Sep 13 '22 05:09 nblumhardt