Skip to content

fix: avoid false watchdog restart right after startup#392

Closed
CJKmkp wants to merge 1 commit intobetafrom
codex/fix-app-forced-restart-on-launch
Closed

fix: avoid false watchdog restart right after startup#392
CJKmkp wants to merge 1 commit intobetafrom
codex/fix-app-forced-restart-on-launch

Conversation

@CJKmkp
Copy link
Member

@CJKmkp CJKmkp commented Mar 5, 2026

Motivation

  • Prevent the watchdog from falsely detecting the app as unresponsive immediately after startup due to stale/timing/race conditions on heartbeat/startup timestamps.
  • Ensure heartbeat and startup timestamps are thread-safe and consistent across threads to avoid premature automatic restarts.

Description

  • Replaced DateTime fields used by the heartbeat/watchdog logic with UTC ticks stored in long fields and accessed with Interlocked to make reads/writes thread-safe (Ink Canvas/App.xaml.cs).
  • Added ReadUtcTicks helper to convert atomic tick values into DateTime consistently and moved all timestamp writes (splash, app startup, startup-complete, heartbeat) to UTC ticks via Interlocked.Exchange.
  • When the main window Loaded event records the startup-complete heartbeat, the code now also updates the live heartbeat timestamp to avoid stale pre-startup heartbeats triggering a restart.
  • Introduced a StartupCompleteGracePeriod (30s) that skips post-startup hang detection immediately after startup completeness is recorded to avoid noisy restarts during final initialization.

Testing

  • Attempted to build with dotnet build "Ink Canvas.sln" -c Release, but the build could not be run in this environment because dotnet is not installed (build failed).

Codex Task

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e7af98721d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 1080 to +1082
isStartupComplete = true;
startupCompleteHeartbeat = DateTime.Now;
var startupCompleteHeartbeat = DateTime.UtcNow;
Interlocked.Exchange(ref startupCompleteHeartbeatTicksUtc, startupCompleteHeartbeat.Ticks);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Record startup heartbeat before marking startup complete

The Loaded handler publishes isStartupComplete before updating startupCompleteHeartbeatTicksUtc/lastHeartbeatTicksUtc, but watchdogTimer reads these values from another thread. In the small window after isStartupComplete becomes true, the watchdog can still see an old heartbeat and a MinValue startup-complete timestamp, which bypasses the new grace-period check and can trigger the same immediate post-startup restart this change is trying to prevent. Write the timestamps first and only then mark startup complete (or enforce ordering with synchronization).

Useful? React with 👍 / 👎.

@CJKmkp CJKmkp closed this Mar 5, 2026
@CJKmkp CJKmkp deleted the codex/fix-app-forced-restart-on-launch branch March 5, 2026 03:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant