Skip to content

Add potential fix for duck typing derived types#8410

Merged
andrewlock merged 2 commits intomasterfrom
andrew/investigate-uri
Apr 2, 2026
Merged

Add potential fix for duck typing derived types#8410
andrewlock merged 2 commits intomasterfrom
andrew/investigate-uri

Conversation

@andrewlock
Copy link
Copy Markdown
Member

Summary of changes

Potentially fixes a FieldAccessException in some cases

Reason for change

We have seen stack traces like the following in production:

Error : Error creating or populating span.
System.FieldAccessException
   at REDACTED
   at Datadog.Trace.ClrProfiler.ScopeFactory.CreateInactiveOutboundHttpSpan(Tracer tracer, String httpMethod, Uri requestUri, IntegrationId integrationId, HttpTags& tags, TraceId traceId, UInt64 spanId, Nullable`1 startTime, Boolean addToTraceContext)

After a bunch of 🤖 noodling and testing (and based on @zacharycmontoya idea), proved that we can repro this stack (sort of, assuming inlining) if we derive from System.Uri. The error happens because we apply IgnoresAccessChecksToAttribute to the target type (i.e. the derived type) during ducktyping, but it needs to be applied to the targetField.DeclaringType.

Implementation details

Updated the UseDirectAccessTo calls to point to the type that "owns" the field, instead of the current instance.

Test coverage

Added a test confirming the error, and then showing it passed

Other details

https://app.datadoghq.com/cases/APMLP-35

Copy link
Copy Markdown
Contributor

@zacharycmontoya zacharycmontoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for looking into this!

@dd-trace-dotnet-ci-bot
Copy link
Copy Markdown

dd-trace-dotnet-ci-bot bot commented Apr 2, 2026

Execution-Time Benchmarks Report ⏱️

Execution-time results for samples comparing This PR (8410) and master.

✅ No regressions detected - check the details below

Full Metrics Comparison

FakeDbCommand

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration75.50 ± (75.51 - 75.94) ms75.34 ± (75.33 - 75.66) ms-0.2%
.NET Framework 4.8 - Bailout
duration80.73 ± (80.66 - 81.00) ms79.64 ± (79.53 - 79.90) ms-1.3%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1105.34 ± (1105.15 - 1110.36) ms1106.19 ± (1107.37 - 1114.97) ms+0.1%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms23.30 ± (23.24 - 23.36) ms23.14 ± (23.08 - 23.20) ms-0.7%
process.time_to_main_ms88.25 ± (88.05 - 88.45) ms88.22 ± (88.03 - 88.41) ms-0.0%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.90 ± (10.90 - 10.90) MB10.92 ± (10.92 - 10.93) MB+0.2%✅⬆️
runtime.dotnet.threads.count12 ± (12 - 12)12 ± (12 - 12)+0.0%
.NET Core 3.1 - Bailout
process.internal_duration_ms23.28 ± (23.22 - 23.34) ms23.05 ± (22.99 - 23.12) ms-1.0%
process.time_to_main_ms90.35 ± (90.13 - 90.57) ms89.81 ± (89.58 - 90.03) ms-0.6%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.95 ± (10.94 - 10.95) MB10.96 ± (10.96 - 10.97) MB+0.2%✅⬆️
runtime.dotnet.threads.count13 ± (13 - 13)13 ± (13 - 13)+0.0%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms235.61 ± (234.53 - 236.68) ms235.68 ± (234.26 - 237.09) ms+0.0%✅⬆️
process.time_to_main_ms542.03 ± (540.78 - 543.29) ms540.38 ± (539.08 - 541.67) ms-0.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed47.78 ± (47.75 - 47.81) MB47.77 ± (47.73 - 47.81) MB-0.0%
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+0.5%✅⬆️
.NET 6 - Baseline
process.internal_duration_ms21.73 ± (21.69 - 21.77) ms21.82 ± (21.77 - 21.86) ms+0.4%✅⬆️
process.time_to_main_ms76.49 ± (76.31 - 76.68) ms76.60 ± (76.43 - 76.78) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.63 ± (10.62 - 10.63) MB10.64 ± (10.64 - 10.64) MB+0.1%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 6 - Bailout
process.internal_duration_ms21.67 ± (21.63 - 21.72) ms21.67 ± (21.62 - 21.73) ms-0.0%
process.time_to_main_ms77.20 ± (77.00 - 77.40) ms77.40 ± (77.20 - 77.60) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.72 ± (10.72 - 10.73) MB10.75 ± (10.75 - 10.76) MB+0.3%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms385.24 ± (383.18 - 387.30) ms383.00 ± (380.95 - 385.05) ms-0.6%
process.time_to_main_ms539.85 ± (538.77 - 540.94) ms539.54 ± (538.37 - 540.72) ms-0.1%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed49.94 ± (49.91 - 49.97) MB49.94 ± (49.90 - 49.97) MB-0.0%
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+0.1%✅⬆️
.NET 8 - Baseline
process.internal_duration_ms20.07 ± (20.02 - 20.12) ms20.05 ± (20.01 - 20.10) ms-0.1%
process.time_to_main_ms75.91 ± (75.72 - 76.09) ms76.21 ± (76.04 - 76.39) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.66 ± (7.66 - 7.67) MB7.67 ± (7.67 - 7.68) MB+0.1%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 8 - Bailout
process.internal_duration_ms20.07 ± (20.03 - 20.12) ms19.91 ± (19.85 - 19.96) ms-0.8%
process.time_to_main_ms77.21 ± (77.02 - 77.40) ms76.76 ± (76.57 - 76.96) ms-0.6%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.75 ± (7.74 - 7.76) MB7.74 ± (7.73 - 7.75) MB-0.2%
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms304.86 ± (302.23 - 307.48) ms305.11 ± (303.06 - 307.17) ms+0.1%✅⬆️
process.time_to_main_ms500.47 ± (499.59 - 501.35) ms502.72 ± (501.86 - 503.58) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed37.01 ± (36.98 - 37.04) MB37.12 ± (37.09 - 37.15) MB+0.3%✅⬆️
runtime.dotnet.threads.count27 ± (27 - 27)27 ± (27 - 27)-0.2%

HttpMessageHandler

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration213.30 ± (213.08 - 214.02) ms217.68 ± (217.18 - 218.27) ms+2.1%✅⬆️
.NET Framework 4.8 - Bailout
duration217.64 ± (217.17 - 218.12) ms223.28 ± (222.74 - 223.77) ms+2.6%✅⬆️
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1236.98 ± (1236.70 - 1242.87) ms1239.92 ± (1239.45 - 1245.99) ms+0.2%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms208.74 ± (208.22 - 209.27) ms209.37 ± (208.83 - 209.90) ms+0.3%✅⬆️
process.time_to_main_ms90.99 ± (90.72 - 91.25) ms91.19 ± (90.86 - 91.52) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed15.88 ± (15.86 - 15.90) MB15.90 ± (15.88 - 15.93) MB+0.1%✅⬆️
runtime.dotnet.threads.count20 ± (20 - 20)20 ± (20 - 20)-0.3%
.NET Core 3.1 - Bailout
process.internal_duration_ms209.33 ± (208.86 - 209.81) ms208.94 ± (208.40 - 209.47) ms-0.2%
process.time_to_main_ms92.84 ± (92.55 - 93.13) ms92.91 ± (92.68 - 93.13) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed15.94 ± (15.92 - 15.97) MB16.01 ± (15.99 - 16.04) MB+0.4%✅⬆️
runtime.dotnet.threads.count21 ± (21 - 21)21 ± (21 - 21)-0.4%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms420.85 ± (419.30 - 422.40) ms422.96 ± (421.35 - 424.57) ms+0.5%✅⬆️
process.time_to_main_ms554.77 ± (553.23 - 556.31) ms552.00 ± (550.67 - 553.33) ms-0.5%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed58.73 ± (58.68 - 58.77) MB58.65 ± (58.61 - 58.70) MB-0.1%
runtime.dotnet.threads.count30 ± (30 - 30)30 ± (30 - 30)-0.1%
.NET 6 - Baseline
process.internal_duration_ms215.09 ± (214.52 - 215.66) ms216.51 ± (215.91 - 217.10) ms+0.7%✅⬆️
process.time_to_main_ms79.84 ± (79.57 - 80.10) ms80.76 ± (80.49 - 81.03) ms+1.2%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.19 ± (16.16 - 16.21) MB16.18 ± (16.16 - 16.20) MB-0.0%
runtime.dotnet.threads.count20 ± (19 - 20)20 ± (20 - 20)+0.4%✅⬆️
.NET 6 - Bailout
process.internal_duration_ms213.84 ± (213.36 - 214.31) ms216.23 ± (215.62 - 216.84) ms+1.1%✅⬆️
process.time_to_main_ms80.73 ± (80.46 - 80.99) ms81.39 ± (81.18 - 81.59) ms+0.8%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.23 ± (16.20 - 16.25) MB16.20 ± (16.18 - 16.22) MB-0.2%
runtime.dotnet.threads.count21 ± (20 - 21)21 ± (20 - 21)+0.2%✅⬆️
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms603.46 ± (600.81 - 606.11) ms600.50 ± (598.18 - 602.83) ms-0.5%
process.time_to_main_ms558.99 ± (557.73 - 560.25) ms560.49 ± (559.23 - 561.76) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed61.28 ± (61.20 - 61.37) MB61.35 ± (61.25 - 61.45) MB+0.1%✅⬆️
runtime.dotnet.threads.count31 ± (31 - 31)31 ± (31 - 31)-0.5%
.NET 8 - Baseline
process.internal_duration_ms212.96 ± (212.48 - 213.44) ms212.05 ± (211.59 - 212.51) ms-0.4%
process.time_to_main_ms79.10 ± (78.82 - 79.38) ms78.66 ± (78.43 - 78.89) ms-0.6%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.56 ± (11.54 - 11.58) MB11.59 ± (11.56 - 11.61) MB+0.2%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 19)-0.2%
.NET 8 - Bailout
process.internal_duration_ms212.73 ± (212.27 - 213.19) ms212.66 ± (212.10 - 213.22) ms-0.0%
process.time_to_main_ms80.37 ± (80.15 - 80.58) ms80.32 ± (80.12 - 80.51) ms-0.1%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.65 ± (11.62 - 11.67) MB11.60 ± (11.58 - 11.62) MB-0.4%
runtime.dotnet.threads.count20 ± (20 - 20)20 ± (20 - 20)-0.2%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms555.81 ± (547.99 - 563.64) ms563.36 ± (555.46 - 571.26) ms+1.4%✅⬆️
process.time_to_main_ms514.44 ± (513.37 - 515.50) ms515.21 ± (514.14 - 516.28) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed51.00 ± (50.91 - 51.10) MB51.19 ± (51.09 - 51.28) MB+0.4%✅⬆️
runtime.dotnet.threads.count30 ± (30 - 30)30 ± (30 - 30)+0.3%✅⬆️
Comparison explanation

Execution-time benchmarks measure the whole time it takes to execute a program, and are intended to measure the one-off costs. Cases where the execution time results for the PR are worse than latest master results are highlighted in **red**. The following thresholds were used for comparing the execution times:

  • Welch test with statistical test for significance of 5%
  • Only results indicating a difference greater than 5% and 5 ms are considered.

Note that these results are based on a single point-in-time result for each branch. For full results, see the dashboard.

Graphs show the p99 interval based on the mean and StdDev of the test run, as well as the mean value of the run (shown as a diamond below the graph).

Duration charts
FakeDbCommand (.NET Framework 4.8)
gantt
    title Execution time (ms) FakeDbCommand (.NET Framework 4.8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8410) - mean (75ms)  : 73, 78
    master - mean (76ms)  : 72, 79

    section Bailout
    This PR (8410) - mean (80ms)  : 78, 82
    master - mean (81ms)  : 79, 83

    section CallTarget+Inlining+NGEN
    This PR (8410) - mean (1,111ms)  : 1055, 1167
    master - mean (1,108ms)  : 1070, 1145

Loading
FakeDbCommand (.NET Core 3.1)
gantt
    title Execution time (ms) FakeDbCommand (.NET Core 3.1)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8410) - mean (119ms)  : 115, 122
    master - mean (119ms)  : 116, 122

    section Bailout
    This PR (8410) - mean (120ms)  : 118, 123
    master - mean (121ms)  : 118, 124

    section CallTarget+Inlining+NGEN
    This PR (8410) - mean (814ms)  : 791, 836
    master - mean (816ms)  : 795, 837

Loading
FakeDbCommand (.NET 6)
gantt
    title Execution time (ms) FakeDbCommand (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8410) - mean (105ms)  : 102, 109
    master - mean (105ms)  : 101, 109

    section Bailout
    This PR (8410) - mean (106ms)  : 103, 109
    master - mean (106ms)  : 102, 109

    section CallTarget+Inlining+NGEN
    This PR (8410) - mean (949ms)  : 917, 981
    master - mean (955ms)  : 924, 985

Loading
FakeDbCommand (.NET 8)
gantt
    title Execution time (ms) FakeDbCommand (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8410) - mean (105ms)  : 101, 108
    master - mean (104ms)  : 101, 107

    section Bailout
    This PR (8410) - mean (105ms)  : 102, 107
    master - mean (105ms)  : 103, 108

    section CallTarget+Inlining+NGEN
    This PR (8410) - mean (840ms)  : 801, 878
    master - mean (836ms)  : 794, 878

Loading
HttpMessageHandler (.NET Framework 4.8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET Framework 4.8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8410) - mean (218ms)  : 210, 225
    master - mean (214ms)  : 209, 219

    section Bailout
    This PR (8410) - mean (223ms)  : 218, 228
    master - mean (218ms)  : 213, 222

    section CallTarget+Inlining+NGEN
    This PR (8410) - mean (1,243ms)  : 1194, 1291
    master - mean (1,240ms)  : 1196, 1284

Loading
HttpMessageHandler (.NET Core 3.1)
gantt
    title Execution time (ms) HttpMessageHandler (.NET Core 3.1)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8410) - mean (310ms)  : 302, 318
    master - mean (309ms)  : 302, 316

    section Bailout
    This PR (8410) - mean (312ms)  : 304, 319
    master - mean (311ms)  : 304, 319

    section CallTarget+Inlining+NGEN
    This PR (8410) - mean (1,018ms)  : 983, 1053
    master - mean (1,016ms)  : 990, 1043

Loading
HttpMessageHandler (.NET 6)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8410) - mean (306ms)  : 298, 315
    master - mean (304ms)  : 297, 311

    section Bailout
    This PR (8410) - mean (306ms)  : 300, 313
    master - mean (304ms)  : 297, 311

    section CallTarget+Inlining+NGEN
    This PR (8410) - mean (1,193ms)  : 1155, 1231
    master - mean (1,196ms)  : 1154, 1237

Loading
HttpMessageHandler (.NET 8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8410) - mean (301ms)  : 294, 308
    master - mean (303ms)  : 295, 310

    section Bailout
    This PR (8410) - mean (304ms)  : 298, 310
    master - mean (303ms)  : 299, 308

    section CallTarget+Inlining+NGEN
    This PR (8410) - mean (1,113ms)  : 988, 1237
    master - mean (1,111ms)  : 982, 1240

Loading

Copy link
Copy Markdown
Collaborator

@bouwkast bouwkast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@andrewlock andrewlock enabled auto-merge (squash) April 2, 2026 17:33
@andrewlock andrewlock merged commit 5686c1c into master Apr 2, 2026
138 of 139 checks passed
@andrewlock andrewlock deleted the andrew/investigate-uri branch April 2, 2026 19:12
@github-actions github-actions bot added this to the vNext-v3 milestone Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants