- Rust 59.6%
- Go 40.4%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
All checks were successful
Suite release / deliver-prune (push) Has been skipped
Suite release / finalize (push) Has been skipped
Suite release / check-package (push) Has been skipped
Suite release / reused-checks (push) Has been skipped
Suite release / scope (push) Successful in 21s
Suite release / build (none, ubuntu-latest) (ubuntu-latest, none) (push) Has been skipped
Suite release / deploy-image (push) Has been skipped
Suite release / deliver (push) Has been skipped
Suite release / reserve (push) Has been skipped
Suite release / publish (push) Has been skipped
Suite release / module (push) Has been skipped
Suite release / check-manifest (push) Has been skipped
Suite release / check-build (none, ubuntu-latest) (ubuntu-latest, none) (push) Has been skipped
|
||
| rust | ||
| .gitattributes | ||
| .gitignore | ||
| Cargo.lock | ||
| Cargo.toml | ||
| go.mod | ||
| go.sum | ||
| lock_other.go | ||
| lock_unix.go | ||
| lock_windows.go | ||
| lock_windows_test.go | ||
| onecopy.go | ||
| onecopy_test.go | ||
| README.md | ||
one-copy
One copy of each interactive home app, and the library every app holds it with
(FP-17,
AHT-218). Nelson decided every
interactive app in the at menu is one copy: the menu brings its one window to you, and a
second copy opened some other way (typed in a shell, started by a script or a Claude
session) refuses to start. A terminal app prints <app> is already open and exits; a
desktop app (String Lab) brings the running copy's window forward instead.
A Go module and a Rust crate with the same behavior, taken as logline is. mGBA, in C, keeps its own code for the same lock.
Using it
Call it at the start of the app's interactive run, never at the top of main: a
subcommand, a --list, a version, a --demo or an engine the app starts must still run
while the app is open (guitar-tabs' import, shows-remote's status, fisch-coach's
engine). The lock is held until the process ends.
Go (github.com/romaine-life/one-copy, package onecopy):
require github.com/romaine-life/one-copy v0.1.1
import onecopy "github.com/romaine-life/one-copy"
func openUI() {
onecopy.ClaimOrExit("guitar-tabs") // "guitar-tabs is already open" on stderr, exit 1
...
}
Rust (one-copy, by git tag):
one-copy = { git = "https://forgejo.romaine.life/romaine-life/one-copy", tag = "v0.1.1" }
// A terminal app: "tuner is already open" on stderr, exit 1.
one_copy::claim_or_exit("tuner");
// A desktop app: brings the running copy's window forward and exits 0. An error is a
// lock that couldn't be checked; the app runs on and logs it.
if let Err(error) = one_copy::claim_or_focus("string-lab") {
log.warn("one-copy check failed", &[("error", &error)]);
}
| Go | Rust | Does |
|---|---|---|
ClaimOrExit(name string) |
claim_or_exit(name: &str) |
Holds the lock for the process's life, or prints <name> is already open to stderr and exits 1 |
claim_or_focus(name: &str) -> Result<(), Error> |
Holds the lock for the process's life, or brings the running copy's window forward and exits 0 | |
Claim(name string) (*Lock, error) |
claim(name: &str) -> Result<Lock, Error> |
The lock as a value: ErrAlreadyOpen / Error::AlreadyOpen when another copy holds it (when it answers) |
(*Lock).Release() |
drop(lock) |
Lets it go before the process ends |
focus_running(name: &str) -> bool |
Brings the holder's window forward; false when it found none |
A name is letters, digits, ., _ and -: the app's own name, as the at menu knows it.
When the lock can't be checked at all (the OS refused to make it), ClaimOrExit and
claim_or_exit print <name>: couldn't check for another open copy (<error>); starting anyway and the app runs, as at-terminal's lock does: a lock that fails never keeps him
out of his app.
The lock
- Windows: the named mutex
Local\one-copy-<name>, in his sign-in session's namespace. The handle isn't inherited, so a program the app starts never holds it, and Windows destroys the mutex with its last handle when the app ends, crash included. The copy that takes it also records its process id in the named shared memoryLocal\one-copy-<name>-pid(4 bytes, a native-endianu32), which Go and Rust write alike, so a copy in either language reads the other's. - macOS and Linux: an
flockonone-copy/<name>.lockin$XDG_RUNTIME_DIR, else the user's cache folder (~/Library/Caches,$XDG_CACHE_HOMEor~/.cache), opened close-on-exec. The lock goes with the process; the file stays, empty. - Answering: on Windows, a claim that finds the lock held reads the holder's recorded
process id and answers at once (
<name> is already open, or the window brought forward) when that process is running and is the same program: it opens (PROCESS_QUERY_LIMITED_INFORMATION), isn't signaled, its exit code is stillSTILL_ACTIVE, and its image's file name (QueryFullProcessImageNameW, compared without its folder or case) is this process's own. A second copy of a running app is told in milliseconds. - A copy still ending: when the holder isn't seen running, the claim tries again for up
to 5 seconds before it says another copy is open: the holder has made the mutex but not
yet recorded its id, it has ended or is ending, or its id now names some other program (a
holder that died and whose id Windows gave to a new process). looper and tuner update
themselves with
tmux respawn-pane -k, which terminates the old copy and starts the new one in the same step (at-tmuxwin32/win32-pty.c:416-418), so the new copy starts while the old one's lock is still going. A terminated process stays unsignaled for a while with the lock still held, but its exit code is set at once, so the exit code is what tells it from a running one. On macOS and Linux theflockrecords no holder, and a claim that finds it held always tries again for the 5 seconds. - Test runs:
ONE_COPY_SCOPE, when set, is appended to the name (one-copy-tuner-<scope>), so a copy started on a throwaway tmux server runs beside his instead of being refused, asFZT_FILES_DATAmoves fzt-files' data. It takes the same characters as a name.
Desktop apps (Rust)
focus_running reads the holder's recorded process id, finds that process's visible
top-level window (EnumWindows, GetWindowThreadProcessId), restores it if minimized and
calls SetForegroundWindow. The second copy was just started by him, so it holds the right
to give the foreground away. On macOS and Linux there is no window to find:
focus_running returns false and claim_or_focus just exits 0.
How it fits together
| Path | Holds |
|---|---|
onecopy.go |
The Go package: names, Claim, ClaimOrExit |
lock_windows.go, lock_unix.go, lock_other.go |
Go's lock per platform (other is any platform no home app runs on, where a claim reports it has no lock and the app starts); on Windows also the recorded process id and the check that its holder is running |
onecopy_test.go, lock_windows_test.go |
Go's tests (the second, Windows' recorded holder); TestMain is also the child process they start |
rust/ |
The Rust crate (a workspace member of the root Cargo.toml) |
rust/src/windows.rs, rust/src/unix.rs, rust/src/other.rs |
Rust's lock per platform; on Windows also the recorded process id, the check that its holder is running, and the window search |
rust/src/tests.rs |
Rust's tests; its child test is also the child process they start |
Testing
go test ./...
cargo test && cargo clippy --all-targets -- -D warnings
Each language's tests claim a name twice and see the second refused, release it and see
it taken, see ONE_COPY_SCOPE give a separate lock, and run child processes: to see
ClaimOrExit / claim_or_exit print its line and exit 1, to see a lock freed when its
holder exits without letting it go, and to see a claim made the moment its holder is
terminated take the lock, as tmux's respawn does. On Windows they also read back the
holder's recorded process id, see a running child holder answered in under 1.5 seconds
(it takes milliseconds; the retry would take 5), and see a holder not seen running waited
for and its lock taken once it goes: one with no id recorded, one whose recorded process
has ended, and one whose id names another program. Go's also see a lock nothing refers to
stay held through garbage collection. Every name is unique to the test run,
so a run beside his open apps never touches their locks, and on macOS and Linux no lock
file outlives the run. Nothing opens a window: bringing one forward is checked by hand in
the first desktop app that takes it.
How it ships
pipeline-templates' go, rust and release suites, topic release-module, with tests
on Linux and Windows: every pull request runs both languages' tests and lints, and a merge
that changes the code takes the next tag (v0.1.N) and publishes it with its source. Each
fleet repository whose go.mod requires the module gets a take-up PR; Rust users move
their git tag. Go fetches the module by its GitHub path (a push mirror of this
repository), which the go suite maps to Forgejo; Cargo fetches from Forgejo directly. The
repository is public on both, so a build reads it with no credentials.