Skip to content

Run scenarios on the thread pool, queue System Log entries - #13

Merged
Prengemann merged 1 commit into
mainfrom
fix/26-szenario-vom-ui-thread
Nov 15, 2026
Merged

Prengemann merged 1 commit into
mainfrom
fix/26-szenario-vom-ui-thread

Conversation

@Prengemann

Copy link
Copy Markdown
Contributor

Why

A scenario ran entirely on the WPF UI thread (internal board, issue 26). ScenariosViewModel.RunOnceAsync called the SecsGemBase engine on the dispatcher, and the engine awaits without ConfigureAwait(false), so every node continued there.

  • Timing: the time between two nodes depended on what the UI was busy with (message log, system log, canvas). For the planned connection-drop node that latency sits between the triggering Receive and the drop.
  • Freezes: a node that blocks froze the window.
  • Logging: every ILogger call waited for the UI thread (Dispatcher.Invoke), also from the receive loop.

Changes

  • Services/ScenarioRun.RunAsync (new): takes a snapshot of the graph on the UI thread and starts the engine with Task.Run. RunOnceAsync continues on the UI thread after the await; the Progress<T> for the canvas is created there.
  • ScenarioRun.Snapshot: a copy of graph, nodes and edges that nothing else refers to.
    • The stored graph changes on the UI thread during a run: opening a scenario clears TransactionJson of its nodes. The engine reads a node only when it reaches it.
    • A node from an older file that only names its transaction gets a copy of the library transaction. The engine does not read or write the UI-bound library for it, and a run no longer changes the library transaction.
    • That copy is written with the invariant culture. The SecsGemBase converter writes values with the system culture and reads F4/F8 invariant, which turns 1,5 into 15 on a decimal-comma system (internal SecsGemBase issue 68, reproduced with packages 0018 and 0027).
  • Logging/ObservableLogger: queues entries with Dispatcher.InvokeAsync, like SecsMessageLogger. It takes an optional dispatcher provider and drops the entry when there is no dispatcher (unit tests, shutdown); before, such a call threw.
  • ScenariosViewModel.LogInfo/LogError: go through the logger instead of adding to the collection directly. A direct add overtook engine entries that were still queued: after a cancel, "cancelled by the user" came before the engine's "was cancelled". The unused ILogService constructor parameter is gone.
  • Two guards the engine did not need on a single thread (internal SecsGemBase issue 69):
    • Runs enter the engine one at a time. It keeps the run's cancellation source in a field while it starts, and two runs starting at the same instant could pick up each other's source.
    • An And node's late "1/2 branches" report no longer replaces its Succeeded state.
  • specification/features.md: new section.

Known limits

  • Unknown transaction names: for a name the library does not know, the engine still searches the library from its own thread and reports the node as failed.
  • And node with three or more inputs: two "n/m branches" reports can arrive in swapped order, so the tooltip may show the lower count until the last branch arrives. The engine reports outside its lock (issue 69).
  • Canvas nodes on a decimal-comma system: they still serialize with the system culture (ScenarioNodeViewModel.ToModel). That is issue 68, which also affects saved libraries and the item editor, and it is not changed here.
  • View-model wiring: RunOnceAsync calling RunAsync and IsStale is not covered by a unit test. The view model needs the whole DI graph and reads the user's profile in its constructor. It is covered by the smoke run below.
  • Uncommitted CLAUDE.md rewrite: the local, uncommitted rewrite of CLAUDE.md in the main checkout says ObservableLogger throws in tests. After this PR it drops the entry.

Verification

  • Build and tests: Linux SDK 10.0.404, Release build of app and tests with 0 warnings, 0 errors. Tests under Wine with Windows .NET: 29/29 pass, 10 runs in a row.
  • New tests: 22 new test cases.
    • ScenarioRunTests: the caller is a thread with a real Dispatcher; the engine sees no context, at start and after its first await. Also: the snapshot is independent of the stored graph, name-only nodes get their copy, floats survive under de-DE, node, edge and graph properties are all copied, starts are serialized, stale reports are dropped.
    • ObservableLoggerTests: a log call returns while the UI thread is busy, entries arrive in call order on the UI thread, and without a dispatcher the entry is dropped.
  • Counter-checks: 14 mutations, each applied alone; every one turns the matching tests red:
    • no Task.Run
    • an await before the engine call
    • no start lock
    • engine gets the stored graph
    • Dispatcher.Invoke in the logger
    • shared nodes
    • shared edges
    • a forgotten node property
    • a forgotten edge property
    • Wait nodes resolved too
    • case-sensitive name match
    • system culture
    • IsStale never true
    • IsStale for every Waiting report
  • Smoke run in the real app (Wine, Xvfb, throwaway profile with a Start → Wait 4000 → End scenario):
    • Run, Cancel during the wait, and loop with stop during the wait all work.
    • The System Log shows the engine's and the view model's lines in order, with monotonic timestamps.
    • Run is disabled while the scenario runs.
    • The app closes cleanly.
  • Independent review: three lenses (thread affinity, logger, snapshot and tests), every finding checked by a second agent, then a second round on the fixes. The findings are fixed or listed under "Known limits".
  • Not tested on Windows itself; CI runs the tests on windows-latest.

🤖 Generated with Claude Code

A scenario ran entirely on the WPF UI thread: RunOnceAsync called the
engine on the dispatcher, and the engine awaits without
ConfigureAwait(false). The time between two nodes depended on the UI's
load, and a blocking node froze the window. Every log call also waited for
the UI thread (Dispatcher.Invoke).

- Services/ScenarioRun.RunAsync takes a snapshot of the graph on the UI
  thread and starts the engine with Task.Run. RunOnceAsync continues on
  the UI thread after the await.
- The snapshot is a copy nothing else refers to: opening a scenario
  during a run clears TransactionJson of the stored nodes. A node from an
  older file that only names its transaction gets a copy of the library
  transaction, so the engine does not read or write the UI-bound library
  for it. The copy is written with the invariant culture; the SecsGemBase
  converter otherwise turns 1,5 into 15 on a decimal-comma system.
- ObservableLogger queues entries with Dispatcher.InvokeAsync, like
  SecsMessageLogger, takes an optional dispatcher provider and drops the
  entry when there is no dispatcher. ScenariosViewModel writes its own
  lines through the logger, so they keep their place between the engine's
  lines.
- Runs enter the engine one at a time (it keeps the run's cancellation
  source in a field while it starts), and an And node's late "1/2
  branches" report no longer replaces its Succeeded state. Both were
  harmless on a single thread.
- specification/features.md: new section.

New tests: ScenarioRunTests and ObservableLoggerTests.

Internal board: secsgem/evencoolerfastsim issue 26.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Prengemann
Prengemann merged commit d17a786 into main Nov 15, 2026
1 check passed
@Prengemann
Prengemann deleted the fix/26-szenario-vom-ui-thread branch November 15, 2026 15:53
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