Skip to content

SpaceBattle's EnableDeepProfiling switch silently produces no trace — env vars are set after the engine has already snapshotted TelemetryConfig #792

Closed

Description

@nockawa

Summary

The SpaceBattle demo's documented deep-profiling switch does not work. Flipping EnableDeepProfiling to true produces no trace file, no warning, and no error — the run completes normally and silently writes nothing.

The engine is not at fault: TYPHON__PROFILER__TRACE works correctly when set before process start. The bug is that the demo sets it inside Main, by which point Typhon.Engine's [ModuleInitializer] has already read and frozen the configuration.

Where it breaks

ProfilerBootstrap.Initialize runs at assembly load and calls TelemetryConfig.EnsureInitialized(), which builds the merged configuration (typhon.telemetry.json overlaid with TYPHON__PROFILER__*) once and caches it. ProfilerLaunchConfig.FromConfiguration then reads Typhon:Profiler:Trace from that frozen snapshot.

Program.ConfigureDeepProfiling sets the variables from inside Main:

// 必须先于 Typhon.Engine 的静态 telemetry 初始化设置,避免 JIT 固化关闭的事件门控。
Environment.SetEnvironmentVariable("TYPHON__PROFILER__ENABLED", "true");
Environment.SetEnvironmentVariable("TYPHON__PROFILER__TRACE", SpaceBattleProductionSettings.GetProfilerTracePath(databaseLocation));

The comment anticipates exactly this hazard — but Main is itself too late, because Main references engine types and so triggers the assembly load (and its module initializer) before its first statement runs. Typhon:Profiler:Trace stays null, no output channel is opened, and the profiler produces nothing.

Reproduction — verified both ways

Same binary, same variable, only the timing differs:

How TYPHON__PROFILER__TRACE is set Result
Externally, before process start ✅ trace written (8.4 MB for 500 ships × 60 ticks)
Inside Main via ConfigureDeepProfiling (current code) ❌ no file, no warning, exit code unchanged
# Fails silently — current documented path
# (EnableDeepProfiling = true in Program.cs)
dotnet run --project demo/SpaceBattle/SpaceBattle.Main -c Release

# Works
TYPHON__PROFILER__TRACE=ext.typhon-trace ./SpaceBattle.Main.exe

Adding "Trace": "default.typhon-trace" to a typhon.telemetry.json beside the assembly also works, since that file is read during the same static init.

Impact

  • doc/demos/space-battle.md documents this switch as the way to emit a .typhon-trace. As written it cannot work, so the demo's profiling story is unusable as documented.
  • SpaceBattle ships no typhon.telemetry.json, so even when the profiler is enabled every sub-gate (GcTracing, CpuSampling, Scheduler, Runtime, ECS, Spatial, …) stays at its disabled default. Both AntHill projects carry one; SpaceBattle was never given the equivalent.
  • The failure mode is the problem as much as the bug: a user follows the documentation, gets a clean run and no output, with nothing pointing at the cause.

