Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Diagnostics.TextWriterTraceListener: System.ArgumentOutOfRangeException: Count cannot be less than zero. Parameter name: count #101600

Open
HareeshvareU opened this issue Apr 26, 2024 · 5 comments
Labels
area-System.IO untriaged New issue has not been triaged by the area owner

Comments

@HareeshvareU
Copy link

HareeshvareU commented Apr 26, 2024

We have a app hosted in AWS and we get this exception in our database once a while bringing the application down. A simple restart of the services makes it work again. This issue has been flagged in Github at multiple places but there is no resolution or root cause for this . Can someone please help here.
.NET framework - 4.8
Stack trace is below:
Exception
ArgumentOutOfRangeException
StackTrace
at System.String.CopyTo(Int32 sourceIndex, Char[] destination, Int32 destinationIndex, Int32 count) at System.IO.StreamWriter.Write(String value) at System.IO.TextWriter.SyncTextWriter.Write(String value) at System.Diagnostics.TextWriterTraceListener.Write(String message) at System.Diagnostics.TraceListener.WriteHeader(String source, TraceEventType eventType, Int32 id) at System.Diagnostics.TraceListener.TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, String message) at System.Diagnostics.TraceInternal.TraceEvent(TraceEventType eventType, Int32 id, String format, Object[] args) at System.Web.Http.Tracing.SystemDiagnosticsTraceWriter.TraceMessage(TraceLevel level, String message) at System.Web.Http.Tracing.SystemDiagnosticsTraceWriter.Trace(HttpRequestMessage request, String category, TraceLevel level, Action1 traceAction) at System.Web.Http.Tracing.ITraceWriterExtensions.TraceError(ITraceWriter traceWriter, Exception exception, HttpRequestMessage request, String category, String operatorName, String operationName, Action1 errorTrace) at System.Web.Http.Tracing.ITraceWriterExtensions.d__24.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Owin.HttpMessageHandlerAdapter.d__13.MoveNext()

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 26, 2024
Copy link
Contributor

Tagging subscribers to this area: @tarekgh, @tommcdon, @pjanotti
See info in area-owners.md if you want to be subscribed.

@jkotas jkotas changed the title System.ArgumentOutOfRangeException: Count cannot be less than zero. Parameter name: count System.Diagnostics.TextWriterTraceListener: System.ArgumentOutOfRangeException: Count cannot be less than zero. Parameter name: count Apr 26, 2024
@tarekgh
Copy link
Member

tarekgh commented Apr 26, 2024

This is seems Sysstem.IO issue. Here is a readable stack:

at System.String.CopyTo(Int32 sourceIndex, Char[] destination, Int32 destinationIndex, Int32 count) 
at System.IO.StreamWriter.Write(String value) 
at System.IO.TextWriter.SyncTextWriter.Write(String value) 
at System.Diagnostics.TextWriterTraceListener.Write(String message) 
at System.Diagnostics.TraceListener.WriteHeader(String source, TraceEventType eventType, Int32 id) 
at System.Diagnostics.TraceListener.TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, String message) 
at System.Diagnostics.TraceInternal.TraceEvent(TraceEventType eventType, Int32 id, String format, Object[] args) 
at System.Web.Http.Tracing.SystemDiagnosticsTraceWriter.TraceMessage(TraceLevel level, String message) 
at System.Web.Http.Tracing.SystemDiagnosticsTraceWriter.Trace(HttpRequestMessage request, String category, TraceLevel level, Action1 traceAction) 
at System.Web.Http.Tracing.ITraceWriterExtensions.TraceError(ITraceWriter traceWriter, Exception exception, HttpRequestMessage request, String category, String operatorName, String operationName, Action1 errorTrace) 
at System.Web.Http.Tracing.ITraceWriterExtensions.d__24.MoveNext() --- End of stack trace from previous location where exception was thrown --- 
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
at System.Web.Http.Owin.HttpMessageHandlerAdapter.d__13.MoveNext()

The string that trying to write to the stream is originally created by a call like string.Create(CultureInfo.InvariantCulture, stackalloc char[256], $"{source} {eventType}: {id} : ") https://source.dot.net/#System.Diagnostics.TraceSource/System/Diagnostics/TraceListener.cs,384 which I am not sure if this is what causes the problem?

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

@stephentoub
Copy link
Member

The original poster cited running on .NET Framework 4.8.

@tarekgh
Copy link
Member

tarekgh commented Apr 26, 2024

@stephentoub good point.

on netfx, the string is created normally using String.Format(CultureInfo.InvariantCulture, "{0} {1}: {2} : ", source, eventType.ToString(), id.ToString(CultureInfo.InvariantCulture)) https://referencesource.microsoft.com/#System/compmod/system/diagnostics/TraceListener.cs,404.

The code will throw in the line https://referencesource.microsoft.com/#mscorlib/system/io/streamwriter.cs,437 which has assert statement in the previous line suggesting what the problem could be: Contract.Assert(n > 0, "StreamWriter::Write(String) isn't making progress! This is most likely a race condition in user code.");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.IO untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants