dotnet / dotnet/runtime

[browser][CoreCLR] GC.GetGCMemoryInfo(GCKind.Background) asserts when background GC is unavailable

Open
#131,766 2 comments 0 reactions 1 assignee Claimed by @radekdoulik View on GitHub
arch-wasm area-GC-coreclr os-browser
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Description

CoreCLR browser WebAssembly is built without `BACKGROUND_GC`, but `GC.GetGCMemoryInfo(GCKind.Background)` remains a valid managed API call.

In Checked builds, preprocessing removes the branch handling `gc_kind_background`, so the call falls through to the `gc_kind_any` path and asserts. In Release builds, the same path can incorrectly return information for the most recent foreground collection.

### Reproduction Steps

Build a Checked CoreCLR browser-WASM runtime and run the existing test:

```text
src/tests/GC/API/GC/GetGCMemoryInfo.csproj
```

The minimal managed operation that triggers the failure is:

```csharp
GCMemoryInfo info = GC.GetGCMemoryInfo(GCKind.Background);
Console.WriteLine(info.Index);
```

This was reproduced on current `main` at `3241014d5a2d09d044d112af0b5bed2d2ad94e96`.

### Expected behavior

When background GC is unavailable and no background collection can have occurred, the API should return an empty collection-specific record, including `Index == 0`.

### Actual behavior

Checked builds fail with:

```text
ASSERT FAILED
Expression: (gc_kind)kind == gc_kind_any
Location: src/coreclr/gc/interface.cpp
Function: GetMemoryInfo
```

Release builds can return information for a foreground collection instead of an empty background-GC record.

### Regression?

No known regression. This appears to be a latent issue exposed by running the existing process-isolated test on CoreCLR browser-WASM.

### Known Workarounds

Avoid requesting `GCKind.Background` on runtimes without background GC. The affected browser-WASM test can be marked with an ActiveIssue until the runtime behavior is corrected.

### Configuration

- CoreCLR browser-WASM
- Checked and Release configurations
- `BACKGROUND_GC` not defined
- Reproduced with the Node-hosted runtime test infrastructure

### Other information

The implementation should preserve the documented behavior for a requested GC kind that has never occurred: return an empty record rather than information for a different collection kind.

> [!NOTE]
> This issue was drafted with GitHub Copilot assistance.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.