Skip to content

[Interpreter] A branch to itself gets no safepoint, so a thread in while (true) { } blocks GC suspension #135465

Open

Description

@pcshrosbree

With the CoreCLR interpreter (DOTNET_InterpMode=1), an empty infinite loop branches to itself without a safepoint, so a thread spinning in it cannot be suspended and the next GC hangs the process. I plan to open a PR that adds the missing safepoint, fixes a related thread-abort attribution problem (described below), and adds tests.

Repro:

using System;
using System.Threading;

static class Program
{
    static volatile bool s_started;

    static void Main()
    {
        var spinner = new Thread(() => { s_started = true; while (true) { } }) { IsBackground = true };
        spinner.Start();
        while (!s_started) Thread.Sleep(1);
        Console.WriteLine("collecting");
        GC.Collect();
        Console.WriteLine("collected");
    }
}
Checked, at 6ff62b1 JIT DOTNET_InterpMode=1
linux-x64, osx-arm64, windows-x64 collecting, collected collecting, then hangs

Cause

while (true) { } compiles to L: br.s L. InterpCompiler::EmitBranch emits INTOP_SAFEPOINT only when ilOffset < 0, and its callers pass the offset relative to the start of the branch instruction (2 + disp for br.s, 5 + offset for br). So a branch to itself has offset 0 and gets no safepoint. DOTNET_InterpDump shows the loop as a br that targets itself with no safepoint before it. Cancelling such a loop with ControlledExecution never completes either.

Fix

In a local build, changing the condition to ilOffset <= 0 fixes the repro on linux-x64. With that change, a try region whose body is the loop can begin with the safepoint in front of the branch. That exposes a thread-abort attribution problem: INTOP_SAFEPOINT records the start of the instruction, but HandleThreadAbort dispatches the abort as if from a call. So an abort raised at that safepoint is attributed to the instruction before the region. The PR will fix both.

Note

This issue was drafted with AI assistance (Anthropic Claude). The runs it cites were done at commit 6ff62b1.

Activity

  1. added
    untriaged
    on Oct 9, 2026
  2. dotnet-policy-service commented on Oct 9, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
    See info in area-owners.md if you want to be subscribed.

  3. dotnet-policy-service commented on Oct 9, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @BrzVlad, @janvorli
    See info in area-owners.md if you want to be subscribed.

  4. removed
    untriaged
    on Oct 9, 2026
  5. self-assigned this
    on Oct 9, 2026
  6. added this to the 12.0.0 milestone on Oct 9, 2026
  7. pcshrosbree commented on Oct 9, 2026

    @pcshrosbree
    ContributorAuthor

    @BrzVlad I've opened #135467 for this. While fixing it I found that a backward leave (for example while (true) { try { ... } catch { ... } }) and a backward switch have the same gap; the PR covers all three, with a test, and is validated on linux-x64, osx-arm64 and windows-x64.

    Note

    This comment was prepared with AI assistance (Anthropic Claude).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions