mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-21 01:29:57 -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:
@@ -21,7 +21,7 @@ if __name__ == '__main__' and not __package__:
|
||||
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
|
||||
|
||||
from . import build, gamefiles, jobs, versions # noqa: E402
|
||||
from . import build, gamefiles, jobs, locks, versions # noqa: E402
|
||||
from .context import Context # noqa: E402
|
||||
from .paths import Layout # noqa: E402
|
||||
|
||||
@@ -65,6 +65,24 @@ def main(argv=None):
|
||||
p.add_argument('old')
|
||||
a = ap.parse_args(argv)
|
||||
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
|
||||
|
||||
if a.cmd == 'check':
|
||||
@@ -93,6 +111,7 @@ def main(argv=None):
|
||||
new = ctx.layout.apps / versions.display(check.release.version)
|
||||
cli = new / 'launcher' / 'cli.py'
|
||||
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),
|
||||
'continue-update', ctx.version])
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import threading
|
||||
import time
|
||||
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
|
||||
|
||||
@@ -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.
|
||||
# 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):
|
||||
@@ -102,6 +102,12 @@ class Step:
|
||||
class Job:
|
||||
kind = 'job'
|
||||
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=''):
|
||||
self.ctx = ctx
|
||||
@@ -229,8 +235,31 @@ class Job:
|
||||
self._main()
|
||||
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:
|
||||
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.result = 'ok'
|
||||
except net.Cancelled:
|
||||
@@ -246,6 +275,9 @@ class Job:
|
||||
if s.state == RUNNING:
|
||||
s.state = FAILED if self.result == 'failed' else PENDING
|
||||
s.value = 'Stopped' if self.result == 'failed' else 'Cancelled'
|
||||
locks.release_claim(claimed)
|
||||
if file:
|
||||
file.release()
|
||||
self.finished = time.time()
|
||||
self._touch()
|
||||
|
||||
@@ -359,6 +391,8 @@ class Job:
|
||||
class InstallJob(Job):
|
||||
kind = 'install'
|
||||
title = 'Installing'
|
||||
claims = ('game',)
|
||||
task = 'the installation of the game'
|
||||
|
||||
def __init__(self, ctx, dump, game_dir):
|
||||
super().__init__(ctx, ['tools', 'check', 'exefs', 'romfs', 'lift', 'compile', 'shaders', *prompt_rows(ctx),
|
||||
@@ -392,6 +426,8 @@ class InstallJob(Job):
|
||||
class RebuildJob(Job):
|
||||
kind = 'rebuild'
|
||||
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)
|
||||
|
||||
def __init__(self, ctx, game_dir, dump=''):
|
||||
@@ -431,12 +467,14 @@ class ModsRebuildJob(RebuildJob):
|
||||
class UpdateDownloadJob(Job):
|
||||
"""Before the restart: download, check and stage the release, then switch app/current.txt to it."""
|
||||
kind = 'update'
|
||||
claims = ('app',)
|
||||
|
||||
def __init__(self, ctx, release):
|
||||
ver = versions.display(release.version)
|
||||
super().__init__(ctx, ['download', 'verify', 'tools_update', 'lift', 'compile_changed', 'swap', 'shaders'], ver)
|
||||
self.release, self.ver = release, ver
|
||||
self.title = f'Updating to {ver}'
|
||||
self.task = f'the update to {ver}'
|
||||
|
||||
def run(self):
|
||||
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."""
|
||||
retune = False # an update keeps the compiled game's level, so it compiles only what changed
|
||||
kind = 'update'
|
||||
claims = ('game', 'app') # the rebuild, then updater.prune in the app folder
|
||||
|
||||
def __init__(self, ctx, game_dir, old, dump=''):
|
||||
Job.__init__(self, ctx, ['download', 'verify', 'tools_update', 'lift', 'compile_changed', 'swap', 'shaders',
|
||||
*prompt_rows(ctx), 'scan'], ctx.layout.version)
|
||||
self.game_dir, self.dump, self.old = str(game_dir), dump, old
|
||||
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')):
|
||||
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
|
||||
shares only the network with it. No screen shows it: the home screen's Updates row reads it."""
|
||||
kind = 'stage'
|
||||
claims = ('app',)
|
||||
|
||||
def __init__(self, ctx, release):
|
||||
ver = versions.display(release.version)
|
||||
super().__init__(ctx, ['download'], ver)
|
||||
self.release, self.ver = release, ver
|
||||
self.task = f'the background download of {ver}'
|
||||
|
||||
def run(self):
|
||||
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.downloads = self.data / 'downloads'
|
||||
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
|
||||
if not ansi_safe(tool_root):
|
||||
tool_root = Path(os.environ.get('SystemDrive', 'C:') + '\\') / 'NX2navi'
|
||||
|
||||
@@ -37,7 +37,7 @@ import tkinter.font as tkfont
|
||||
from pathlib import Path
|
||||
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 . import art, picture, sound
|
||||
|
||||
@@ -2673,28 +2673,26 @@ def parse_args(argv):
|
||||
|
||||
|
||||
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
|
||||
wait seconds (the one an update restart replaces may still be closing)."""
|
||||
path = layout.data / 'launcher.lock'
|
||||
deadline = time.time() + wait
|
||||
while True:
|
||||
try:
|
||||
f = open(path, 'a+')
|
||||
try:
|
||||
if os.name == 'nt':
|
||||
import msvcrt
|
||||
msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1)
|
||||
else:
|
||||
import fcntl
|
||||
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
return f
|
||||
except OSError:
|
||||
f.close()
|
||||
raise
|
||||
except OSError:
|
||||
if time.time() >= deadline:
|
||||
return None
|
||||
time.sleep(0.25)
|
||||
"""The launcher folder's lock (launcher/locks.py) for this window's life. locks.Busy, naming the holder, when
|
||||
another launcher or a command of launcher/cli.py still holds it after wait seconds (the launcher an update
|
||||
restart replaces may still be closing)."""
|
||||
return locks.FileLock(layout.instance_lock, 'the launcher window', wait=wait, where=layout.root)
|
||||
|
||||
|
||||
def tell(text):
|
||||
"""A sentence for the player before the window is up: a message box that waits for OK."""
|
||||
try:
|
||||
if os.name == 'nt':
|
||||
import ctypes
|
||||
ctypes.windll.user32.MessageBoxW(None, text, NAME, 0x40)
|
||||
else:
|
||||
from tkinter import messagebox
|
||||
root = tk.Tk()
|
||||
root.withdraw()
|
||||
messagebox.showinfo(NAME, text, parent=root)
|
||||
root.destroy()
|
||||
except Exception: # no display, no toolkit: the terminal that started it
|
||||
print(text, file=sys.stderr)
|
||||
|
||||
|
||||
def play_only(ctx):
|
||||
@@ -2743,14 +2741,23 @@ def main(argv=None):
|
||||
return code
|
||||
lock = None
|
||||
if not a.selftest:
|
||||
lock = single_instance(ctx.layout, 20 if (a.continue_update or a.rolled_back) else 2)
|
||||
if lock is None:
|
||||
ctx.log('another launcher is running')
|
||||
try:
|
||||
lock = single_instance(ctx.layout, 20 if (a.continue_update or a.rolled_back) else 2)
|
||||
except locks.Busy as e:
|
||||
# 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
|
||||
root = tk.Tk(className='NX2navi')
|
||||
app = App(root, ctx, a)
|
||||
if a.selftest:
|
||||
from .selftest import run
|
||||
root.after(300, lambda: run(app, a.shots))
|
||||
root.mainloop()
|
||||
try:
|
||||
root = tk.Tk(className='NX2navi')
|
||||
app = App(root, ctx, a)
|
||||
if a.selftest:
|
||||
from .selftest import run
|
||||
root.after(300, lambda: run(app, a.shots))
|
||||
root.mainloop()
|
||||
finally:
|
||||
if lock:
|
||||
lock.release()
|
||||
return 0
|
||||
|
||||
Reference in New Issue
Block a user