plugins icon indicating copy to clipboard operation
plugins copied to clipboard

statechart: silent exit on Windows — CRLF line endings break message parsing + POSIX-only process management

Open xycxmz opened this issue 4 weeks ago • 3 comments

Thanks for maintaining statechart. On Windows, TLC completes normally but the worker then exits silently with code 1 and writes no chart.mmd / report.md. Two related issues in the same file family cause this.

Bug 1 (primary): Windows JVM CRLF breaks @!@!@ message parsing

Symptoms

On Windows (Git Bash or CMD), any statechart subcommand such as node statechart.ts SPEC.tla finishes TLC model checking, then the process exits 1 with no error text and no output artifacts. No crash, no timeout — just a silent failure.

Root cause

run.ts outputLines() splits on byte \n (0x0A) and does not strip trailing \r (0x0D). On Windows, the JVM’s System.out emits CRLF for some lines, including every @!@!@STARTMSG / @!@!@ENDMSG marker line.

Downstream frontends/messages.ts anchors markers with $:

const START = /@!@!@STARTMSG (\d+):(\d+) @!@!@$/;
const END = /^@!@!@ENDMSG \d+ @!@!@$/;

A line like "@!@!@ENDMSG 2192 @!@!@\r" fails the END regex, so the message never closes. Later content is swallowed into one open message part. readVerdict() then sees only 1 coded message (of ~21 envelopes) and judge() falls back to “TLC exited N but its output names no rule that broke,” after which the worker exits quietly.

Evidence

Reproduced with corpus CounterInstance under pstack/skills/statechart/scripts/test/corpus/:

  • TLC stdout (verdict-1.stdout) had mixed endings (27 CRLF, 51 LF)
  • Of 21 @!@!@STARTMSG envelopes, readVerdict recognized only the first banner (code=2262)

Verified against current cached plugin sources: outputLines still does split("\n") with no \r strip, and the $-anchored START/END regexes are unchanged.

Suggested one-line fix (run.ts outputLines)

-    yield* utf8.decode(buf.subarray(0, cut)).split("\n");
+    yield* utf8.decode(buf.subarray(0, cut)).split("\n").map((l) => (l.endsWith("\r") ? l.slice(0, -1) : l));

After this change the corpus path succeeded end-to-end: exit 1 (rule broken = expected semantics), and chart.mmd / report.md / tlc.out / tlc-ReachesThree.out were written with “ReachesThree breaks” in the report.

Bug 2 (secondary): POSIX-only process management on Windows

Even with Bug 1 fixed, Windows still hits process-management failures:

  1. statechart.ts spawns "/bin/sh" as a janitor — that path does not exist on Windows (ENOENT).
  2. run.ts killGroup() runs /bin/sh -c 'kill -s KILL -- "-$pid"' (POSIX process groups). Windows has no negative-PID process-group semantics, so this fails the same way.

Suggested fix

run.ts killGroup():

-  spawn("/bin/sh", ["-c", 'kill -s KILL -- "-$1"', "sh", String(pid)], { stdio: "ignore" }).on("exit", resolve)
+  process.platform === "win32"
+    ? spawn("taskkill", ["/PID", String(pid), "/T", "/F"], { stdio: "ignore" }).on("exit", resolve)
+    : spawn("/bin/sh", ["-c", 'kill -s KILL -- "-$1"', "sh", String(pid)], { stdio: "ignore" }).on("exit", resolve)

statechart.ts janitor:

-    janitor = spawn("/bin/sh", [...])
+    janitor = process.platform === "win32" ? undefined : spawn("/bin/sh", [...])

On win32, cleanup can rely on the existing finally { fs.rmSync } path, with killGroup using taskkill.

Environment

  • Windows 10 19045 x64, Git Bash
  • Node v22.16.0 (--experimental-strip-types)
  • OpenJDK 17.0.18 and Temurin 25.0.5 (Bug 1 reproduces on both; marker lines carry CRLF regardless of Java version)
  • tla2tools.jar v1.7.4 (TLC2 2.19, 08 August 2024)
  • Side note: bun 1.4.4 under Git Bash hangs on statechart (not fully investigated; not blocking)

Happy to adjust the patches if you’d prefer a different Windows process strategy.

xycxmz avatar Nov 04 '26 06:11 xycxmz

@xycxmz both causes here are covered by the open PR #1147 from @Amyssjj:

  • CRLF: outputLines and fileLines in run.ts, and linesOf in frontends/dot.ts, split on /\r?\n/ and drop a trailing \r at chunk boundaries, so the $-anchored STARTMSG/ENDMSG and verdict regexes match again. Its cli.test.ts adds a replay that rewrites the recorded TLC output to CRLF.
  • /bin/sh: killGroup runs taskkill /T /F /PID on Windows, and the cleanup watcher moves from a /bin/sh child to a janitor.ts script run on the worker's runtime.

It also passes TEMP/TMP to the worker and sets windowsHide on the java spawns. If you can, running your CounterInstance case on that branch would confirm it on a real Windows JVM.

deanrie avatar Nov 05 '26 14:11 deanrie

Fixed in #1287: normalize each line in linesOf so the $-anchored @!@!@ENDMSG regex closes on Windows CRLF output. Simulated CRLF repro goes red → green (before: messages never close; after: LF and CRLF parse identically). Full suite 341 tests / 271 pass / 0 fail.

lnuxe avatar Nov 19 '26 12:11 lnuxe

This is now fixed on main by #1292 (maintainer's broader CRLF + native-Windows fix, which also covers Bug 2 of this issue). My narrower #1287 was closed as superseded. #1292 splits on \r?\n in linesOf/fileLines/outputLines and replays every corpus case with Windows line ends.

lnuxe avatar Nov 19 '26 22:11 lnuxe