mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-21 10:09:59 -04:00
Let one process work in a launcher folder at a time, and one job on the same thing.
The window already locked data/launcher.lock, but a second one closed without a word, the command line did not take the lock, and nothing kept two jobs apart. The lock is now launcher/locks.py: the window, every command but check and every job that changes files hold it, the one refused says who holds it, and a job names what it changes so that a second one on the same thing fails before its first step. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -432,6 +432,21 @@ game folder is the default `<launcher>\game`. The dump is never touched.
|
|||||||
`rebuild [--game D]`, `fonts`. `NX2navi.exe --desktop-shortcut on|off` and `--shortcuts on|off` are the stub's own.
|
`rebuild [--game D]`, `fonts`. `NX2navi.exe --desktop-shortcut on|off` and `--shortcuts on|off` are the stub's own.
|
||||||
`NX2navi-Setup.exe` takes `--dest DIR`, `--no-shortcuts`, `--no-launch` and `--quiet`.
|
`NX2navi-Setup.exe` takes `--dest DIR`, `--no-shortcuts`, `--no-launch` and `--quiet`.
|
||||||
|
|
||||||
|
## One launcher, one job
|
||||||
|
|
||||||
|
One process works in a launcher folder at a time (`launcher/locks.py`). The window holds the folder's lock,
|
||||||
|
`data/launcher.lock`, while it is open, every command of `launcher/cli.py` but `check` holds it while it runs, and a
|
||||||
|
job that changes files takes it too (the window's own jobs share the window's). The operating system keeps the lock
|
||||||
|
(`msvcrt.locking` on Windows, `flock` elsewhere) and lets go of it when the process ends, however it ends; who holds
|
||||||
|
it is written to `data/launcher.lock.owner`. A second window says who holds the folder in a message box and closes;
|
||||||
|
a command prints it and ends with exit code 1; a job fails with it before its first step.
|
||||||
|
|
||||||
|
Within one process, a job names what it changes (`jobs.Job.claims`: `game`, the game folder and the build tools;
|
||||||
|
`app`, the app folder's versions and the downloads), and a second job on the same thing fails at once with the first
|
||||||
|
one's name. An install, a rebuild and the rebuild of an update claim `game`; an update's download and the background
|
||||||
|
download claim `app`; the update check claims nothing and runs beside anything. Two launcher folders that share one
|
||||||
|
game folder are not kept apart: the lock belongs to the launcher folder.
|
||||||
|
|
||||||
## Releases
|
## Releases
|
||||||
|
|
||||||
1. Bump `VERSION`; build the shader translator (`tools/build_xlate.py`).
|
1. Bump `VERSION`; build the shader translator (`tools/build_xlate.py`).
|
||||||
@@ -511,7 +526,9 @@ folder, refusals), the shared save's zip (export of the save the game would load
|
|||||||
files, the import of a zip with its backup, a zip in the saves folder, the menu's waiting import dropped, the zips it
|
files, the import of a zip with its backup, a zip in the saves folder, the menu's waiting import dropped, the zips it
|
||||||
refuses: no manifest, another format or game, a newer format, paths outside the save, links, damage), the install
|
refuses: no manifest, another format or game, a newer format, paths outside the save, links, damage), the install
|
||||||
job with a fake
|
job with a fake
|
||||||
`make_game.py` (with and without the prompts row), and the window's selftest when a display is there.
|
`make_game.py` (with and without the prompts row), the folder's lock (a second launcher process refused and told who
|
||||||
|
holds it, the lock gone when its holder is killed, a job and the command line refused while another process holds
|
||||||
|
it, two jobs on one folder), and the window's selftest when a display is there.
|
||||||
|
|
||||||
What goes wrong around a build is tested the same way, on made-up folders and stand-in processes: an update that
|
What goes wrong around a build is tested the same way, on made-up folders and stand-in processes: an update that
|
||||||
stops in its compile (the game stays installed, home says how to finish, the install goes on from the unpacked
|
stops in its compile (the game stays installed, home says how to finish, the install goes on from the unpacked
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ if __name__ == '__main__' and not __package__:
|
|||||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
__package__ = 'launcher' # noqa: A001 — lets the relative imports below work when run as a script
|
__package__ = 'launcher' # noqa: A001 — lets the relative imports below work when run as a script
|
||||||
|
|
||||||
from . import build, gamefiles, jobs, versions # noqa: E402
|
from . import build, gamefiles, jobs, locks, versions # noqa: E402
|
||||||
from .context import Context # noqa: E402
|
from .context import Context # noqa: E402
|
||||||
from .paths import Layout # noqa: E402
|
from .paths import Layout # noqa: E402
|
||||||
|
|
||||||
@@ -65,6 +65,24 @@ def main(argv=None):
|
|||||||
p.add_argument('old')
|
p.add_argument('old')
|
||||||
a = ap.parse_args(argv)
|
a = ap.parse_args(argv)
|
||||||
ctx = Context(Layout(root=a.root) if a.root else None, echo=False)
|
ctx = Context(Layout(root=a.root) if a.root else None, echo=False)
|
||||||
|
if a.cmd == 'check': # reads the feed and changes nothing: it runs beside an open launcher
|
||||||
|
return run(a, ctx, None)
|
||||||
|
# Every other command changes the launcher's folder or the game's, as the window's jobs do: it holds the folder's
|
||||||
|
# lock (launcher/locks.py) from before it writes the settings until it ends, and is refused while the window or
|
||||||
|
# another command holds it
|
||||||
|
try:
|
||||||
|
held = locks.FileLock(ctx.layout.instance_lock, f'"{a.cmd}" from the command line', where=ctx.layout.root)
|
||||||
|
except locks.Busy as e:
|
||||||
|
print(f'error: {e}. Close it, or wait until it is done, and try again.', file=sys.stderr, flush=True)
|
||||||
|
return 1
|
||||||
|
try:
|
||||||
|
return run(a, ctx, held)
|
||||||
|
finally:
|
||||||
|
held.release()
|
||||||
|
|
||||||
|
|
||||||
|
def run(a, ctx, held):
|
||||||
|
"""One command. held: the launcher folder's lock, which `update` lets go of before the new version takes over."""
|
||||||
s = ctx.settings
|
s = ctx.settings
|
||||||
|
|
||||||
if a.cmd == 'check':
|
if a.cmd == 'check':
|
||||||
@@ -93,6 +111,7 @@ def main(argv=None):
|
|||||||
new = ctx.layout.apps / versions.display(check.release.version)
|
new = ctx.layout.apps / versions.display(check.release.version)
|
||||||
cli = new / 'launcher' / 'cli.py'
|
cli = new / 'launcher' / 'cli.py'
|
||||||
print(f'finishing in {versions.display(check.release.version)}', flush=True)
|
print(f'finishing in {versions.display(check.release.version)}', flush=True)
|
||||||
|
held.release() # the new version's command takes the lock itself
|
||||||
return subprocess.call([ctx.layout.python_for_builds(), str(cli), '--root', str(ctx.layout.root),
|
return subprocess.call([ctx.layout.python_for_builds(), str(cli), '--root', str(ctx.layout.root),
|
||||||
'continue-update', ctx.version])
|
'continue-update', ctx.version])
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import threading
|
|||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from . import build, feed, gamefiles, net, toolchain, updater, versions, winsys
|
from . import build, feed, gamefiles, locks, net, toolchain, updater, versions, winsys
|
||||||
|
|
||||||
RECHECK = 3600 # seconds between update checks while the launcher stays open, hidden during play included
|
RECHECK = 3600 # seconds between update checks while the launcher stays open, hidden during play included
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ PENDING, RUNNING, DONE, SKIPPED, FAILED = 'pending', 'running', 'done', 'skipped
|
|||||||
|
|
||||||
# Failures whose message says all there is to say: the log gets one line for them (failure_line), not a traceback.
|
# Failures whose message says all there is to say: the log gets one line for them (failure_line), not a traceback.
|
||||||
# A refused update check wrote 22 lines of two tracebacks into launcher.log each time.
|
# A refused update check wrote 22 lines of two tracebacks into launcher.log each time.
|
||||||
PLAIN_FAILURES = (feed.FeedError, build.BuildError)
|
PLAIN_FAILURES = (feed.FeedError, build.BuildError, locks.Busy)
|
||||||
|
|
||||||
|
|
||||||
def failure_line(e):
|
def failure_line(e):
|
||||||
@@ -102,6 +102,12 @@ class Step:
|
|||||||
class Job:
|
class Job:
|
||||||
kind = 'job'
|
kind = 'job'
|
||||||
title = ''
|
title = ''
|
||||||
|
# What the job changes while it runs, for the lock between jobs (launcher/locks.py claim): 'game' is the game
|
||||||
|
# folder and the build tools, 'app' the app folder's versions and the downloads. A job with claims also holds the
|
||||||
|
# launcher folder's lock between processes while it runs; one without (the update check) changes no file and
|
||||||
|
# runs beside anything.
|
||||||
|
claims = ()
|
||||||
|
task = '' # the job as the holder of a lock is named (doing)
|
||||||
|
|
||||||
def __init__(self, ctx, step_ids, ver=''):
|
def __init__(self, ctx, step_ids, ver=''):
|
||||||
self.ctx = ctx
|
self.ctx = ctx
|
||||||
@@ -229,8 +235,31 @@ class Job:
|
|||||||
self._main()
|
self._main()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def _main(self):
|
def doing(self):
|
||||||
|
"""The job in a few words ("the rebuild of the game"), for the one a lock refuses because of it."""
|
||||||
|
return self.task or f'a {self.kind} job'
|
||||||
|
|
||||||
|
def _hold(self):
|
||||||
|
"""Take what the job needs to itself before it runs (launcher/locks.py): the launcher folder against other
|
||||||
|
processes, then its claims against the other jobs of this one. locks.Busy, with nothing held, when either is
|
||||||
|
taken. Returns what _main lets go of when the job has ended."""
|
||||||
|
if not self.claims:
|
||||||
|
return None, None
|
||||||
|
layout = self.ctx.layout
|
||||||
try:
|
try:
|
||||||
|
file = locks.FileLock(layout.instance_lock, self.doing(), where=layout.root)
|
||||||
|
except locks.Busy as e:
|
||||||
|
raise locks.Busy(f'{e}. Close it, or wait until it is done, and try again') from None
|
||||||
|
try:
|
||||||
|
return file, locks.claim(layout.root, self.claims, self.doing())
|
||||||
|
except locks.Busy:
|
||||||
|
file.release()
|
||||||
|
raise
|
||||||
|
|
||||||
|
def _main(self):
|
||||||
|
file = claimed = None
|
||||||
|
try:
|
||||||
|
file, claimed = self._hold()
|
||||||
self.run()
|
self.run()
|
||||||
self.result = 'ok'
|
self.result = 'ok'
|
||||||
except net.Cancelled:
|
except net.Cancelled:
|
||||||
@@ -246,6 +275,9 @@ class Job:
|
|||||||
if s.state == RUNNING:
|
if s.state == RUNNING:
|
||||||
s.state = FAILED if self.result == 'failed' else PENDING
|
s.state = FAILED if self.result == 'failed' else PENDING
|
||||||
s.value = 'Stopped' if self.result == 'failed' else 'Cancelled'
|
s.value = 'Stopped' if self.result == 'failed' else 'Cancelled'
|
||||||
|
locks.release_claim(claimed)
|
||||||
|
if file:
|
||||||
|
file.release()
|
||||||
self.finished = time.time()
|
self.finished = time.time()
|
||||||
self._touch()
|
self._touch()
|
||||||
|
|
||||||
@@ -359,6 +391,8 @@ class Job:
|
|||||||
class InstallJob(Job):
|
class InstallJob(Job):
|
||||||
kind = 'install'
|
kind = 'install'
|
||||||
title = 'Installing'
|
title = 'Installing'
|
||||||
|
claims = ('game',)
|
||||||
|
task = 'the installation of the game'
|
||||||
|
|
||||||
def __init__(self, ctx, dump, game_dir):
|
def __init__(self, ctx, dump, game_dir):
|
||||||
super().__init__(ctx, ['tools', 'check', 'exefs', 'romfs', 'lift', 'compile', 'shaders', *prompt_rows(ctx),
|
super().__init__(ctx, ['tools', 'check', 'exefs', 'romfs', 'lift', 'compile', 'shaders', *prompt_rows(ctx),
|
||||||
@@ -392,6 +426,8 @@ class InstallJob(Job):
|
|||||||
class RebuildJob(Job):
|
class RebuildJob(Job):
|
||||||
kind = 'rebuild'
|
kind = 'rebuild'
|
||||||
title = 'Rebuilding'
|
title = 'Rebuilding'
|
||||||
|
claims = ('game',)
|
||||||
|
task = 'the rebuild of the game'
|
||||||
retune = True # Rebuild the Game: the game's code for this processor's level (build.rebuild_args)
|
retune = True # Rebuild the Game: the game's code for this processor's level (build.rebuild_args)
|
||||||
|
|
||||||
def __init__(self, ctx, game_dir, dump=''):
|
def __init__(self, ctx, game_dir, dump=''):
|
||||||
@@ -431,12 +467,14 @@ class ModsRebuildJob(RebuildJob):
|
|||||||
class UpdateDownloadJob(Job):
|
class UpdateDownloadJob(Job):
|
||||||
"""Before the restart: download, check and stage the release, then switch app/current.txt to it."""
|
"""Before the restart: download, check and stage the release, then switch app/current.txt to it."""
|
||||||
kind = 'update'
|
kind = 'update'
|
||||||
|
claims = ('app',)
|
||||||
|
|
||||||
def __init__(self, ctx, release):
|
def __init__(self, ctx, release):
|
||||||
ver = versions.display(release.version)
|
ver = versions.display(release.version)
|
||||||
super().__init__(ctx, ['download', 'verify', 'tools_update', 'lift', 'compile_changed', 'swap', 'shaders'], ver)
|
super().__init__(ctx, ['download', 'verify', 'tools_update', 'lift', 'compile_changed', 'swap', 'shaders'], ver)
|
||||||
self.release, self.ver = release, ver
|
self.release, self.ver = release, ver
|
||||||
self.title = f'Updating to {ver}'
|
self.title = f'Updating to {ver}'
|
||||||
|
self.task = f'the update to {ver}'
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
layout = self.ctx.layout
|
layout = self.ctx.layout
|
||||||
@@ -463,12 +501,14 @@ class ContinueUpdateJob(RebuildJob):
|
|||||||
"""After the restart, in the new version: bring the tools to its lock and rebuild the game."""
|
"""After the restart, in the new version: bring the tools to its lock and rebuild the game."""
|
||||||
retune = False # an update keeps the compiled game's level, so it compiles only what changed
|
retune = False # an update keeps the compiled game's level, so it compiles only what changed
|
||||||
kind = 'update'
|
kind = 'update'
|
||||||
|
claims = ('game', 'app') # the rebuild, then updater.prune in the app folder
|
||||||
|
|
||||||
def __init__(self, ctx, game_dir, old, dump=''):
|
def __init__(self, ctx, game_dir, old, dump=''):
|
||||||
Job.__init__(self, ctx, ['download', 'verify', 'tools_update', 'lift', 'compile_changed', 'swap', 'shaders',
|
Job.__init__(self, ctx, ['download', 'verify', 'tools_update', 'lift', 'compile_changed', 'swap', 'shaders',
|
||||||
*prompt_rows(ctx), 'scan'], ctx.layout.version)
|
*prompt_rows(ctx), 'scan'], ctx.layout.version)
|
||||||
self.game_dir, self.dump, self.old = str(game_dir), dump, old
|
self.game_dir, self.dump, self.old = str(game_dir), dump, old
|
||||||
self.title = f'Updating to {versions.display(ctx.layout.version)}'
|
self.title = f'Updating to {versions.display(ctx.layout.version)}'
|
||||||
|
self.task = f'the update to {versions.display(ctx.layout.version)}'
|
||||||
for sid, value in (('download', 'Done'), ('verify', 'Checksum OK')):
|
for sid, value in (('download', 'Done'), ('verify', 'Checksum OK')):
|
||||||
self.step(sid).state, self.step(sid).value, self.step(sid).fraction = DONE, value, 1.0
|
self.step(sid).state, self.step(sid).value, self.step(sid).fraction = DONE, value, 1.0
|
||||||
|
|
||||||
@@ -507,11 +547,13 @@ class StageJob(Job):
|
|||||||
(updater.stage), while the launcher runs and the game too. Its thread runs at background priority, so the game
|
(updater.stage), while the launcher runs and the game too. Its thread runs at background priority, so the game
|
||||||
shares only the network with it. No screen shows it: the home screen's Updates row reads it."""
|
shares only the network with it. No screen shows it: the home screen's Updates row reads it."""
|
||||||
kind = 'stage'
|
kind = 'stage'
|
||||||
|
claims = ('app',)
|
||||||
|
|
||||||
def __init__(self, ctx, release):
|
def __init__(self, ctx, release):
|
||||||
ver = versions.display(release.version)
|
ver = versions.display(release.version)
|
||||||
super().__init__(ctx, ['download'], ver)
|
super().__init__(ctx, ['download'], ver)
|
||||||
self.release, self.ver = release, ver
|
self.release, self.ver = release, ver
|
||||||
|
self.task = f'the background download of {ver}'
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
winsys.background_thread()
|
winsys.background_thread()
|
||||||
|
|||||||
134
launcher/locks.py
Normal file
134
launcher/locks.py
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
"""Who may work in a launcher folder: one process at a time, and in that process one job at a time on the same thing.
|
||||||
|
|
||||||
|
Two launchers on one folder each ran their own build into the same game folder, wrote the same settings and unpacked
|
||||||
|
an update over each other. So:
|
||||||
|
|
||||||
|
- FileLock is a lock between processes: a file locked by the operating system (msvcrt.locking on Windows, flock
|
||||||
|
elsewhere), which lets go of it when the process ends, however it ends. The window takes the one of its folder
|
||||||
|
(paths.Layout.instance_lock) for as long as it is open, launcher/cli.py for a command, and every job that changes
|
||||||
|
files while it runs (jobs.Job). A process that holds it already shares it with its own jobs. Who holds it is
|
||||||
|
written beside it (<lock>.owner), so that the one refused can say so.
|
||||||
|
- claim() is the lock between the jobs of one process: a job names what it changes (jobs.Job.claims: the game folder
|
||||||
|
and its build tools, the app folder), and a second job on the same thing is refused with the first one's name.
|
||||||
|
|
||||||
|
Both refuse with Busy, whose text names the holder; neither waits (FileLock for a few seconds when asked to, for a
|
||||||
|
launcher that is closing for its restart).
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
|
_guard = threading.Lock()
|
||||||
|
_files = {} # lock file path -> [the open file, how many FileLocks of this process share it]
|
||||||
|
_claims = {} # (folder, what is claimed) -> who claimed it
|
||||||
|
|
||||||
|
|
||||||
|
class Busy(Exception):
|
||||||
|
"""Another process or job holds what was asked for. Its text says who."""
|
||||||
|
|
||||||
|
|
||||||
|
def _key(path):
|
||||||
|
return os.path.normcase(os.path.abspath(str(path)))
|
||||||
|
|
||||||
|
|
||||||
|
def _os_lock(f):
|
||||||
|
"""Lock the open file f for this process without waiting. OSError when another open file holds the lock."""
|
||||||
|
if os.name == 'nt':
|
||||||
|
import msvcrt
|
||||||
|
f.seek(0)
|
||||||
|
msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1)
|
||||||
|
else:
|
||||||
|
import fcntl
|
||||||
|
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||||
|
|
||||||
|
|
||||||
|
def owner(path):
|
||||||
|
"""Who holds the lock file path, as its holder wrote it ("the launcher window (process 1234, since 14:02)"), or
|
||||||
|
'another NX2navi' when that cannot be read. Only worth asking after the lock was refused: the note of a holder
|
||||||
|
that has ended stays."""
|
||||||
|
try:
|
||||||
|
with open(str(path) + '.owner', encoding='utf-8') as f:
|
||||||
|
o = json.load(f)
|
||||||
|
return f'{o["what"]} (process {int(o["pid"])}, since {time.strftime("%H:%M", time.localtime(o["since"]))})'
|
||||||
|
except (OSError, ValueError, KeyError, TypeError, OverflowError):
|
||||||
|
return 'another NX2navi'
|
||||||
|
|
||||||
|
|
||||||
|
class FileLock:
|
||||||
|
"""The lock file path held for this process until release() or the process's end. what says who holds it, for
|
||||||
|
the one that is refused. Raises Busy ("<the holder> is using <where>"; where is the lock's folder unless given)
|
||||||
|
when another process still holds it after wait seconds. A lock file that cannot be made or opened (a folder
|
||||||
|
that cannot be written) locks nothing and refuses nobody: the launcher must still start there."""
|
||||||
|
|
||||||
|
def __init__(self, path, what, wait=0.0, where=None):
|
||||||
|
self.path, self.key = path, _key(path)
|
||||||
|
deadline = time.time() + wait
|
||||||
|
while True:
|
||||||
|
with _guard:
|
||||||
|
if self._take(what):
|
||||||
|
return
|
||||||
|
if time.time() >= deadline:
|
||||||
|
raise Busy(f'{owner(path)} is using {where or os.path.dirname(os.path.abspath(str(path)))}')
|
||||||
|
time.sleep(0.25)
|
||||||
|
|
||||||
|
def _take(self, what):
|
||||||
|
"""True when this process holds the file now (or cannot lock at all), False when another process does."""
|
||||||
|
entry = _files.get(self.key)
|
||||||
|
if entry:
|
||||||
|
entry[1] += 1
|
||||||
|
return True
|
||||||
|
try:
|
||||||
|
f = open(self.path, 'a+')
|
||||||
|
except OSError:
|
||||||
|
self.key = None
|
||||||
|
return True
|
||||||
|
try:
|
||||||
|
_os_lock(f)
|
||||||
|
except OSError:
|
||||||
|
f.close()
|
||||||
|
return False
|
||||||
|
_files[self.key] = [f, 1]
|
||||||
|
try:
|
||||||
|
with open(str(self.path) + '.owner', 'w', encoding='utf-8') as o:
|
||||||
|
json.dump({'what': what, 'pid': os.getpid(), 'since': time.time()}, o)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
return True
|
||||||
|
|
||||||
|
def release(self):
|
||||||
|
"""Let go of the lock (the file is closed when the last FileLock of this process on it lets go). Calling it
|
||||||
|
again does nothing."""
|
||||||
|
with _guard:
|
||||||
|
key, self.key = self.key, None
|
||||||
|
entry = _files.get(key) if key else None
|
||||||
|
if entry:
|
||||||
|
entry[1] -= 1
|
||||||
|
if entry[1] <= 0:
|
||||||
|
del _files[key]
|
||||||
|
entry[0].close() # closing the file releases the operating system's lock
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *exc):
|
||||||
|
self.release()
|
||||||
|
|
||||||
|
|
||||||
|
def claim(folder, names, who):
|
||||||
|
"""Claim each of names in folder for who (a job's description), all or none. Raises Busy with the holder of the
|
||||||
|
first one that is taken. Returns what release_claim() takes."""
|
||||||
|
keys = [(_key(folder), n) for n in names]
|
||||||
|
with _guard:
|
||||||
|
for k in keys:
|
||||||
|
if k in _claims:
|
||||||
|
raise Busy(f'{_claims[k]} is still running: wait for it to end, or cancel it')
|
||||||
|
for k in keys:
|
||||||
|
_claims[k] = who
|
||||||
|
return keys
|
||||||
|
|
||||||
|
|
||||||
|
def release_claim(keys):
|
||||||
|
with _guard:
|
||||||
|
for k in keys or ():
|
||||||
|
_claims.pop(k, None)
|
||||||
@@ -72,6 +72,9 @@ class Layout:
|
|||||||
self.cache = self.data / 'cache'
|
self.cache = self.data / 'cache'
|
||||||
self.downloads = self.data / 'downloads'
|
self.downloads = self.data / 'downloads'
|
||||||
self.settings = self.data / 'launcher.json'
|
self.settings = self.data / 'launcher.json'
|
||||||
|
# held by whichever process works in this folder: the window while it is open, launcher/cli.py for a command
|
||||||
|
# and every job that changes files (launcher/locks.py)
|
||||||
|
self.instance_lock = self.data / 'launcher.lock'
|
||||||
tool_root = self.root
|
tool_root = self.root
|
||||||
if not ansi_safe(tool_root):
|
if not ansi_safe(tool_root):
|
||||||
tool_root = Path(os.environ.get('SystemDrive', 'C:') + '\\') / 'NX2navi'
|
tool_root = Path(os.environ.get('SystemDrive', 'C:') + '\\') / 'NX2navi'
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ import tkinter.font as tkfont
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from tkinter import filedialog
|
from tkinter import filedialog
|
||||||
|
|
||||||
from .. import NAME, build, feed, gameini, gamefiles, jobs, mods, saves, steam, versions, winsys
|
from .. import NAME, build, feed, gameini, gamefiles, jobs, locks, mods, saves, steam, versions, winsys
|
||||||
from ..context import Context
|
from ..context import Context
|
||||||
from . import art, picture, sound
|
from . import art, picture, sound
|
||||||
|
|
||||||
@@ -2673,28 +2673,26 @@ def parse_args(argv):
|
|||||||
|
|
||||||
|
|
||||||
def single_instance(layout, wait):
|
def single_instance(layout, wait):
|
||||||
"""An exclusive lock on data/launcher.lock for this process's life, or None when another launcher holds it after
|
"""The launcher folder's lock (launcher/locks.py) for this window's life. locks.Busy, naming the holder, when
|
||||||
wait seconds (the one an update restart replaces may still be closing)."""
|
another launcher or a command of launcher/cli.py still holds it after wait seconds (the launcher an update
|
||||||
path = layout.data / 'launcher.lock'
|
restart replaces may still be closing)."""
|
||||||
deadline = time.time() + wait
|
return locks.FileLock(layout.instance_lock, 'the launcher window', wait=wait, where=layout.root)
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
f = open(path, 'a+')
|
def tell(text):
|
||||||
|
"""A sentence for the player before the window is up: a message box that waits for OK."""
|
||||||
try:
|
try:
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
import msvcrt
|
import ctypes
|
||||||
msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1)
|
ctypes.windll.user32.MessageBoxW(None, text, NAME, 0x40)
|
||||||
else:
|
else:
|
||||||
import fcntl
|
from tkinter import messagebox
|
||||||
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
root = tk.Tk()
|
||||||
return f
|
root.withdraw()
|
||||||
except OSError:
|
messagebox.showinfo(NAME, text, parent=root)
|
||||||
f.close()
|
root.destroy()
|
||||||
raise
|
except Exception: # no display, no toolkit: the terminal that started it
|
||||||
except OSError:
|
print(text, file=sys.stderr)
|
||||||
if time.time() >= deadline:
|
|
||||||
return None
|
|
||||||
time.sleep(0.25)
|
|
||||||
|
|
||||||
|
|
||||||
def play_only(ctx):
|
def play_only(ctx):
|
||||||
@@ -2743,14 +2741,23 @@ def main(argv=None):
|
|||||||
return code
|
return code
|
||||||
lock = None
|
lock = None
|
||||||
if not a.selftest:
|
if not a.selftest:
|
||||||
|
try:
|
||||||
lock = single_instance(ctx.layout, 20 if (a.continue_update or a.rolled_back) else 2)
|
lock = single_instance(ctx.layout, 20 if (a.continue_update or a.rolled_back) else 2)
|
||||||
if lock is None:
|
except locks.Busy as e:
|
||||||
ctx.log('another launcher is running')
|
# A second window on the same folder would run its own jobs into the same game folder and settings. The
|
||||||
|
# first one may be out of sight (hidden while the game runs), so the player is told who holds the folder
|
||||||
|
ctx.log(f'not started: {e}')
|
||||||
|
tell(f'NX2navi is already running: {e}.\n\nIts window may be hidden while the game runs; it comes back '
|
||||||
|
'when you quit the game.')
|
||||||
return 0
|
return 0
|
||||||
|
try:
|
||||||
root = tk.Tk(className='NX2navi')
|
root = tk.Tk(className='NX2navi')
|
||||||
app = App(root, ctx, a)
|
app = App(root, ctx, a)
|
||||||
if a.selftest:
|
if a.selftest:
|
||||||
from .selftest import run
|
from .selftest import run
|
||||||
root.after(300, lambda: run(app, a.shots))
|
root.after(300, lambda: run(app, a.shots))
|
||||||
root.mainloop()
|
root.mainloop()
|
||||||
|
finally:
|
||||||
|
if lock:
|
||||||
|
lock.release()
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ ROOT = Path(__file__).resolve().parent.parent
|
|||||||
sys.path.insert(0, str(ROOT))
|
sys.path.insert(0, str(ROOT))
|
||||||
sys.path.insert(0, str(ROOT / 'tools'))
|
sys.path.insert(0, str(ROOT / 'tools'))
|
||||||
|
|
||||||
from launcher import build, feed, gameini, gamefiles, jobs, net, saves, settings, toolchain, updater, versions, winsys # noqa: E402
|
from launcher import build, feed, gameini, gamefiles, jobs, locks, net, saves, settings, toolchain, updater, versions, winsys # noqa: E402
|
||||||
from launcher.context import Context # noqa: E402
|
from launcher.context import Context # noqa: E402
|
||||||
from launcher.paths import Layout # noqa: E402
|
from launcher.paths import Layout # noqa: E402
|
||||||
|
|
||||||
@@ -3681,6 +3681,167 @@ class JobTest(Tmp):
|
|||||||
self.assertEqual(jobs.InstallJob(ctx, self.tmp / 'g.nup', game).args()[0], '--nup')
|
self.assertEqual(jobs.InstallJob(ctx, self.tmp / 'g.nup', game).args()[0], '--nup')
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- one launcher, one job
|
||||||
|
|
||||||
|
# Another launcher process: it takes the lock file argv[2] as argv[3], says so and holds it until its input closes.
|
||||||
|
HOLDER = ('import sys; sys.path.insert(0, sys.argv[1]); from launcher import locks; '
|
||||||
|
'lock = locks.FileLock(sys.argv[2], sys.argv[3]); print("held", flush=True); sys.stdin.read()')
|
||||||
|
|
||||||
|
|
||||||
|
class LockTest(Tmp):
|
||||||
|
"""launcher/locks.py: one process works in a launcher folder at a time, and in it one job on the same thing."""
|
||||||
|
|
||||||
|
make_ctx, patch_tools = JobTest.make_ctx, JobTest.patch_tools
|
||||||
|
|
||||||
|
def holder(self, layout, what='the launcher window'):
|
||||||
|
"""A second process holding layout's lock as what, until the test ends or it is killed."""
|
||||||
|
p = subprocess.Popen([sys.executable, '-c', HOLDER, str(ROOT), str(layout.instance_lock), what],
|
||||||
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
|
||||||
|
|
||||||
|
def end():
|
||||||
|
p.kill()
|
||||||
|
p.wait()
|
||||||
|
p.stdin.close()
|
||||||
|
p.stdout.close()
|
||||||
|
self.addCleanup(end)
|
||||||
|
self.assertEqual(p.stdout.readline().strip(), 'held')
|
||||||
|
return p
|
||||||
|
|
||||||
|
def test_a_second_launcher_is_refused_and_told_who_holds_the_folder(self):
|
||||||
|
other = self.holder(self.layout)
|
||||||
|
with self.assertRaises(locks.Busy) as refused:
|
||||||
|
locks.FileLock(self.layout.instance_lock, 'the launcher window', where=self.layout.root)
|
||||||
|
self.assertIn(f'the launcher window (process {other.pid}, since ', str(refused.exception))
|
||||||
|
self.assertIn(str(self.layout.root), str(refused.exception))
|
||||||
|
try:
|
||||||
|
from launcher.ui import app
|
||||||
|
except ImportError as e:
|
||||||
|
self.skipTest(f'no window toolkit: {e}')
|
||||||
|
said = []
|
||||||
|
with mock.patch.object(app, 'tell', said.append), mock.patch.object(app.tk, 'Tk') as window, \
|
||||||
|
mock.patch.object(app.winsys, 'dpi_aware'), mock.patch.object(app.winsys, 'app_id'), \
|
||||||
|
mock.patch.object(app, 'single_instance', lambda layout, wait: locks.FileLock(
|
||||||
|
layout.instance_lock, 'the launcher window', where=layout.root)): # without the wait
|
||||||
|
self.assertEqual(app.main(['--root', str(self.layout.root)]), 0)
|
||||||
|
window.assert_not_called() # no second window
|
||||||
|
self.assertEqual(len(said), 1)
|
||||||
|
self.assertIn(f'NX2navi is already running: the launcher window (process {other.pid}', said[0])
|
||||||
|
|
||||||
|
def test_the_lock_goes_when_its_process_dies(self):
|
||||||
|
other = self.holder(self.layout)
|
||||||
|
other.kill() # no clean exit: the operating system lets go of the lock
|
||||||
|
other.wait()
|
||||||
|
mine = locks.FileLock(self.layout.instance_lock, 'the launcher window', wait=10)
|
||||||
|
self.addCleanup(mine.release)
|
||||||
|
# the window's own jobs share its lock, and it stays the window's until the window lets go of it
|
||||||
|
job = locks.FileLock(self.layout.instance_lock, 'a job')
|
||||||
|
job.release()
|
||||||
|
job.release() # letting go twice lets go once
|
||||||
|
self.assertIn(locks._key(self.layout.instance_lock), locks._files)
|
||||||
|
mine.release()
|
||||||
|
self.assertNotIn(locks._key(self.layout.instance_lock), locks._files)
|
||||||
|
|
||||||
|
def test_a_folder_that_cannot_hold_a_lock_file_refuses_nobody(self):
|
||||||
|
gone = self.tmp / 'no such folder' / 'launcher.lock'
|
||||||
|
with locks.FileLock(gone, 'the launcher window'), locks.FileLock(gone, 'another'):
|
||||||
|
self.assertFalse(gone.exists())
|
||||||
|
|
||||||
|
def test_a_job_does_not_run_while_another_process_holds_the_folder(self):
|
||||||
|
self.patch_tools()
|
||||||
|
ctx = self.make_ctx('ok')
|
||||||
|
self.addCleanup(ctx._file.close)
|
||||||
|
other = self.holder(ctx.layout, '"rebuild" from the command line')
|
||||||
|
j = jobs.InstallJob(ctx, self.tmp / 'g.nup', self.tmp / 'game').run_inline()
|
||||||
|
self.assertEqual(j.result, 'failed')
|
||||||
|
self.assertIn(f'"rebuild" from the command line (process {other.pid}', j.error)
|
||||||
|
self.assertIn('try again', j.error)
|
||||||
|
self.assertEqual({s.state for s in j.steps}, {jobs.PENDING}) # nothing of it ran
|
||||||
|
self.assertFalse(any('working on' in ln for ln in j.lines))
|
||||||
|
check = jobs.Job(ctx, []) # a job that changes no file (the update check) runs beside it
|
||||||
|
check.run = lambda: None
|
||||||
|
self.assertEqual(check.run_inline().result, 'ok')
|
||||||
|
other.stdin.close() # the other process ends
|
||||||
|
other.wait()
|
||||||
|
j = jobs.InstallJob(ctx, self.tmp / 'g.nup', self.tmp / 'game').run_inline()
|
||||||
|
self.assertEqual(j.result, 'ok', j.error)
|
||||||
|
self.assertEqual(locks._files, {}) # and the job let go of the folder when it ended
|
||||||
|
|
||||||
|
def test_two_jobs_on_one_folder(self):
|
||||||
|
self.patch_tools()
|
||||||
|
ctx = self.make_ctx('ok')
|
||||||
|
self.addCleanup(ctx._file.close)
|
||||||
|
started, go = threading.Event(), threading.Event()
|
||||||
|
|
||||||
|
class Slow(jobs.RebuildJob):
|
||||||
|
def run(self):
|
||||||
|
started.set()
|
||||||
|
go.wait(30)
|
||||||
|
|
||||||
|
first = Slow(ctx, self.tmp / 'game').start()
|
||||||
|
self.addCleanup(go.set)
|
||||||
|
self.assertTrue(started.wait(30))
|
||||||
|
second = jobs.RebuildJob(ctx, self.tmp / 'game').run_inline()
|
||||||
|
self.assertEqual(second.result, 'failed')
|
||||||
|
self.assertEqual(second.error, 'the rebuild of the game is still running: wait for it to end, or cancel it')
|
||||||
|
self.assertEqual({s.state for s in second.steps}, {jobs.PENDING})
|
||||||
|
install = jobs.InstallJob(ctx, self.tmp / 'g.nup', self.tmp / 'game').run_inline()
|
||||||
|
self.assertEqual(install.result, 'failed') # another kind of job on the same folder
|
||||||
|
self.assertIn('the rebuild of the game is still running', install.error)
|
||||||
|
|
||||||
|
class Download(jobs.Job): # what the background download claims: the app folder, not the game
|
||||||
|
claims = ('app',)
|
||||||
|
task = 'the background download of 9.9.9'
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.assertEqual(Download(ctx, []).run_inline().result, 'ok')
|
||||||
|
elsewhere = Context(Layout(root=self.tmp / 'other root', app=ctx.layout.app))
|
||||||
|
self.addCleanup(elsewhere._file.close)
|
||||||
|
self.assertEqual(jobs.InstallJob(elsewhere, self.tmp / 'g.nup', self.tmp / 'game 2').run_inline().result, 'ok')
|
||||||
|
go.set()
|
||||||
|
for _ in range(300):
|
||||||
|
if first.result is not None:
|
||||||
|
break
|
||||||
|
time.sleep(0.1)
|
||||||
|
self.assertEqual(first.result, 'ok')
|
||||||
|
again = jobs.InstallJob(ctx, self.tmp / 'g.nup', self.tmp / 'game').run_inline()
|
||||||
|
self.assertEqual(again.result, 'ok', again.error) # the first job let go when it ended
|
||||||
|
self.assertEqual((locks._files, locks._claims), ({}, {}))
|
||||||
|
|
||||||
|
def test_a_failed_job_lets_go_of_the_folder(self):
|
||||||
|
self.patch_tools()
|
||||||
|
ctx = self.make_ctx('--fail')
|
||||||
|
self.addCleanup(ctx._file.close)
|
||||||
|
self.assertEqual(jobs.InstallJob(ctx, self.tmp / 'g.nup', self.tmp / 'game').run_inline().result, 'failed')
|
||||||
|
self.assertEqual((locks._files, locks._claims), ({}, {}))
|
||||||
|
|
||||||
|
def test_the_command_line_takes_the_folders_lock(self):
|
||||||
|
from launcher import cli
|
||||||
|
ctx = self.make_ctx('ok')
|
||||||
|
ctx._file.close()
|
||||||
|
root = ['--root', str(ctx.layout.root)]
|
||||||
|
ran = []
|
||||||
|
|
||||||
|
def run(a, ctx, held):
|
||||||
|
ran.append((a.cmd, held is not None and locks._key(ctx.layout.instance_lock) in locks._files))
|
||||||
|
ctx._file.close()
|
||||||
|
return 0
|
||||||
|
err = io.StringIO()
|
||||||
|
with mock.patch.object(cli, 'run', run), contextlib.redirect_stderr(err):
|
||||||
|
self.assertEqual(cli.main(root + ['tools']), 0)
|
||||||
|
self.assertEqual(ran, [('tools', True)]) # held while the command runs
|
||||||
|
self.assertEqual(locks._files, {}) # and let go of after
|
||||||
|
other = self.holder(ctx.layout)
|
||||||
|
self.assertEqual(cli.main(root + ['rebuild']), 1)
|
||||||
|
self.assertEqual(cli.main(root + ['install', '--dump', 'd.nup', '--game', str(self.tmp / 'game')]), 1)
|
||||||
|
self.assertEqual(len(ran), 1) # neither ran, and no setting was written
|
||||||
|
self.assertEqual(cli.main(root + ['check']), 0) # the update check changes nothing: it runs
|
||||||
|
self.assertEqual(ran[-1], ('check', False))
|
||||||
|
self.assertIn(f'error: the launcher window (process {other.pid}', err.getvalue())
|
||||||
|
self.assertIn('try again', err.getvalue())
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- the icon
|
# ---------------------------------------------------------------- the icon
|
||||||
|
|
||||||
class SavesTest(unittest.TestCase):
|
class SavesTest(unittest.TestCase):
|
||||||
|
|||||||
Reference in New Issue
Block a user