Skip to main content

Module aside

Module aside 

Source
Expand description

A running server’s program moved aside, so the next build can write a fresh one (ticket 2545).

Windows won’t let a running program’s file be removed or rewritten, and cargo must do both to relink; it does let the file be renamed. So on Windows shuyi mcp and mcp-host move their own program, and the hard-linked twin cargo wrote first (in deps/, or beside an example), into <exe dir>/.shuyi-running/, and run on from there. A copy of each, with its modified time, takes its place: no program runs from the copy, so the next build can replace it, and until then the program is still there to run (another session’s cargo run, a test’s CARGO_BIN_EXE_shuyi) and cargo sees it fresh.

The program is never missing from its path, not even for a moment (ticket 2561): the running file is hard-linked into .shuyi-running/ and the copy renamed over the old name, rather than the file renamed away and the copy renamed in after it. Windows lets a running program’s name be replaced while another name keeps the file.

That replacing is the one moment left, and it is the system’s: on a busy machine a name being replaced can read as not there for tens of milliseconds (ticket 2564 saw it in about one move in 40,000, inside the call, with fs::rename, a POSIX-semantics rename and a replacing hard link alike). No order of the moves avoids it.

Only a cargo build output has a twin: a shuyi installed with cargo install has none and is never moved.

Constants§

RUNNING
The folder, beside the program, its moved files go to.

Functions§

move_aside
Moves exe and its twins into <exe dir>/.shuyi-running/, each as <stem>-<pid>-<n> with exe’s extension, after sweeping that folder, and leaves a copy of each in its place: the new paths, none if exe has no twin. Or why not.
sweep
Deletes every file in dir that can be deleted; a program still running from one refuses, and is left.
this_program
Moves this process’s own program aside, saying on stderr if it couldn’t (the server serves either way).