using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
+using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
long fileLength = fs.Length;
if (fileLength > int.MaxValue)
{
- return Task.FromException<byte[]>(new IOException(SR.IO_FileTooLong2GB));
+ var e = new IOException(SR.IO_FileTooLong2GB);
+#if !MS_IO_REDIST
+ ExceptionDispatchInfo.SetCurrentStackTrace(e);
+#endif
+ return Task.FromException<byte[]>(e);
}
returningInternalTask = true;
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.Runtime.ExceptionServices;
using System.Threading;
namespace System.IO.Pipes
base.AsyncCallback(errorCode, numBytes);
}
- protected override void HandleError(int errorCode)
- {
- TrySetException(Win32Marshal.GetExceptionForWin32Error(errorCode));
- }
+ protected override void HandleError(int errorCode) =>
+ TrySetException(ExceptionDispatchInfo.SetCurrentStackTrace(Win32Marshal.GetExceptionForWin32Error(errorCode)));
- protected override void HandleUnexpectedCancellation() => TrySetException(Error.GetOperationAborted());
+ protected override void HandleUnexpectedCancellation() =>
+ TrySetException(ExceptionDispatchInfo.SetCurrentStackTrace(Error.GetOperationAborted()));
}
internal struct VoidResult { }
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.Runtime.ExceptionServices;
using System.Threading;
namespace System.IO.Pipes
base.AsyncCallback(errorCode, numBytes);
}
- protected override void HandleError(int errorCode)
- {
- TrySetException(_pipeStream.WinIOError(errorCode));
- }
+ protected override void HandleError(int errorCode) =>
+ TrySetException(ExceptionDispatchInfo.SetCurrentStackTrace(_pipeStream.WinIOError(errorCode)));
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
+using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
if (task.Result == null)
{
- sendState.TrySetException(new InvalidOperationException(SR.net_http_handler_noresponse));
+ sendState.TrySetException(ExceptionDispatchInfo.SetCurrentStackTrace(new InvalidOperationException(SR.net_http_handler_noresponse)));
return;
}
using System.Net.Security;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
+using System.Runtime.ExceptionServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
case SocketError.ConnectionAborted:
if (CancellationToken.IsCancellationRequested)
{
- Builder.SetException(CancellationHelper.CreateOperationCanceledException(null, CancellationToken));
+ Builder.SetException(ExceptionDispatchInfo.SetCurrentStackTrace(CancellationHelper.CreateOperationCanceledException(null, CancellationToken)));
break;
}
goto default;
default:
- Builder.SetException(new SocketException((int)SocketError));
+ Builder.SetException(ExceptionDispatchInfo.SetCurrentStackTrace(new SocketException((int)SocketError)));
break;
}
}
using System.Diagnostics;
using System.Collections.Generic;
using System.IO;
+using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
{
while (_waiters.TryDequeue(out Waiter waiter))
{
- waiter.TrySetException(CreateObjectDisposedException(forActiveWaiter: true));
+ waiter.TrySetException(ExceptionDispatchInfo.SetCurrentStackTrace(CreateObjectDisposedException(forActiveWaiter: true)));
}
}
}
using System.IO;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
+using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
if (http2Stream == null)
{
- return new ValueTask<int>(Task.FromException<int>(new ObjectDisposedException(nameof(Http2ReadStream))));
+ return new ValueTask<int>(Task.FromException<int>(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http2ReadStream)))));
}
if (cancellationToken.IsCancellationRequested)
// See the LICENSE file in the project root for more information.
using System.IO;
+using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
HttpConnection connection = _connection;
if (connection == null)
{
- return new ValueTask(Task.FromException(new IOException(SR.ObjectDisposed_StreamClosed)));
+ return new ValueTask(Task.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new IOException(SR.ObjectDisposed_StreamClosed))));
}
if (buffer.Length == 0)
// See the LICENSE file in the project root for more information.
using System.Runtime.CompilerServices;
+using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.Threading;
}
else
{
- state.SetResult(new SocketException((int)errorCode));
+ state.SetResult(ExceptionDispatchInfo.SetCurrentStackTrace(new SocketException((int)errorCode)));
}
}
finally
{
if (sEx.SocketErrorCode == SocketError.TimedOut)
{
- SetException(new WebException(SR.net_timeout, WebExceptionStatus.Timeout));
+ SetException(ExceptionDispatchInfo.SetCurrentStackTrace(new WebException(SR.net_timeout, WebExceptionStatus.Timeout)));
}
}
}
{
if (_queuedReadCount + count > MaxQueuedReadBytes)
{
- return new IOException(SR.Format(SR.net_auth_ignored_reauth, MaxQueuedReadBytes.ToString(NumberFormatInfo.CurrentInfo)));
+ return ExceptionDispatchInfo.SetCurrentStackTrace(
+ new IOException(SR.Format(SR.net_auth_ignored_reauth, MaxQueuedReadBytes.ToString(NumberFormatInfo.CurrentInfo))));
}
if (count != 0)
{
// Fail re-handshake.
ProtocolToken message = new ProtocolToken(null, status);
- StartSendAuthResetSignal(null, asyncRequest, ExceptionDispatchInfo.Capture(new AuthenticationException(SR.net_auth_SSPI, message.GetException())));
+ StartSendAuthResetSignal(null, asyncRequest, ExceptionDispatchInfo.Capture(
+ ExceptionDispatchInfo.SetCurrentStackTrace(new AuthenticationException(SR.net_auth_SSPI, message.GetException()))));
return;
}
// Re-handshake status is not supported.
ArrayPool<byte>.Shared.Return(rentedBuffer);
ProtocolToken message = new ProtocolToken(null, status);
- return new ValueTask(Task.FromException(new IOException(SR.net_io_encrypt, message.GetException())));
+ return new ValueTask(Task.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new IOException(SR.net_io_encrypt, message.GetException()))));
}
ValueTask t = writeAdapter.WriteAsync(outBuffer, 0, encryptedBytes);
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
+using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
/// <summary>Gets a SocketException or an IOException wrapping a SocketException for the specified error.</summary>
private static Exception GetException(SocketError error, bool wrapExceptionsInIOExceptions = false)
{
- Exception e = new SocketException((int)error);
+ Exception e = ExceptionDispatchInfo.SetCurrentStackTrace(new SocketException((int)error));
return wrapExceptionsInIOExceptions ?
new IOException(SR.Format(SR.net_io_readwritefailure, e.Message), e) :
e;
cancellationToken.ThrowIfCancellationRequested();
}
- throw CreateException(error);
+ throw CreateException(error, forAsyncThrow: false);
}
- private Exception CreateException(SocketError error)
+ private Exception CreateException(SocketError error, bool forAsyncThrow = true)
{
- var se = new SocketException((int)error);
+ Exception e = new SocketException((int)error);
+
+ if (forAsyncThrow)
+ {
+ e = ExceptionDispatchInfo.SetCurrentStackTrace(e);
+ }
+
return WrapExceptionsInIOExceptions ? (Exception)
- new IOException(SR.Format(SR.net_io_readfailure, se.Message), se) :
- se;
+ new IOException(SR.Format(SR.net_io_readfailure, e.Message), e) :
+ e;
}
}
}
internal ExceptionDispatchInfo() { }
public System.Exception SourceException { get { throw null; } }
public static System.Runtime.ExceptionServices.ExceptionDispatchInfo Capture(System.Exception source) { throw null; }
+ public static System.Exception SetCurrentStackTrace(System.Exception source) { throw null; }
[System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute]
public void Throw() { }
[System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute]
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.ComponentModel;
+using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using Xunit;
Assert.Equal(i, Regex.Matches(e.StackTrace, RethrowMessageSubstring).Count);
}
}
+
+ [Fact]
+ public static void SetCurrentStackTrace_Invalid_Throws()
+ {
+ Exception e;
+
+ // Null argument
+ e = null;
+ AssertExtensions.Throws<ArgumentNullException>("source", () => ExceptionDispatchInfo.SetCurrentStackTrace(e));
+
+ // Previously set current stack
+ e = new Exception();
+ ExceptionDispatchInfo.SetCurrentStackTrace(e);
+ Assert.Throws<InvalidOperationException>(() => ExceptionDispatchInfo.SetCurrentStackTrace(e));
+
+ // Previously thrown
+ e = new Exception();
+ try { throw e; } catch { }
+ Assert.Throws<InvalidOperationException>(() => ExceptionDispatchInfo.SetCurrentStackTrace(e));
+ }
+
+ [Fact]
+ public static void SetCurrentStackTrace_IncludedInExceptionStackTrace()
+ {
+ Exception e;
+
+ e = new Exception();
+ ABCDEFGHIJKLMNOPQRSTUVWXYZ(e);
+ Assert.Contains(nameof(ABCDEFGHIJKLMNOPQRSTUVWXYZ), e.StackTrace);
+
+ e = new Exception();
+ ABCDEFGHIJKLMNOPQRSTUVWXYZ(e);
+ try { throw e; } catch { }
+ Assert.Contains(nameof(ABCDEFGHIJKLMNOPQRSTUVWXYZ), e.StackTrace);
+ }
+
+ [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
+ private static void ABCDEFGHIJKLMNOPQRSTUVWXYZ(Exception e)
+ {
+ Assert.Same(e, ExceptionDispatchInfo.SetCurrentStackTrace(e));
+ }
}
}