Suggested fix

  1. Demo: add demo/SpaceBattle/SpaceBattle.Main/typhon.telemetry.json (mirroring AntHill's gate set) with a Trace key, wired via <Content Include="typhon.telemetry.json" CopyToOutputDirectory="PreserveNewest" />. Drop ConfigureDeepProfiling's env-var writes, or keep them only as an override documented as "must be set before launch".
  2. Docs: correct doc/demos/space-battle.md:77 to describe the config-file / pre-launch-env route.
  3. Engine (the durable fix): make the silent case observable. When TelemetryConfig.ProfilerActive is true but no output channel resolved, emit one warning at ProfilerBootstrap.TryStart — "profiler enabled but no Trace/Live channel configured; no output will be produced". That converts a silent no-op into a one-line diagnostic and would have caught this immediately.

Optionally, TelemetryConfig could expose whether a late Environment.SetEnvironmentVariable was ignored, but the warning above covers the realistic case.

Notes

Found while configuring the demo for a tracing run during a review of demo/SpaceBattle/. With the config-file route in place the demo traces correctly — a 10,000-ship × 800-tick run produced a valid 1.02 GiB TYTR v11 trace including 811,347 CPU samples, so nothing else in the profiling path is broken.

Activity

  1. self-assigned this
    on Aug 12, 2026
  2. added
    documentation
    on Aug 12, 2026
  3. added theissue type on Aug 12, 2026
  4. moved this to Todo in Typhonon Aug 12, 2026
  5. updated Area Observability, Estimate S, Product Engine and Priority P1-High on Aug 12, 2026
  6. nockawa commented on Aug 14, 2026

    @nockawa
    CollaboratorAuthor

    Engine fix landed (03bf261); the demo half is already done on the demo branch; one new trap found

    ③ Engine — done

    ProfilerBootstrap.TryStart returned silently when the master switch was on and neither Trace nor Live resolved. It now emits one diagnostic, once per process, on Console.Error:

    [Typhon] Profiler is ENABLED but no output channel is configured, so NO trace will be produced. Set 'Typhon:Profiler:Trace' … or set TYPHON__PROFILER__TRACE / TYPHON__PROFILER__LIVE in the environment BEFORE the process starts. Setting those variables from inside Main has no effect: the telemetry configuration is read once when Typhon.Engine loads, which happens before Main's first statement runs.

    The last sentence is the part that is not guessable from the symptom, and it is the whole reason this issue exists.

    Console.Error rather than [LoggerMessage] is deliberate: ProfilerBootstrap is a static pre-DI bootstrap with no ILogger, and the startup-failure path a few lines below already uses Console.Error — a host with no logging configured is precisely the host that needs to see this. Say the word if you would rather it went through a logger.

    BuildInertProfilerWarning is a pure null-or-text function with the one-shot latch left in the caller, so its 8 tests need neither the frozen TelemetryConfig singleton nor Console redirection — both of which would have made them order-dependent under parallel fixtures.

    ① and ② — the premise has moved

    This issue states SpaceBattle ships no typhon.telemetry.json. On feature/796-simple-space-battle it does, with "Enabled": true and "Trace": "default.typhon-trace" already set — i.e. the config-file route this issue recommends has since been implemented. I traced the demo successfully through exactly that path today.

    doc/demos/space-battle.md does not exist on main or on the demo branch (doc/demos/ holds only anthill.md, index.md, toc.yml), so ② has nothing to correct. Note also that neither demo/SpaceBattle nor demo/SimpleSpaceBattle exists on main — every blob/main/demo/… link in this issue is a 404.

    Suggest closing once someone confirms ①, since ③ has landed and ② has no target.

    A new trap, worth its own issue

    Program.EnableDeepProfiling is a const bool = false, and BuildDefinition() gates the tick and ship-count overrides behind it:

    maximumCompletedTicks: (ulong)ReadIntOverride("SPACEBATTLE_TRACE_TICKS", 400)

    So with the const off, SPACEBATTLE_TRACE_TICKS and SPACEBATTLE_TRACE_SHIPS are silently ignored and the run falls back to DefaultMaximumCompletedTicks = 45_000 — about 4.5 hours at this demo's ~200 ms/tick. Anyone trying to take a bounded trace hits that with no indication why. Filing separately.

    Second, smaller: the run persists its state, and a prior TimedOut run makes the next launch throw SimulationRun 状态 TimedOut 不可恢复. The database has to be cleared between bounded runs.

  7. added a commit that references this issue on Aug 15, 2026
  8. nockawa commented on Aug 15, 2026

    @nockawa
    CollaboratorAuthor

    Closing — all three parts are resolved.

    • ③ Engine — done in 03bf261 (PR #792 #803 #696 #802 Four bug-bash fixes, and three performance issues that measured flat #808). An enabled-but-inert profiler now emits one diagnostic naming both config keys, both environment variables, and the before-Main constraint.
    • ① Demo — already implemented on feature/796-simple-space-battle: SpaceBattle.Main/typhon.telemetry.json carries "Enabled": true and "Trace": "default.typhon-trace". Verified by using exactly that route to capture a .typhon-trace from the demo.
    • ② Docs — doc/demos/space-battle.md does not exist on main or on the demo branch, so there is nothing to correct.

    The residual traps found while confirming this are filed as #806 (the EnableDeepProfiling const silently disabling the tick/ship overrides, and the non-resumable TimedOut run) rather than kept here.

  9. moved this from Todo to Done in Typhonon Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

documentation

Type

Fields

Priority

P1-High

Estimate

S

Area

Observability

Start date

None yet

Target date

None yet

Bug status

None yet

Product

Engine

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions