Skip to content

fix(app): keep a second instance off a profile that is still in use - #1047

Open
bartech-lab wants to merge 2 commits into
rafatosta:mainfrom
bartech-lab:fix/profile-instance-lock
Open

bartech-lab wants to merge 2 commits into
rafatosta:mainfrom
bartech-lab:fix/profile-instance-lock

Conversation

@bartech-lab

Copy link
Copy Markdown

Fixes #1038.

The single-instance check only connects to the local socket. When an earlier ZapZap process is still alive but its socket is gone, a second instance opens the same WebEngine profile, shows the QR login page and resets the quota database.

This change takes a QLockFile (instance.lock in the data directory) before any WebEngine profile is created:

  • Another live process holds the lock: the new instance exits with "ZapZap is already running (PID n) but does not answer. Close that process and start ZapZap again."
  • "Restart application" starts the successor before the old process has exited, so the new instance waits up to 10 seconds for the lock.
  • A crashed or killed process leaves the lock file behind; QLockFile sees that the process is gone and takes the lock over.
  • If the lock file cannot be created at all (for example a read-only data directory), ZapZap starts as before.

The socket path is unchanged, so a normal second launch still activates the running window.

Validation

  • New tests/test_single_instance_lock.py runs real separate instances with a throwaway data and socket directory: live process without socket (the second instance exits with code 1), lock left by a killed process (taken over), and a restart-like handover (successor waits and starts). The first fails if the instance does not exit on a held lock, and the third fails if the wait is removed. They are skipped outside Linux and are not in the CI workflow, which runs named test files only.
  • The reproduction script from Second instance starts on a profile that is still in use and shows the QR login page #1038 against this branch: the second instance now exits with the message above. On main it still reproduces.
  • "Restart application" end to end (zapzap zapzap://restart sent to a running instance): the old process exits and the new one holds the lock and listens on the socket.
  • Full test suite, check_unused_code.py, test_documentation_structure.py, compileall and git diff --check: no new failures. Eight tests fail identically on main in my environment (dictionaries, zh_CN catalog, macOS packaging, one accounts UI test).

I ran the test list from AGENTS.md in the Flatpak (com.rtosta.zapzap 7.5.1 from Flathub: Python 3.13, Qt 6.11.2, PyQt6 6.11.0) and with the Arch system Python (Qt 6.12, PyQt6 6.12.1, QtWebEngine 6.140), with the same results in both. The reproduction script and the restart check ran with the system Python. CHANGELOG, docs/architecture.md and the docs/testing.md inventory are updated.

One limitation: the message goes to stderr only, so a launch from the app menu waits up to 10 seconds and then shows nothing. A dialog would need new translatable strings; I can add one if you prefer.

The single-instance check only connects to the local socket. When an
earlier process is still alive but its socket is gone, a second instance
opened the same WebEngine profile, showed the QR login page and reset the
quota database (rafatosta#1038).

Take a QLockFile in the data directory before any WebEngine profile is
created. While another live process holds it, exit with a message naming
that process. Wait up to 10 seconds so the successor of a full restart
can start while the old process exits. The lock of a dead process is
taken over.

Tests:
- python -m unittest tests/test_single_instance_lock.py -v
- python -m unittest discover -s tests -q (same pre-existing failures as main)
- python tests/check_unused_code.py --packages-only
- python tests/test_documentation_structure.py -v
- python -m compileall -q zapzap tests tools run.py
- git diff --check
Exit only when another process holds the lock. A data directory where the
lock file cannot be created is not proof of another instance. Skip the
lock tests outside Linux, where they rely on XDG_DATA_HOME and a socket
file in TMPDIR.

Tests:
- python -m unittest tests/test_single_instance_lock.py -v
- python -m unittest discover -s tests -q (same pre-existing failures as main)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Second instance starts on a profile that is still in use and shows the QR login page

1 participant