dotnet / dotnet/BenchmarkDotNet
ObjectDisposedException during logging (0.13.5)
- Dominant language
- C#
- Stars
- 11.5k
- Forks
- 1.1k
- Avg merge
- 6d 11h
- Merged PRs (30d)
- 12
Description
I'm observing the following exception whilst trying to run a benchmark on our build server ([Concourse](https://concourse-ci.org/)):
```
The active test run was aborted. Reason: Test host process crashed : Unhandled exception. System.ObjectDisposedException: Cannot write to a closed TextWriter.
Object name: 'StreamWriter'.
at System.IO.StreamWriter.g__ThrowObjectDisposedException|77_0()
at System.IO.StreamWriter.WriteLine(String value)
at BenchmarkDotNet.Loggers.StreamLogger.WriteLine(LogKind logKind, String text)
at BenchmarkDotNet.Loggers.CompositeLogger.WriteLine(LogKind logKind, String text)
at BenchmarkDotNet.Loggers.LoggerExtensions.WriteLineError(ILogger logger, String text)
at BenchmarkDotNet.Toolchains.InProcess.Emit.InProcessEmitExecutor.ExecuteCore(IHost host, ExecuteParameters parameters)
at BenchmarkDotNet.Toolchains.InProcess.Emit.InProcessEmitExecutor.<>c__DisplayClass9_0.b__0()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Test Run Aborted with error System.Exception: One or more errors occurred.
---> System.Exception: Unable to read beyond the end of the stream.
at System.IO.BinaryReader.Read7BitEncodedInt()
at System.IO.BinaryReader.ReadString()
at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.LengthPrefixCommunicationChannel.NotifyDataAvailable()
at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.TcpClientExtensions.MessageLoopAsync(TcpClient client, ICommunicationChannel channel, Action`1 errorHandler, CancellationToken cancellationToken)
--- End of inner exception stack trace ---.
```
I believe the second part of the stacktrace (`Unable to read beyond the end of the stream`) is potentially not relevant, as it seems to happen when the test-host crashes / exits ungracefully:
https://github.com/microsoft/vstest/issues/3658
I am unable to reproduce the problem locally though. I have tried a few different logging configurations (i.e. ConsoleLogger, AccumulatorLogger), but both fail on the build server.
Turning the logging off **does** prevent the problem. But the benchmark then fails with the following (after running for 7 minutes):
```
System.InvalidOperationException : Benchmark PortfoliosApiCashflowBenchmarks.RunTestJobForUpsertableCashflowsMemoryUsage: ShortRun(Server=True, Toolchain=InProcessEmitToolchain, InvocationCount=1, IterationCount=1, LaunchCount=1, UnrollFactor=1, WarmupCount=3) [instrumentTypeForCashflowsMemoryBenchmark=Bond, numberInstrumentsForCashflowsMemoryBenchmark=1, numberTransactionsPerInstrumentForCashflowsMemoryBenchmark=200] takes too long to run. Prefer to use out-of-process toolchains for long-running benchmarks.
```
That is also confusing, as the equivalent benchmark completes rapidly when running locally (hence my desire to get the log output to see what's going on).
Contributor guide
Assessment
This issue has not been assessed yet.