Skip to content

Comments

Fix debug assert in Uri.CreateThisFromUri when combining UNC base with "file:" relative URI#124660

Merged
MihaZupan merged 2 commits intomainfrom
copilot/fix-debug-assert-in-uri
Feb 24, 2026
Merged

Fix debug assert in Uri.CreateThisFromUri when combining UNC base with "file:" relative URI#124660
MihaZupan merged 2 commits intomainfrom
copilot/fix-debug-assert-in-uri

Conversation

Copy link
Contributor

Copilot AI commented Feb 20, 2026

Uri(Uri, Uri) triggers a Debug.Assert failure in CreateThisFromUri when combining a UNC file URI base with a "file:" relative URI:

var baseUri = new Uri(@"\\aa", UriKind.Absolute); // file://aa/
var relUri  = new Uri("file:", UriKind.Relative);
_ = new Uri(baseUri, relUri);                     // Debug.Assert failure

Description

Root cause: CreateThisFromUri copies all flags from otherUri verbatim, including Debug_LeftConstructor. In Uri(Uri, Uri), CreateThisFromUri is called twice — first to initialize from relUri, and again after ResolveHelper returns a resolved URI. On the second call, this._flags already carries Debug_LeftConstructor (copied from the first call), so DebugAssertInCtor() fires.

Fix: Strip Debug_LeftConstructor from the copied flags in CreateThisFromUri:

// Before
_flags = otherUri._flags;

// After
_flags = otherUri._flags & ~Flags.Debug_LeftConstructor;

Debug_LeftConstructor is per-instance state — it must only be set via DebugSetLeftCtor() when the current constructor finishes, never inherited from another URI.

A regression test covering the UNC + "file:" relative combination is added to UncTest.cs.

Original prompt

This section details on the original issue you should resolve

<issue_title>Harmless debug assert hit in Uri's DebugAssertInCtor</issue_title>
<issue_description>```c#
// Uri.CreateThisFromUri fires Debug.Assert: (_flags & Flags.Debug_LeftConstructor) == 0
// when combining a UNC file URI base with a "file:" relative URI.
//
// Source: System.Uri.DebugAssertInCtor() at Uri.cs:205
// System.Uri.CreateThisFromUri() at UriExt.cs:891

var baseUri = new Uri(@"\aa", UriKind.Absolute); // parses as file://aa/
var relUri = new Uri("file:", UriKind.Relative);
_ = new Uri(baseUri, relUri); // Debug.Assert failure


The assert violation is in CreateThisFromUri — it expects the constructor flag Debug_LeftConstructor to not be set, but the re-entrant URI construction from a UNC base + file: relative violates this invariant.

This is from the extra `DebugAssertInCtor` I added to `DebugSetLeftCtor` in dotnet/runtime#123932. We should either remove the assert or avoid copying the Debug_LeftConstructor flag in CreateThisFromUri.</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

@dotnet-policy-service
Copy link
Contributor

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

…nstructor flag

Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix debug assert in Uri's constructor handling Fix debug assert in Uri.CreateThisFromUri when combining UNC base with "file:" relative URI Feb 20, 2026
Copilot AI requested a review from MihaZupan February 20, 2026 17:28
@MihaZupan MihaZupan marked this pull request as ready for review February 21, 2026 16:41
Copilot AI review requested due to automatic review settings February 21, 2026 16:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a debug assertion failure in Uri.CreateThisFromUri when combining a UNC file URI base with a "file:" relative URI. The root cause was that CreateThisFromUri was copying all flags verbatim from the source URI, including the Debug_LeftConstructor flag, which is per-instance state that should only be set when the current constructor finishes.

Changes:

  • Strip Debug_LeftConstructor flag when copying flags in CreateThisFromUri to prevent re-entrant constructor calls from triggering false assertions
  • Add regression test covering the UNC + "file:" relative URI combination scenario

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Private.Uri/src/System/UriExt.cs Strip Debug_LeftConstructor flag when copying flags from source URI in CreateThisFromUri method
src/libraries/System.Private.Uri/tests/FunctionalTests/UncTest.cs Add regression test verifying that combining UNC base URI with "file:" relative URI does not trigger debug assertion

@MihaZupan MihaZupan requested a review from a team February 23, 2026 09:39
@MihaZupan MihaZupan enabled auto-merge (squash) February 24, 2026 13:47
@MihaZupan MihaZupan merged commit 81c46c6 into main Feb 24, 2026
93 of 96 checks passed
@MihaZupan MihaZupan deleted the copilot/fix-debug-assert-in-uri branch February 24, 2026 14:15
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.

Harmless debug assert hit in Uri's DebugAssertInCtor

3 participants