Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Bug in v7.1.0 with CloseAndFlush hanging app during exit. #226

Closed
hastarin opened this issue Mar 20, 2019 · 7 comments
Closed

Bug in v7.1.0 with CloseAndFlush hanging app during exit. #226

hastarin opened this issue Mar 20, 2019 · 7 comments
Labels

Comments

@hastarin
Copy link

hastarin commented Mar 20, 2019

What version of Serilog.Sinks.Elasticsearch is affected? Please list the related NuGet package.
v7.1.0

What is the target framework and operating system?
4.7.1

Please describe the current behavior?
When calling Log.CloseAndFlush() my application hangs.

Stack trace when hitting pause in a debug session:

 	mscorlib.dll!System.Threading.SynchronizationContext.InvokeWaitMethodHelper(System.Threading.SynchronizationContext syncContext, System.IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)	Unknown
 	[Native to Managed Transition]	
 	[Managed to Native Transition]	
 	mscorlib.dll!System.Threading.Monitor.Wait(object obj, int millisecondsTimeout, bool exitContext)	Unknown
 	mscorlib.dll!System.Threading.Monitor.Wait(object obj, int millisecondsTimeout)	Unknown
 	mscorlib.dll!System.Threading.ManualResetEventSlim.Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken)	Unknown
 	mscorlib.dll!System.Threading.Tasks.Task.SpinThenBlockingWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken)	Unknown
 	mscorlib.dll!System.Threading.Tasks.Task.InternalWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken)	Unknown
 	mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task task)	Unknown
 	Serilog.Sinks.Elasticsearch.dll!Serilog.Sinks.Elasticsearch.Durable.LogShipper<System.Collections.Generic.List<string>>.CloseAndFlush()	Unknown
 	Serilog.Sinks.Elasticsearch.dll!Serilog.Sinks.Elasticsearch.Durable.DurableElasticsearchSink.Dispose()	Unknown
 	Serilog.dll!Serilog.Core.Sinks.RestrictedSink.Dispose()	Unknown
 	Serilog.dll!Serilog.LoggerConfiguration.CreateLogger.__Dispose|0()	Unknown
 	Serilog.dll!Serilog.Core.Logger.Dispose()	Unknown
 	Serilog.dll!Serilog.Log.CloseAndFlush()	Unknown

Please describe the expected behavior?
I would expect the previous behavior, or a small extra delay to ensure a flush occurs, but not a hang.

@mivano
Copy link
Contributor

mivano commented Mar 23, 2019

You are correct, it should certainly not hang, but make sure all buffered events are flushed inside the sinks and then dispose of the logger. Is there any logging in the Selflog of Serilog or in the Elasticsearch server?

@hastarin
Copy link
Author

@mivano My apologies I only just noticed this. I had to roll back the version as this was a production system so I'm afraid I can't help with further logs.

@mivano
Copy link
Contributor

mivano commented Apr 16, 2019

Then I m afraid that it is pretty hard to support this issue. Closing for now, feel free to reopen when you have more details.

@mivano mivano closed this as completed Apr 16, 2019
@hastarin
Copy link
Author

@mivano Totally understood thanks. FYI I did a quick local test and did not have the problem so perhaps it was a conflict with another package that I have since upgraded.

@mivano
Copy link
Contributor

mivano commented Apr 18, 2019

Great. When it happens again, make sure to enable the selflog of Serilog. The sink will write any exceptions to there.

@ghost
Copy link

ghost commented Jul 15, 2019

I have been seeing this behaviour ever since DurableSink started flushing to elastic on shutdown.

The problem is rather obvious:

void CloseAndFlush()
{
    /* ... */

    OnTick().GetAwaiter().GetResult();
}

/* ... */

protected virtual async Task OnTick()
{
    /* ... */
}

The CloseAndFlush method is blocking on an async method, which will almost always deadlock. In my experience, the only way to reliably block on an asynchronous method without deadlocking is running it on another thread:

void CloseAndFlush()
{
    /* ... */

    Task.Run(OnTick).Wait();
}

@mivano
Copy link
Contributor

mivano commented Jul 17, 2019

Thanks for the pointer. We will most likely get rid of the durable mode here soon. Causing more issues then actually delivering value #254

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants