feat(libkernel): stop a second copy of a title with ANYPS5_INSTANCE_LOCK #12970
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12970"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.lockin its working directory (the directory that holdsapp0/). A second copy started from the same directory stops at start-up with an error naming the title ID, the directory, the lock file andANYPS5_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.cpplocks its own journal file (flock/LockFileEx); #12060 only makes the guest'sflockon 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
offchanges nothing (no file is opened or created). It is opt-in rather than on by default because, measured withANYPS5_INSTANCE_LOCK=onexported over the existing libkernel fixtures that carry a title (download_data_mount_*,param_json_parser,guest_app_info,kernel_error, run withctest -j8),download_data_mount_invalid(a deliberately malformedparam.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.InstanceLockPosix.cpp(openwithO_CLOEXEC,flock(LOCK_EX | LOCK_NB);execvinsceSystemServiceLoadExecreleases it for the new image) andInstanceLockWindows.cpp(CreateFileWwith read and write sharing, one-byte exclusiveLockFileExwithLOCKFILE_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 withoutparam.json/param.sfotakes none.ReleaseHostInstanceLockis called bysceSystemServiceLoadExecbeforeCreateProcessWon 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, sixdocs/dev/debt/libkernel.mdentries (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: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 byReleaseHostInstanceLock; 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.upstream/mainbb04a4f1ea) and branch with the variable unset:kernel_error,download_data_mount_{declared,none,invalid,missing,sfo},param_json_parser,guest_app_infopass on both.host_cpufails on both (this host's CPU, unrelated);guest_kernel_errorsis not built in either run.ON, unset or empty as on) failsinstance_lock; dropping the title ID, changing the lock path or the variable from the error failsinstance_lock; removingflock,O_CLOEXEC, the close on destruction, the EWOULDBLOCK handling or the open-failure throw failsinstance_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 matchinginstance_lock_startup_*cases. Not controlled: the whole Windows backend and the Windows LoadExec release.InstanceLockWindows.cpp, both test sources, the start-up file) compile withx86_64-w64-mingw32-g++ -fsyntax-only.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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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.
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
libkernelrather than in a launcher script. Right now "Unblocks" names nothing.Work in a static initializer.
InstanceLockStartup.cppruns at libkernel load: it parses the variable, readsparam.json/param.sfofor the title ID and creates and locks.anyps5-instance.lockin the working directory. Failure there stops the process beforemain, and the PR itself measured one existing fixture (download_data_mount_invalid) changing behaviour underonbecause 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 malformedparam.jsonstill fails where it does today?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.
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:
pipe2is 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 withgit -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 withgh pr view. Nothing was built or run.Results
Findings
pipe2under#ifndef _WIN32is called at InstanceLockHost.cpp L106-L113, and the targetinstance_lock_host_testsis 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.InstanceLockCheckedruns in a static initializer (InstanceLockStartup.cpp L24-L39). It reads the title ID (L28-L34), acquires the lock (L35-L36) and throws (L32) beforemain. Whether the title read can move to the startup path is your decision.Unblocks: noneis allowed; CONTRIBUTING.md L67 allowsnone. The "why libkernel" question is still open.Sources
github.com/boykopovar/AnyPS5@6dbd51412e/core/libs/tests/InstanceLockHost.cpp (L106-L113)github.com/boykopovar/AnyPS5@6dbd51412e/core/libs/prx/libkernel/System/src/InstanceLockStartup.cpp (L24-L39)github.com/boykopovar/AnyPS5@6dbd51412e/core/libs/prx/libkernel/CMakeLists.txt (L1613-L1613)github.com/boykopovar/AnyPS5@6dbd51412e/docs/dev/debt/libkernel.md (L356-L361)github.com/boykopovar/AnyPS5@6dbd51412e/CONTRIBUTING.md (L67-L67)Not verified
pipe2: not checked here.download_data_mount_invalidchanged behaviour: not verified.