Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2c368e8

Browse files
committed
Add exception diagnostics to WinHttp
To help diagnose pernicious exceptions, this debug-build-only change augments exceptions created using CreateExceptionUsingError to include the calling thread's stack trace, so that when these exceptions are then moved to other threads and propagated there, we can better correlate where they came from.
1 parent a1ceee7 commit 2c368e8

File tree

8 files changed

+43
-35
lines changed

8 files changed

+43
-35
lines changed

src/Common/src/System/Diagnostics/ExceptionExtensions.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Common/src/System/Net/Http/WinHttpException.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
65
using System.ComponentModel;
6+
using System.Runtime.ExceptionServices;
77
using System.Runtime.InteropServices;
88

99
namespace System.Net.Http
@@ -47,7 +47,9 @@ public static WinHttpException CreateExceptionUsingLastError()
4747

4848
public static WinHttpException CreateExceptionUsingError(int error)
4949
{
50-
return new WinHttpException(error, GetErrorMessage(error));
50+
var e = new WinHttpException(error, GetErrorMessage(error));
51+
ExceptionStackTrace.AddCurrentStack(e);
52+
return e;
5153
}
5254

5355
public static string GetErrorMessage(int error)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics;
6+
using System.Reflection;
7+
8+
namespace System.Runtime.ExceptionServices
9+
{
10+
internal static class ExceptionStackTrace
11+
{
12+
/// <summary>In debug builds, appends the current stack trace to the exception.</summary>
13+
[Conditional("DEBUG")]
14+
public static void AddCurrentStack(Exception exception)
15+
{
16+
Debug.Assert(exception != null, "Expected non-null Exception");
17+
18+
const string ExceptionRemoteStackTraceStringName = "_remoteStackTraceString";
19+
FieldInfo fi = typeof(Exception).GetField(ExceptionRemoteStackTraceStringName, BindingFlags.NonPublic | BindingFlags.Instance);
20+
21+
if (fi != null)
22+
{
23+
string text =
24+
(string)fi.GetValue(exception) +
25+
Environment.StackTrace + Environment.NewLine +
26+
"--- End of stack trace from AddCurrentStack ---" + Environment.NewLine;
27+
fi.SetValue(exception, text);
28+
}
29+
}
30+
}
31+
}

src/System.Net.Http.WinHttpHandler/src/System.Net.Http.WinHttpHandler.msbuild

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
<CompileItem Include="$(CommonPath)\Interop\Windows\winhttp\Interop.winhttp.cs" />
1616
<CompileItem Include="$(CommonPath)\System\CharArrayHelpers.cs" />
1717
<CompileItem Include="$(CommonPath)\System\StringExtensions.cs" />
18-
<CompileItem Include="$(CommonPath)\System\Diagnostics\ExceptionExtensions.cs" />
1918
<CompileItem Include="$(CommonPath)\System\Net\HttpKnownHeaderNames.cs" />
2019
<CompileItem Include="$(CommonPath)\System\Net\HttpKnownHeaderNames.TryGetHeaderName.cs" />
2120
<CompileItem Include="$(CommonPath)\System\Net\UriScheme.cs" />
2221
<CompileItem Include="$(CommonPath)\System\Net\SecurityProtocol.cs" />
2322
<CompileItem Include="$(CommonPath)\System\Net\Http\HttpHandlerDefaults.cs" />
2423
<CompileItem Include="$(CommonPath)\System\Net\Http\NoWriteNoSeekStreamContent.cs" />
2524
<CompileItem Include="$(CommonPath)\System\Net\Http\WinHttpException.cs" />
25+
<CompileItem Include="$(CommonPath)\System\Runtime\ExceptionServices\ExceptionStackTrace.cs" />
2626
<CompileItem Include="$(CommonPath)\System\Threading\Tasks\RendezvousAwaitable.cs" />
2727
<CompileItem Include="$(CommonPath)\System\Threading\Tasks\TaskToApm.cs" />
2828
<CompileItem Include="$(MSBuildThisFileDirectory)\System\Net\Http\WinHttpAuthHelper.cs" />

src/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpRequestStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private Task<bool> InternalWriteDataAsync(byte[] buffer, int offset, int count,
249249
IntPtr.Zero))
250250
{
251251
_state.TcsInternalWriteDataToRequestStream.TrySetException(
252-
new IOException(SR.net_http_io_write, WinHttpException.CreateExceptionUsingLastError().InitializeStackTrace()));
252+
new IOException(SR.net_http_io_write, WinHttpException.CreateExceptionUsingLastError()));
253253
}
254254
}
255255

src/System.Net.Http.WinHttpHandler/tests/UnitTests/System.Net.Http.WinHttpHandler.Unit.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
<Compile Include="$(CommonPath)\System\StringExtensions.cs">
3838
<Link>Common\System\StringExtensions.cs</Link>
3939
</Compile>
40-
<Compile Include="$(CommonPath)\System\Diagnostics\ExceptionExtensions.cs">
41-
<Link>Common\System\Diagnostics\ExceptionExtensions.cs</Link>
42-
</Compile>
4340
<Compile Include="$(CommonPath)\System\IO\StreamHelpers.CopyValidation.cs">
4441
<Link>Common\System\IO\StreamHelpers.CopyValidation.cs</Link>
4542
</Compile>
@@ -70,6 +67,9 @@
7067
<Compile Include="$(CommonPath)\System\Net\Http\WinHttpException.cs">
7168
<Link>Common\System\Net\Http\WinHttpException.cs</Link>
7269
</Compile>
70+
<Compile Include="$(CommonPath)\System\Runtime\ExceptionServices\ExceptionStackTrace.cs">
71+
<Link>Common\System\Runtime\ExceptionServices\ExceptionStackTrace.cs</Link>
72+
</Compile>
7373
<Compile Include="$(CommonPath)\System\Threading\Tasks\RendezvousAwaitable.cs">
7474
<Link>Common\System\Threading\Tasks\RendezvousAwaitable.cs</Link>
7575
</Compile>

src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ServerCertificates.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ public async Task NoCallback_RevokedCertificate_RevocationChecking_Fails()
212212
};
213213

214214
[OuterLoop] // TODO: Issue #11345
215-
[ActiveIssue(7812, TestPlatforms.Windows)]
216215
[ConditionalTheory(nameof(BackendSupportsCustomCertificateHandling))]
217216
[MemberData(nameof(CertificateValidationServersAndExpectedPolicies))]
218217
public async Task UseCallback_BadCertificate_ExpectedPolicyErrors(string url, SslPolicyErrors expectedErrors)

src/System.Net.WebSockets.Client/src/System.Net.WebSockets.Client.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<Compile Include="$(CommonPath)\System\Net\Http\WinHttpException.cs">
5252
<Link>Common\System\Net\Http\WinHttpException.cs</Link>
5353
</Compile>
54+
<Compile Include="$(CommonPath)\System\Runtime\ExceptionServices\ExceptionStackTrace.cs">
55+
<Link>Common\System\Runtime\ExceptionServices\ExceptionStackTrace.cs</Link>
56+
</Compile>
5457
<!-- Interop -->
5558
<Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs">
5659
<Link>Interop\Windows\Interop.Libraries.cs</Link>

0 commit comments

Comments
 (0)