Do not expand stacktraces when completion exception is rethrown multiple times#31375
Do not expand stacktraces when completion exception is rethrown multiple times#31375pakrym merged 2 commits intodotnet:masterfrom
Conversation
| return false; | ||
| } | ||
|
|
||
| if (_exception != s_completedNoException) |
There was a problem hiding this comment.
Remove s_completedNoException now that it is no longer used.
https://github.com/dotnet/corefx/pull/31375/files#diff-ff9ac9c14f9484dcfcaa2332b6353541L16
18dbabc to
0a32a71
Compare
| public bool IsCompleted => _isCompleted; | ||
|
|
||
| public bool IsFaulted => IsCompleted && _exception != s_completedNoException; | ||
| public bool IsFaulted => IsCompleted && _exception != null; |
There was a problem hiding this comment.
Nit: The IsCompleted check now redundant since _exception can't be non-null if not completed.
|
|
||
| private Exception _exception; | ||
| private bool _isCompleted; | ||
| private ExceptionDispatchInfo _exception; |
| Assert.Equal("Writer exception", invalidOperationException.Message); | ||
| Assert.Contains("ThrowTestException", invalidOperationException.StackTrace); | ||
|
|
||
| Assert.Single(Regex.Matches(invalidOperationException.StackTrace, "Pipe.GetReadResult")); |
There was a problem hiding this comment.
Do you also want a test that if the same source exception is passed into multiple pipes it only returns a single entry? aspnet/KestrelHttpServer#2730 (comment)
There was a problem hiding this comment.
But we can't guarantee that. Different instances of EDI would still mutate the same exception object.
There was a problem hiding this comment.
void Main()
{
Exception exception;
try
{
throw new Exception();
}
catch (Exception ex)
{
exception = ex;
}
var edi1 = ExceptionDispatchInfo.Capture(exception);
var edi2 = ExceptionDispatchInfo.Capture(exception);
try
{
edi1.Throw();
}
catch (Exception ex)
{
Console.WriteLine("***");
Console.WriteLine(ex.ToString());
}
try
{
edi1.Throw();
}
catch (Exception ex)
{
Console.WriteLine("***");
Console.WriteLine(ex.ToString());
}
try
{
edi2.Throw();
}
catch (Exception ex)
{
Console.WriteLine("***");
Console.WriteLine(ex.ToString());
}
try
{
edi2.Throw();
}
catch (Exception ex)
{
Console.WriteLine("***");
Console.WriteLine(ex.ToString());
}
}Doesn't seem to?
***
System.Exception: Exception of type 'System.Exception' was thrown.
at UserQuery.Main() in C:\Users\thund\AppData\Local\Temp\LINQPad5\_jffesacy\query_ibgjui.cs:line 36
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at UserQuery.Main() in C:\Users\thund\AppData\Local\Temp\LINQPad5\_jffesacy\query_ibgjui.cs:line 48
***
System.Exception: Exception of type 'System.Exception' was thrown.
at UserQuery.Main() in C:\Users\thund\AppData\Local\Temp\LINQPad5\_jffesacy\query_ibgjui.cs:line 36
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at UserQuery.Main() in C:\Users\thund\AppData\Local\Temp\LINQPad5\_jffesacy\query_ibgjui.cs:line 57
***
System.Exception: Exception of type 'System.Exception' was thrown.
at UserQuery.Main() in C:\Users\thund\AppData\Local\Temp\LINQPad5\_jffesacy\query_ibgjui.cs:line 36
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at UserQuery.Main() in C:\Users\thund\AppData\Local\Temp\LINQPad5\_jffesacy\query_ibgjui.cs:line 67
***
System.Exception: Exception of type 'System.Exception' was thrown.
at UserQuery.Main() in C:\Users\thund\AppData\Local\Temp\LINQPad5\_jffesacy\query_ibgjui.cs:line 36
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at UserQuery.Main() in C:\Users\thund\AppData\Local\Temp\LINQPad5\_jffesacy\query_ibgjui.cs:line 76
There was a problem hiding this comment.
{
Exception exception;
try
{
throw new Exception();
}
catch (Exception ex)
{
exception = ex;
}
var edi1 = ExceptionDispatchInfo.Capture(exception);
try
{
edi1.Throw();
}
catch (Exception ex)
{
Console.WriteLine("***");
Console.WriteLine(ex.ToString());
}
var edi2 = ExceptionDispatchInfo.Capture(exception);
try
{
edi2.Throw();
}
catch (Exception ex)
{
Console.WriteLine("***");
Console.WriteLine(ex.ToString());
}
}
Yes it does:
***
System.Exception: Exception of type 'System.Exception' was thrown.
at UserQuery.RunUserAuthoredQuery() in C:\Users\pakrymet\AppData\Local\Temp\LINQPad5\_clqswnhi\query_ztirrw.cs:line 39
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at UserQuery.RunUserAuthoredQuery() in C:\Users\pakrymet\AppData\Local\Temp\LINQPad5\_clqswnhi\query_ztirrw.cs:line 50
***
System.Exception: Exception of type 'System.Exception' was thrown.
at UserQuery.RunUserAuthoredQuery() in C:\Users\pakrymet\AppData\Local\Temp\LINQPad5\_clqswnhi\query_ztirrw.cs:line 39
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at UserQuery.RunUserAuthoredQuery() in C:\Users\pakrymet\AppData\Local\Temp\LINQPad5\_clqswnhi\query_ztirrw.cs:line 50
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at UserQuery.RunUserAuthoredQuery() in C:\Users\pakrymet\AppData\Local\Temp\LINQPad5\_clqswnhi\query_ztirrw.cs:line 61
There was a problem hiding this comment.
So Throw isn't threadsafe as it uses the same exception object
Exception exception;
Exception exception1 = null;
Exception exception2 = null;
try
{
throw new Exception();
}
catch (Exception ex)
{
exception = ex;
}
var edi1 = ExceptionDispatchInfo.Capture(exception);
try
{
edi1.Throw();
}
catch (Exception ex)
{
Console.WriteLine("***");
exception1 = ex;
Console.WriteLine(ex.ToString());
}
var edi2 = ExceptionDispatchInfo.Capture(exception);
try
{
edi2.Throw();
}
catch (Exception ex)
{
Console.WriteLine("***");
exception2 = ex;
Console.WriteLine(ex.ToString());
}
Console.WriteLine("***");
Console.WriteLine(ReferenceEquals(exception, exception1));
Console.WriteLine(ReferenceEquals(exception, exception2));***
System.Exception: Exception of type 'System.Exception' was thrown.
at UserQuery.Main() in C:\Users\thund\AppData\Local\Temp\LINQPad5\_jffesacy\query_tugyst.cs:line 38
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at UserQuery.Main() in C:\Users\thund\AppData\Local\Temp\LINQPad5\_jffesacy\query_tugyst.cs:line 49
***
System.Exception: Exception of type 'System.Exception' was thrown.
at UserQuery.Main() in C:\Users\thund\AppData\Local\Temp\LINQPad5\_jffesacy\query_tugyst.cs:line 38
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at UserQuery.Main() in C:\Users\thund\AppData\Local\Temp\LINQPad5\_jffesacy\query_tugyst.cs:line 49
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at UserQuery.Main() in C:\Users\thund\AppData\Local\Temp\LINQPad5\_jffesacy\query_tugyst.cs:line 61
***
True
True
So it looks like its equivalent to throw ex; but it keeps the StackTrace at capture and appends from there; whereas throw ex would completely overwrite.
|
@ahsonkhan ping |
…ple times (dotnet/corefx#31375) Commit migrated from dotnet/corefx@24c2780
Fixes #31388
Because of incorrect usage of ExceptionDispatchInfo exception stack trace is expanded every time it's rethrown https://github.com/dotnet/corefx/issues/31371