Commit graph Warrior_EA/System/AtomicFile.mqh
Author SHA1 Message Date
AnimateDread
41d6a63d92 fix: make sidecar writes atomic; extract shared AtomicFile helper
FileOpen(FILE_WRITE) truncates its target on open. CNet::Save already staged
the .nnw through a temp file + rename for that reason, but the three sidecars
written beside it did not:

  .stats  ExpertSignalAIBase.mqh:5918
  .arrows ExpertSignalAIBase.mqh:6224
  .cfg    ExpertSignalAIBase.mqh:7329

Two defects followed.

1. An interrupted write published a truncated sidecar. For .cfg that is the
   worst case: LoadAndCompareTopologyConfiguration() reads a short file as a
   mismatch, which discards the trained model and restarts from era 0.

2. Windows file sharing is a mutual contract - a writer opened with no
   FILE_SHARE_* blocks every concurrent open regardless of the reader's flags.
   All three read paths carry FILE_SHARE_READ|FILE_SHARE_WRITE specifically so
   a tester agent can read them while a live chart runs; an exclusive writer on
   the same path defeated that.

Extracted CNet::Save's proven pattern into System\AtomicFile.mqh
(AtomicWriteBegin/AtomicWriteEnd) and routed all four writers through it. This
also encodes the FileMove gotcha once instead of per call site: the destination
location comes from FILE_COMMON inside the 4th arg, NOT inherited from the
source, and getting it wrong moves the file to the wrong sandbox silently.

Also fixed while in these functions:

- SaveTopologyConfiguration had 13 copy-pasted 6-line error blocks that each
  returned WITHOUT FileClose(handle), leaking the handle on every write
  failure. Collapsed to one ok-chain that closes exactly once. The on-disk
  field order and types are unchanged (asserted during the rewrite) so existing
  .cfg files still load.

- SaveChartSignals documented that pruning runs only after a successful write
  ("a failed write above leaves both the file AND the chart untouched") but
  never checked any write result, so a partial write still deleted the chart
  objects. Results are checked now, making the existing comment true.

Compiles 0 errors, 0 warnings.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-29 00:31:29 -04:00