Skip to content

Use a cryptographically random trace ID generator after SnapStart restore - #2632

Draft
normj wants to merge 1 commit into
masterfrom
normj/snapstart-traceid-generator
Draft

normj wants to merge 1 commit into
masterfrom
normj/snapstart-traceid-generator

Conversation

@normj

@normj normj commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Problem

Activity generates trace IDs from an in-memory, per-thread pseudo-random generator (the internal Xoshiro256** RandomNumberGenerator in System.Diagnostics). A SnapStart snapshot captures that state, so every execution environment restored from the same snapshot continues the same sequence and produces the same trace IDs. OpenTelemetry .NET takes its IDs from Activity, so instrumented functions merge unrelated requests into one trace.

A repro on the dotnet10 managed runtime that created Activity objects during init got 3 distinct trace IDs across 50 concurrent first invokes. A control from RandomNumberGenerator.Fill in the same invokes was 50 of 50 unique.

Change

After /runtime/restore/next returns, and before the function's after restore hooks run, RuntimeSupport sets Activity.TraceIdGenerator to a generator that fills the trace ID from System.Security.Cryptography.RandomNumberGenerator. Lambda reseeds the kernel random source after restore, so each environment gets unique IDs.

  • The generator is only set when Activity.TraceIdGenerator is null, so a generator the function set during init (for example AWSXRayIdGenerator.ReplaceTraceId()) is kept.
  • An after restore hook can still replace it, since it runs afterward.
  • Only SnapStart restores are affected.

Not covered

Activity has no hook for span IDs. ActivitySpanId.CreateRandom() always uses the internal generator, so span IDs can still repeat across restored environments on threads that created an Activity before the snapshot. Fixing that needs a change in dotnet/runtime.

Testing

  • Unit tests cover the generator being set before after restore hooks run, a generator set during init being kept, and generated IDs being unique. Each test fails with its part of the fix reverted.

  • Live SnapStart test on the dotnet10 managed runtime (us-west-2, 512 MB), using an executable-mode function so the referenced RuntimeSupport drives the restore. The same harness was built once against master and once against this branch. Init creates Activity objects on the main thread and 8 thread-pool threads, then each invoke starts a root Activity. Two snapshots per build, 50 concurrent invokes each:

    Build Snapshot Distinct trace IDs Distinct span IDs OS-random control
    master 1 40 / 50 40 / 50 50 / 50
    master 2 35 / 50 35 / 50 50 / 50
    this PR 1 50 / 50 40 / 50 50 / 50
    this PR 2 50 / 50 14 / 50 50 / 50

    Every invoke of this PR's build reported Activity.TraceIdGenerator as SnapStartTraceIdGenerator.GenerateTraceId. Span IDs still repeat, as described above.

…tore

Activity generates trace IDs from an in-memory per-thread PRNG whose state is captured in the SnapStart snapshot, so restored execution environments repeat trace IDs. After restore, and before the function's after restore hooks run, set Activity.TraceIdGenerator to a generator backed by RandomNumberGenerator.Fill. A generator the function set during init is left in place.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant