Entry 623

The launcher's build in a process of its own

Files8 in 5 directories
crates/piney-launcher/src/builder.rsjob.rsmain.rsapp.rscrates/piney-launcher/Cargo.tomlCargo.lockcrates/piney-build/src/build.rsdocs/port/launcher.md

Mentioned incrates/piney-launcher/src/job.rs:37

The launcher's build now runs in a process of its own, the same program again in build mode, as Play runs the game. 622 measured why: a build on a thread of the window's process left glibc holding the build's freed memory for the rest of the session. The maintainer chose the child process over trimming the heap, so the memory goes back to the system when the build ends on every platform, and a crash in the builder cannot close the window (619 is the launcher).

Build mode

piney-apples --build-job DIR DISCS... runs piney_build::build::run with the build folder and the discs given, verified, as the window asked for before. It is the window's own mode and stays out of --help; its paths are taken from args_os, whatever their encoding. The window and --build both start it through BuildJob::start(program, ...), and program is the one Play uses: the executable, or $APPIMAGE in an AppImage, which mounts itself again. The child resolves its portable folder as the window does before it builds.

The line protocol

builder.rs holds both ends' types: Message (child to window) and Order (window to child), each with line() and parse().

linemeaning
say TEXTa line of the account, \, \n, \r escaped
progress V STEP DONE TOTALvolume 1-4, files, port-data or verify
done, cancelled, failed TEXTthe outcome, the last line
cancel (on stdin)stop at the next chance

The builder tells progress for every 64 KiB chunk. The child sends it no more than every 50 ms per step, but always at a step's start and end: an Infection build is 127 lines, 105 of them progress. A line that parses as none (a stray print, libtest's own lines in the tests) is written to the log as (not a message from the builder) ... and the build goes on.

The window's side

BuildJob keeps its API (start, snapshot, cancel, finished, log), so app.rs changes in two calls. Three threads replace the build thread:

  • one reads stdout into Shared and the log, the account as before;
  • one copies stderr into the log and keeps its last 12 lines;
  • one watches the process.

When the process has exited, the watcher sets the outcome, so Cancel and close never closes the window before the build has stopped. If the process ended without an outcome line (a crash, or the kill below), its .part files are removed, and the build counts as failed: "the builder stopped (exit status: 101): ..." with the stderr tail. The one exception is a process ended after Cancel, which counts as cancelled.

Cancel, and a window that is gone

Cancel sets the flag and writes cancel on the child's stdin. A thread in the child sets the flag Report::cancelled reads. That thread also cancels when stdin reaches its end or a stdout write fails: a closed or crashed window leaves no build running. A child that sends nothing for 120 s after Cancel is killed. The builder polls between chunks and files and sends progress all the while. Making a disc's port data is its one quiet stretch, 0.9 s for Infection. remove_partial_build moved to job.rs for the kill path.

Reading build::run for this turned up a hole in "the build before it is as it was". The last cancel check is before chunks.pak.part and the .disc.part files are renamed into place. The verify that follows still polled the flag, so a cancel then returned Cancelled with the new build in place under the old stamp. The window then said the game was as it was before. Verify now reports through Committed, a Report that never says cancelled, so the check runs to its end. The account notes once that the cancel came too late. The build then ends Done, which is what it is.

A second hole: a window opened while another window's build ran removed that build's .part files at its start, and could start a build of its own beside it. The build's process now holds building.lock in the data folder (job::Lock with Busy::Building, beside Busy::Playing's playing.lock). A window starting while it is held leaves the .part files alone, and its own build waits with a note; the CLI's --build refuses. the_game_lock checks the two locks are separate.

Windows

The executable is windows_subsystem = "windows", so the child opens no console. It is still started with CREATE_NO_WINDOW through CommandExt, since a console-subsystem build would open one. A GUI-subsystem process gets the piped handles as its standard handles (STARTF_USESTDHANDLES), and Rust's stdin() and stdout() use them. When the window's process dies, Windows' pipe read returns ERROR_BROKEN_PIPE, which std reads as end of file, so the cancel-on-EOF holds there too. Checked:

  • cargo check and cargo clippy --all-targets -D warnings of piney-launcher for x86_64-pc-windows-gnu;
  • under Wine 11.21, a GUI-subsystem piney-apples.exe from the same tree (x86_64-pc-windows-gnu): --add-disc ISO --build started its build process with piped stdin, stdout and stderr and CREATE_NO_WINDOW, and printed its progress to build: Done. With the parent killed mid-build, the child stopped 100 ms later and the build was as it was.

Measured

Isolated nested kwin_wayland --virtual (its own Xwayland, DBus, runtime folder, home), Infection added with --add-disc so the window starts the build, sampled every second from /proc:

0.1.1, build on a thread0.1.2, build in a process
launcher idle353 MB (97 MB anonymous)287-302 MB (78)
launcher during the buildto 983 MB (peak)302-306 MB (79)
launcher 30 s after837 MB (608)306 MB (79)
the build's own peakin the launcher500 MB, its process

The child's peak (VmHWM polled every 20 ms, headless) is 500 MB. The same Infection build in 0.1.1's headless --build, on a thread of that process, peaks at 499 MB. The launcher no longer moves during or after a build. Four discs in one build's process peak at 695 MB, take 173 s and write 673 lines.

The longest quiet stretch in that four-disc build is Outbreak's port data at 57.2 s (Infection 0.9 s, Mutation 16.0 s, Quarantine 41.4 s): the generator does not ask to stop. 120 s of quiet after Cancel leaves room for that on slower machines. Should a slower machine pass it, the process is ended in its port data, before the replace, and the old build stays as it was. The kill is that safe at any quiet point: after the replace, the remaining steps are a few renames and the verify, which sends progress all the while.

End to end, in the nested session

  • Build from the window: --add-disc Infection. The panel showed "Reading the disc: 0.6 of 2.6 GB", and the build finished in 15 s. Play then started --play --game ... --volume 1, and Infection's desktop came up ("You have new mail").
  • Auto rebuild (a stale stamp): "Updating the game data", finished.
  • Cancel mid-build: a stale stamp starts the rebuild; Cancel clicked at 1 s. "build cancelled" came within 1 s. chunks.pak, infection.disc and build.txt kept their inodes and times, stamp.txt stayed byte for byte the same, no .part was left, and no child was left.
  • Close during a build: the prompt, then "Cancel the build and close". The window closed once the child had stopped; the build was as it was.
  • The launcher killed (SIGKILL) mid-build: its child stopped 152 ms later on the end of its stdin, the build as it was, no .part left.
  • Headless: --add-disc ISO --build printed progress through the child and ended build: Done; --status said Infection Ready.

Checks

  • Crate tests (cargo test --workspace --exclude piney-game): 843 pass, 0 fail, 1 ignored. That is 831 and 12 new:
    • the protocol: every message round-trips, unreadable lines;
    • the child: its lines and thinning, stdin's end cancels, a cancel line cancels and another line does not, a panic is a failed build;
    • the window against this test binary as a fake child: a cancelled build, the window gone, a child that crashed without an outcome, a child deaf to cancel ended and its .part removed;
    • build's Committed. The tests that start a process hold a lock that the_game_lock holds too: a child shares the test's open files between fork and exec, the game lock's file among them, which failed that test once.
  • piney-game (cargo test -p piney-game --release): 364 / 0 / 154.
  • Clippy (workspace, all targets and features, -D warnings), fmt, cargo deny check, tools/docs.py check: clean.
  • piney-launcher 0.1.2.
Still unknown

how the build's process behaves on a real Windows machine. Under Wine 11.21 it started without a console, its pipes carried the protocol, and it stopped on its own when the parent was killed. CI's Windows job compiles it and runs no tests.