feat(libkernel): stop a second copy of a title with ANYPS5_INSTANCE_LOCK #12970

Merged
MarcZX10 merged 23 commits from upstream-pr/12970 into main 2026-11-08 08:16:59 +00:00
MarcZX10 commented 2026-10-29 17:55:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T17:55:41Z.


What

Two copies of one title started from the same directory share its save data (_sd/), app data (data/) and download data (download0/), all relative to the working directory (core/libs/prx/libSceSaveData.native/Export.cpp, core/libs/prx/libkernel/AppMetadata/src/AppMetadata.cpp). Nothing stops the second copy today.

New opt-in ANYPS5_INSTANCE_LOCK=on: while a title runs, the process holds an operating-system lock on .anyps5-instance.lock in its working directory (the directory that holds app0/). A second copy started from the same directory stops at start-up with an error naming the title ID, the directory, the lock file and ANYPS5_INSTANCE_LOCK=off. No PID file, no timeout, no deleting of stale files: the kernel drops the lock when the process ends or crashes. This is not a backup or a save-data safety net.

Existing locks, not duplicated: PipelineJournal.cpp locks its own journal file (flock/LockFileEx); #12060 only makes the guest's flock on a directory descriptor conflict inside the process on Windows. Neither guards the title's data directory, and this PR touches neither file. Open-PR searches for lock, flock, single instance, title id and instance found no other implementation.

Defaults: unset, empty or off changes nothing (no file is opened or created). It is opt-in rather than on by default because, measured with ANYPS5_INSTANCE_LOCK=on exported over the existing libkernel fixtures that carry a title (download_data_mount_*, param_json_parser, guest_app_info, kernel_error, run with ctest -j8), download_data_mount_invalid (a deliberately malformed param.json) stops at start-up with the lock error instead of at its first title ID query. The other 8 pass. The Windows restart handoff, titles that start a copy of themselves and network file systems are also unmeasured (see debt entries). Invalid values stop with an error naming the variable.

Design (core/libs/prx/libkernel/System):

  • IInstanceLockBackend (InstanceLock.hpp): TryLock(file) returns false when held, throws on any other failure; the lock lives as long as the backend object.
  • Backends: InstanceLockPosix.cpp (open with O_CLOEXEC, flock(LOCK_EX | LOCK_NB); execv in sceSystemServiceLoadExec releases it for the new image) and InstanceLockWindows.cpp (CreateFileW with read and write sharing, one-byte exclusive LockFileEx with LOCKFILE_FAIL_IMMEDIATELY).
  • InstanceLockStartup.cpp: a libkernel static initializer, like the host CPU check, parses the variable, reads the title ID and takes the lock; a directory without param.json/param.sfo takes none. ReleaseHostInstanceLock is called by sceSystemServiceLoadExec before CreateProcessW on Windows so the next process does not meet its parent's lock.

Commits (21): interface and parsing, test; POSIX backend, test; Windows backend, test registration for Windows; start-up wiring, test; Windows LoadExec release; unreadable title ID message, test; docs/user/USAGE.md (3), docs/dev/ENVIRONMENT.md, six docs/dev/debt/libkernel.md entries (network file systems, Windows, macOS, start-up location, restart and self-started copies, default off).

Overlaps: none with the named open PRs in code. #12060 also edits docs/dev/debt/libkernel.md; this PR only appends entries at the end of the Functional section, so a textual conflict is possible but trivial.

Tested

Linux x86_64 (Debian, kernel 6.1), GCC, archive build of the branch with the host-CPU gate patched by the helper, ctest:

  • New: instance_lock (parsing, 7 rejected spellings, acquire with a fake backend, error contents), instance_lock_host (real flock: second handle refused, release on destruction, independent roots, uncreatable file throws, double lock throws, exec'd child does not inherit), instance_lock_startup_{held,unset,off,notitle,second,invalid,unreadable} (real processes: lock held for the process lifetime and released by ReleaseHostInstanceLock; unset/off/no title create no file; a real second copy stops with the full error; invalid values stop naming the variable; unreadable title ID stops). All 9 pass, each under 0.2 s, repeated 3 times with -j8.
  • Existing, per test, baseline (upstream/main bb04a4f1ea) and branch with the variable unset: kernel_error, download_data_mount_{declared,none,invalid,missing,sfo}, param_json_parser, guest_app_info pass on both. host_cpu fails on both (this host's CPU, unrelated); guest_kernel_errors is not built in either run.
  • Negative controls, each by editing the code in the scratch tree and restoring: mode parsing (accepting ON, unset or empty as on) fails instance_lock; dropping the title ID, changing the lock path or the variable from the error fails instance_lock; removing flock, O_CLOEXEC, the close on destruction, the EWOULDBLOCK handling or the open-failure throw fails instance_lock_host; ignoring the variable, locking without a title, never locking, not keeping the lock, a no-op release, and removing the unreadable-ID wrapper fail the matching instance_lock_startup_* cases. Not controlled: the whole Windows backend and the Windows LoadExec release.
  • Windows sources (InstanceLockWindows.cpp, both test sources, the start-up file) compile with x86_64-w64-mingw32-g++ -fsyntax-only.
  • Checks clean: check_conventions.py --base upstream/main, git diff --check, test_public_headers.py --audit-only, env_reference.py --check.

NOT RUN: Windows (backend, instance_lock_* tests, the LoadExec release), macOS, network file systems, a real title, a console, a GPU, the full ctest suite.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5073014376 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12970). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T17:55:41Z. --- ### What Two copies of one title started from the same directory share its save data (`_sd/`), app data (`data/`) and download data (`download0/`), all relative to the working directory (`core/libs/prx/libSceSaveData.native/Export.cpp`, `core/libs/prx/libkernel/AppMetadata/src/AppMetadata.cpp`). Nothing stops the second copy today. New opt-in `ANYPS5_INSTANCE_LOCK=on`: while a title runs, the process holds an operating-system lock on `.anyps5-instance.lock` in its working directory (the directory that holds `app0/`). A second copy started from the same directory stops at start-up with an error naming the title ID, the directory, the lock file and `ANYPS5_INSTANCE_LOCK=off`. No PID file, no timeout, no deleting of stale files: the kernel drops the lock when the process ends or crashes. This is not a backup or a save-data safety net. **Existing locks, not duplicated:** `PipelineJournal.cpp` locks its own journal file (`flock`/`LockFileEx`); #12060 only makes the guest's `flock` on a directory descriptor conflict inside the process on Windows. Neither guards the title's data directory, and this PR touches neither file. Open-PR searches for lock, flock, single instance, title id and instance found no other implementation. **Defaults:** unset, empty or `off` changes nothing (no file is opened or created). It is opt-in rather than on by default because, measured with `ANYPS5_INSTANCE_LOCK=on` exported over the existing libkernel fixtures that carry a title (`download_data_mount_*`, `param_json_parser`, `guest_app_info`, `kernel_error`, run with `ctest -j8`), `download_data_mount_invalid` (a deliberately malformed `param.json`) stops at start-up with the lock error instead of at its first title ID query. The other 8 pass. The Windows restart handoff, titles that start a copy of themselves and network file systems are also unmeasured (see debt entries). Invalid values stop with an error naming the variable. Design (`core/libs/prx/libkernel/System`): - `IInstanceLockBackend` (`InstanceLock.hpp`): `TryLock(file)` returns false when held, throws on any other failure; the lock lives as long as the backend object. - Backends: `InstanceLockPosix.cpp` (`open` with `O_CLOEXEC`, `flock(LOCK_EX | LOCK_NB)`; `execv` in `sceSystemServiceLoadExec` releases it for the new image) and `InstanceLockWindows.cpp` (`CreateFileW` with read and write sharing, one-byte exclusive `LockFileEx` with `LOCKFILE_FAIL_IMMEDIATELY`). - `InstanceLockStartup.cpp`: a libkernel static initializer, like the host CPU check, parses the variable, reads the title ID and takes the lock; a directory without `param.json`/`param.sfo` takes none. `ReleaseHostInstanceLock` is called by `sceSystemServiceLoadExec` before `CreateProcessW` on Windows so the next process does not meet its parent's lock. Commits (21): interface and parsing, test; POSIX backend, test; Windows backend, test registration for Windows; start-up wiring, test; Windows LoadExec release; unreadable title ID message, test; `docs/user/USAGE.md` (3), `docs/dev/ENVIRONMENT.md`, six `docs/dev/debt/libkernel.md` entries (network file systems, Windows, macOS, start-up location, restart and self-started copies, default off). **Overlaps:** none with the named open PRs in code. #12060 also edits `docs/dev/debt/libkernel.md`; this PR only appends entries at the end of the Functional section, so a textual conflict is possible but trivial. ### Tested Linux x86_64 (Debian, kernel 6.1), GCC, archive build of the branch with the host-CPU gate patched by the helper, `ctest`: - New: `instance_lock` (parsing, 7 rejected spellings, acquire with a fake backend, error contents), `instance_lock_host` (real flock: second handle refused, release on destruction, independent roots, uncreatable file throws, double lock throws, exec'd child does not inherit), `instance_lock_startup_{held,unset,off,notitle,second,invalid,unreadable}` (real processes: lock held for the process lifetime and released by `ReleaseHostInstanceLock`; unset/off/no title create no file; a real second copy stops with the full error; invalid values stop naming the variable; unreadable title ID stops). All 9 pass, each under 0.2 s, repeated 3 times with `-j8`. - Existing, per test, baseline (`upstream/main` bb04a4f1ea) and branch with the variable unset: `kernel_error`, `download_data_mount_{declared,none,invalid,missing,sfo}`, `param_json_parser`, `guest_app_info` pass on both. `host_cpu` fails on both (this host's CPU, unrelated); `guest_kernel_errors` is not built in either run. - Negative controls, each by editing the code in the scratch tree and restoring: mode parsing (accepting `ON`, unset or empty as on) fails `instance_lock`; dropping the title ID, changing the lock path or the variable from the error fails `instance_lock`; removing `flock`, `O_CLOEXEC`, the close on destruction, the EWOULDBLOCK handling or the open-failure throw fails `instance_lock_host`; ignoring the variable, locking without a title, never locking, not keeping the lock, a no-op release, and removing the unreadable-ID wrapper fail the matching `instance_lock_startup_*` cases. Not controlled: the whole Windows backend and the Windows LoadExec release. - Windows sources (`InstanceLockWindows.cpp`, both test sources, the start-up file) compile with `x86_64-w64-mingw32-g++ -fsyntax-only`. - Checks clean: `check_conventions.py --base upstream/main`, `git diff --check`, `test_public_headers.py --audit-only`, `env_reference.py --check`. NOT RUN: Windows (backend, `instance_lock_*` tests, the LoadExec release), macOS, network file systems, a real title, a console, a GPU, the full ctest suite. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
CnRJay commented 2026-11-01 19:49:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:49:12Z.

Review: COMMENTED


Correction (2026-11-01): this review demanded a title for a PR where no title can supply one. CONTRIBUTING allows Unblocks: none. The technical points below are unaffected.

Three points.

  1. No title or log line. This adds a host-side feature that no PS5 title calls, so the "a title reaches this on main" bar cannot be met by a trace; the PR needs to say who asked for it (a discussion or issue number) and why it belongs in libkernel rather than in a launcher script. Right now "Unblocks" names nothing.

  2. Work in a static initializer. InstanceLockStartup.cpp runs at libkernel load: it parses the variable, reads param.json/param.sfo for the title ID and creates and locks .anyps5-instance.lock in the working directory. Failure there stops the process before main, and the PR itself measured one existing fixture (download_data_mount_invalid) changing behaviour under on because the title ID is now read earlier than before. Can the title-ID read and lock be deferred to the existing startup path instead, so a malformed param.json still fails where it does today?

  3. Scope of the lock. The lock only protects two copies launched from the same working directory, which is the only thing the code can see; the body says restart handoff, self-started copies and network file systems are unmeasured and leans on six new debt entries for them. Please confirm each of those six is a gap in behaviour that exists (not documentation of the new feature's own limits), since the debt files are for missing PS5 behaviour.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729921438 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12970#pullrequestreview-5729921438). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:49:12Z. **Review:** COMMENTED --- **Correction (2026-11-01):** this review demanded a title for a PR where no title can supply one. CONTRIBUTING allows `Unblocks: none`. The technical points below are unaffected. Three points. 1. **No title or log line.** This adds a host-side feature that no PS5 title calls, so the "a title reaches this on main" bar cannot be met by a trace; the PR needs to say who asked for it (a discussion or issue number) and why it belongs in `libkernel` rather than in a launcher script. Right now "Unblocks" names nothing. 2. **Work in a static initializer.** `InstanceLockStartup.cpp` runs at libkernel load: it parses the variable, reads `param.json`/`param.sfo` for the title ID and creates and locks `.anyps5-instance.lock` in the working directory. Failure there stops the process before `main`, and the PR itself measured one existing fixture (`download_data_mount_invalid`) changing behaviour under `on` because the title ID is now read earlier than before. Can the title-ID read and lock be deferred to the existing startup path instead, so a malformed `param.json` still fails where it does today? 3. **Scope of the lock.** The lock only protects two copies launched from the same working directory, which is the only thing the code can see; the body says restart handoff, self-started copies and network file systems are unmeasured and leans on six new debt entries for them. Please confirm each of those six is a gap in behaviour that exists (not documentation of the new feature's own limits), since the debt files are for missing PS5 behaviour.
MarcZX10 commented 2026-11-02 04:58:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T04:58:17Z.

Review: COMMENTED


Reviewed — macOS pipe2 point open at this head, startup point confirmed

oneandonlydean's macOS point stands at this head: pipe2 is still called outside a Windows-only guard. CnRJay's startup point is confirmed by the static initializer.

What I ran

Live head: 6dbd51412e1b6b8438bac601e17c93d13f5d942d (OPEN, same as the batch file). Read the comments and reviews with gh api. Read files with git -C /marc/AnyPS5 show 6dbd5141…:<path>, including InstanceLockHost.cpp, InstanceLockStartup.cpp, libkernel CMakeLists.txt, docs/dev/debt/libkernel.md and CONTRIBUTING.md. Read the hosted check rollup with gh pr view. Nothing was built or run.

Results

Check Result Note
windows, library-portability SUCCESS hosted, at this head; no macOS build of the new target was read
conventions SUCCESS hosted, at this head

Findings

  • oneandonlydean, 2026-11-01 15:50 UTC: pipe2 under #ifndef _WIN32 is called at InstanceLockHost.cpp L106-L113, and the target instance_lock_host_tests is registered at libkernel CMakeLists.txt L1613. The guard does not exclude Apple. I did not check the enclosing CMake condition or the Apple SDK, and I did not build on macOS.
  • CnRJay, 2026-11-01 19:49 UTC, point 2: confirmed. InstanceLockChecked runs in a static initializer (InstanceLockStartup.cpp L24-L39). It reads the title ID (L28-L34), acquires the lock (L35-L36) and throws (L32) before main. Whether the title read can move to the startup path is your decision.
  • Point 1: the header's correction says Unblocks: none is allowed; CONTRIBUTING.md L67 allows none. The "why libkernel" question is still open.
  • Point 3: the six new entries are at docs/dev/debt/libkernel.md L356-L361. They describe host-side lock behaviour (network file systems, Windows, macOS, startup, restart, default), not PS5 behaviour. Whether they belong in the debt file is for the reviewer to judge.

Sources

Not verified

  • The Apple libc and SDK behaviour for pipe2: not checked here.
  • The enclosing CMake condition around L1613: not read.
  • The PR body's claim that download_data_mount_invalid changed behaviour: not verified.
  • Nothing was built or run here.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5732254007 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12970#pullrequestreview-5732254007). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-02T04:58:17Z. **Review:** COMMENTED --- # Reviewed — macOS pipe2 point open at this head, startup point confirmed oneandonlydean's macOS point stands at this head: `pipe2` is still called outside a Windows-only guard. CnRJay's startup point is confirmed by the static initializer. ## What I ran Live head: 6dbd51412e1b6b8438bac601e17c93d13f5d942d (OPEN, same as the batch file). Read the comments and reviews with `gh api`. Read files with `git -C /marc/AnyPS5 show 6dbd5141…:<path>`, including InstanceLockHost.cpp, InstanceLockStartup.cpp, libkernel CMakeLists.txt, docs/dev/debt/libkernel.md and CONTRIBUTING.md. Read the hosted check rollup with `gh pr view`. Nothing was built or run. ## Results | Check | Result | Note | |---|---|---| | windows, library-portability | SUCCESS | hosted, at this head; no macOS build of the new target was read | | conventions | SUCCESS | hosted, at this head | ## Findings - oneandonlydean, 2026-11-01 15:50 UTC: `pipe2` under `#ifndef _WIN32` is called at InstanceLockHost.cpp L106-L113, and the target `instance_lock_host_tests` is registered at libkernel CMakeLists.txt L1613. The guard does not exclude Apple. I did not check the enclosing CMake condition or the Apple SDK, and I did not build on macOS. - CnRJay, 2026-11-01 19:49 UTC, point 2: confirmed. `InstanceLockChecked` runs in a static initializer (InstanceLockStartup.cpp L24-L39). It reads the title ID (L28-L34), acquires the lock (L35-L36) and throws (L32) before `main`. Whether the title read can move to the startup path is your decision. - Point 1: the header's correction says `Unblocks: none` is allowed; CONTRIBUTING.md L67 allows `none`. The "why libkernel" question is still open. - Point 3: the six new entries are at docs/dev/debt/libkernel.md L356-L361. They describe host-side lock behaviour (network file systems, Windows, macOS, startup, restart, default), not PS5 behaviour. Whether they belong in the debt file is for the reviewer to judge. ## Sources - https://github.com/boykopovar/AnyPS5/blob/6dbd51412e1b6b8438bac601e17c93d13f5d942d/core/libs/tests/InstanceLockHost.cpp#L106-L113 - https://github.com/boykopovar/AnyPS5/blob/6dbd51412e1b6b8438bac601e17c93d13f5d942d/core/libs/prx/libkernel/System/src/InstanceLockStartup.cpp#L24-L39 - https://github.com/boykopovar/AnyPS5/blob/6dbd51412e1b6b8438bac601e17c93d13f5d942d/core/libs/prx/libkernel/CMakeLists.txt#L1613-L1613 - https://github.com/boykopovar/AnyPS5/blob/6dbd51412e1b6b8438bac601e17c93d13f5d942d/docs/dev/debt/libkernel.md#L356-L361 - https://github.com/boykopovar/AnyPS5/blob/6dbd51412e1b6b8438bac601e17c93d13f5d942d/CONTRIBUTING.md#L67-L67 ## Not verified - The Apple libc and SDK behaviour for `pipe2`: not checked here. - The enclosing CMake condition around L1613: not read. - The PR body's claim that `download_data_mount_invalid` changed behaviour: not verified. - Nothing was built or run here.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12970
No description provided.