mirror of
https://github.com/yusufipk/dikte.git
synced 2026-09-11 10:56:10 +00:00
Let a second copy yield to the instance already running
listen() was the whole of the single-instance check and it cannot be one: a Windows named pipe takes a second server on the same name rather than refusing it, and everywhere else removeServer() first takes the live socket away from the instance holding it. Starting Dikte over a running Dikte then left two whole copies up, two tray icons and all, and the newer one's sweep() killed the whisper the older one was answering dictations with. On a machine that sleeps instead of logging out, that is one Start Menu click away, and it cost a real dictation before it was understood. A QLockFile in the data directory closes the race on all three systems, taken before the QApplication is even built, and behind it the probe is the side-effect-free status verb: the Settings window opens as the sign of life only when a bare second start deliberately asks for it, not as a byproduct of a probe racing a forwarded toggle. The two Windows relaunch dances collapse into one ipc.respawn. The checkout installer and the packaged setup each kept an autostart the other could not see, so a machine that tried both started two copies at every sign-in: each autostart now removes the other's entry, the silent every-start repair backs off from a Run value whose target still exists, and each uninstaller deletes the shared dikte.cmd only when the shim names its own install. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5
parent
0a70484d84
commit
6f601ab969
@@ -1122,6 +1122,15 @@ def _stay_out_of_the_dock():
|
||||
def run_app(args):
|
||||
command = args[0] if args else ""
|
||||
|
||||
# One Dikte per user, checked by asking rather than by listening: see
|
||||
# ipc.already_serving. Before the QApplication, so a second copy costs a
|
||||
# moment and not a second tray icon.
|
||||
if ipc.already_serving():
|
||||
print("dikte: already running; its Settings window has the attention")
|
||||
if command:
|
||||
ipc.send(command)
|
||||
return 0
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
app.setApplicationName("Dikte")
|
||||
app.setDesktopFileName("dikte")
|
||||
|
||||
+58
-11
@@ -56,6 +56,17 @@ def packaged():
|
||||
return bool(getattr(sys, "frozen", False))
|
||||
|
||||
|
||||
def windowed_executable(executable=None):
|
||||
"""The windowed executable installed beside this one, or None.
|
||||
|
||||
Beside rather than at a known place, because the setup program lays the two
|
||||
executables into the same directory wherever that directory was put: asking
|
||||
from either of them finds the other without knowing where the install is.
|
||||
"""
|
||||
windowed = pathlib.Path(executable or sys.executable).with_name(WINDOWS_APP)
|
||||
return windowed if windowed.is_file() else None
|
||||
|
||||
|
||||
def target():
|
||||
"""The file a launcher has to name to start this build again.
|
||||
|
||||
@@ -74,8 +85,8 @@ def target():
|
||||
# The windowed executable, whichever of the two is running: the console
|
||||
# one is what the `dikte` command names, and a sign-in that started
|
||||
# that one would open a console window nobody asked for.
|
||||
windowed = executable.with_name(WINDOWS_APP)
|
||||
if windowed.is_file():
|
||||
windowed = windowed_executable(executable)
|
||||
if windowed is not None:
|
||||
return windowed
|
||||
return executable
|
||||
|
||||
@@ -572,24 +583,60 @@ def _run_entry_name():
|
||||
return f"HKCU\\{RUN_KEY}\\{RUN_VALUE}"
|
||||
|
||||
|
||||
def _run_target(value):
|
||||
"""The executable a Run value names, out of the quoting the setup wrote.
|
||||
|
||||
Only the first word matters here: it is the file whose existence says
|
||||
whether the entry still starts anything.
|
||||
"""
|
||||
if value.startswith('"'):
|
||||
closing = value.find('"', 1)
|
||||
return value[1:closing] if closing > 0 else ""
|
||||
return value.split(" ", 1)[0]
|
||||
|
||||
|
||||
def _startup_shortcut():
|
||||
"""Where install.ps1 -Autostart puts a checkout's sign-in entry."""
|
||||
appdata = os.environ.get("APPDATA")
|
||||
if not appdata:
|
||||
return None
|
||||
return (pathlib.Path(appdata) / "Microsoft" / "Windows" / "Start Menu"
|
||||
/ "Programs" / "Startup" / "Dikte.lnk")
|
||||
|
||||
|
||||
def _windows_install(app, force=False):
|
||||
"""Point the autostart entry at this build. What changed.
|
||||
|
||||
Only `force`, which is what typing `dikte integrate` means, creates one.
|
||||
The call on every start repairs an entry that is already there and names an
|
||||
executable somewhere else, which is what an installation moved to another
|
||||
drive or reinstalled into another directory leaves behind; somebody who
|
||||
unticked the box in the wizard, or turned it off since, is not asked again
|
||||
by every start.
|
||||
executable that is gone, which is what an installation moved to another
|
||||
drive or reinstalled into another directory leaves behind. An entry naming
|
||||
an executable that still exists is another installation that still works,
|
||||
and is stood aside for the way the Linux half stands aside for another
|
||||
menu entry; somebody who unticked the box in the wizard, or turned it off
|
||||
since, is not asked again by every start either.
|
||||
"""
|
||||
command = f'"{app}"'
|
||||
current = _run_entry()
|
||||
changed = []
|
||||
if force:
|
||||
# install.ps1 -Autostart wrote this for a checkout. The Run value
|
||||
# written below replaces it, and both left in place would be two
|
||||
# Diktes at every sign-in. Only on force: the silent call on every
|
||||
# start has not been asked to move the machine off its checkout.
|
||||
shortcut = _startup_shortcut()
|
||||
if shortcut is not None and shortcut.is_file():
|
||||
shortcut.unlink()
|
||||
changed.append(shortcut)
|
||||
if not current and not force:
|
||||
return []
|
||||
if current == command:
|
||||
return []
|
||||
_write_run_entry(command)
|
||||
return [_run_entry_name()]
|
||||
return changed
|
||||
if current != command:
|
||||
theirs = _run_target(current) if current else ""
|
||||
if not force and theirs and theirs != str(app) and os.path.exists(theirs):
|
||||
return changed
|
||||
_write_run_entry(command)
|
||||
changed.append(_run_entry_name())
|
||||
return changed
|
||||
|
||||
|
||||
def _windows_remove():
|
||||
|
||||
+60
-4
@@ -11,11 +11,14 @@ shortcut may still send.
|
||||
import json
|
||||
import os
|
||||
import shlex
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from PyQt6.QtCore import QLockFile
|
||||
from PyQt6.QtNetwork import QLocalSocket
|
||||
|
||||
from . import integrate
|
||||
from . import paths
|
||||
|
||||
SERVER_NAME = "dikte-" + (
|
||||
str(os.getuid()) if hasattr(os, "getuid")
|
||||
@@ -54,10 +57,9 @@ def launcher():
|
||||
if not getattr(sys, "frozen", False):
|
||||
return [sys.executable, script_path()]
|
||||
if sys.platform == "win32":
|
||||
windowed = os.path.join(os.path.dirname(sys.executable),
|
||||
integrate.WINDOWS_APP)
|
||||
if os.path.isfile(windowed):
|
||||
return [windowed]
|
||||
windowed = integrate.windowed_executable()
|
||||
if windowed is not None:
|
||||
return [str(windowed)]
|
||||
return [os.environ.get("APPIMAGE") or sys.executable]
|
||||
|
||||
|
||||
@@ -72,6 +74,60 @@ def command_for(verb):
|
||||
return shlex.join(launcher() + ([verb] if verb else []))
|
||||
|
||||
|
||||
def already_serving():
|
||||
"""Whether a running instance answers on this user's name.
|
||||
|
||||
Asked before an instance opens a server of its own, because listen() is
|
||||
not the check: a Windows named pipe takes a second server on the same name
|
||||
rather than refusing it, and everywhere else removeServer() would first
|
||||
take the live socket away from the instance holding it. Either way two
|
||||
whole Diktes then run, and the newer one's sweep() kills the whisper the
|
||||
older one is answering dictations with. The probe is "status" and nothing
|
||||
else: a verb with a side effect here would fire it during the relaunch a
|
||||
slow-to-answer instance provokes, on top of the verb being forwarded.
|
||||
"""
|
||||
return send("status") is not None
|
||||
|
||||
|
||||
def instance_lock():
|
||||
"""This user's one-Dikte lock, taken before anything else is built.
|
||||
|
||||
The probe above has a hole: two copies started in the same moment both ask
|
||||
before either listens, and both come up. A lock file closes it, and
|
||||
QLockFile writes the holder's pid into it, so a lock a killed instance
|
||||
left behind identifies itself as stale and clears. None when the data
|
||||
directory cannot be made, which a start should survive: the probe still
|
||||
stands guard, just without the simultaneous-start case.
|
||||
"""
|
||||
try:
|
||||
paths.DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||||
except OSError:
|
||||
return None
|
||||
lock = QLockFile(str(paths.DATA_DIR / "dikte.lock"))
|
||||
# Never presume a lock is stale by age alone; the pid check is the truth.
|
||||
lock.setStaleLockTime(0)
|
||||
return lock
|
||||
|
||||
|
||||
def respawn(arguments):
|
||||
"""Start this installation again with `arguments`, leaving this process.
|
||||
|
||||
execv everywhere it works the way it says: the new process takes this
|
||||
pid and nothing is left behind. On Windows execv mangles arguments with
|
||||
spaces and leaves the two processes sharing a console, so the replacement
|
||||
is started detached instead and the caller exits on its own.
|
||||
"""
|
||||
args = launcher() + list(arguments)
|
||||
if sys.platform == "win32":
|
||||
# By value where the names are missing, so the Windows half of this is
|
||||
# testable from the suite's other platforms too.
|
||||
detached = (getattr(subprocess, "DETACHED_PROCESS", 0x00000008)
|
||||
| getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0x00000200))
|
||||
subprocess.Popen(args, creationflags=detached, close_fds=True)
|
||||
return
|
||||
os.execv(args[0], args)
|
||||
|
||||
|
||||
def send(cmd, wait=False, timeout=0, **args):
|
||||
"""Send one request; the reply, or None when no instance is running.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user