From 7bf5abbc692f962ca39957e10d4e01d5bf2c5e91 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 23 Jul 2023 16:32:49 +0200 Subject: Add a test for :restart --- doc/changelog.asciidoc | 1 + qutebrowser/misc/quitter.py | 10 +++++++++- tests/end2end/test_invocations.py | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/doc/changelog.asciidoc b/doc/changelog.asciidoc index f356ba1c4..03dbbeeae 100644 --- a/doc/changelog.asciidoc +++ b/doc/changelog.asciidoc @@ -196,6 +196,7 @@ Fixed - The `asciidoc2html.py` script now correctly uses the virtualenv-installed asciidoc rather than requiring a system-wide installation. - "Package would be ignored" deprecation warnings when running `setup.py`. +- ResourceWarning when using `:restart`. - Crash when shutting down before fully initialized. - Crash with some notification servers when the server is quitting. - Crash when using QtWebKit with PAC and the file has an invalid encoding. diff --git a/qutebrowser/misc/quitter.py b/qutebrowser/misc/quitter.py index 9ef708d7a..825acfcd8 100644 --- a/qutebrowser/misc/quitter.py +++ b/qutebrowser/misc/quitter.py @@ -13,6 +13,7 @@ import shutil import argparse import tokenize import functools +import warnings import subprocess from typing import Iterable, Mapping, MutableSequence, Sequence, cast @@ -179,11 +180,18 @@ class Quitter(QObject): # Open a new process and immediately shutdown the existing one try: args = self._get_restart_args(pages, session, override_args) - subprocess.Popen(args) # pylint: disable=consider-using-with + proc = subprocess.Popen(args) # pylint: disable=consider-using-with except OSError: log.destroy.exception("Failed to restart") return False else: + log.destroy.debug(f"New process PID: {proc.pid}") + # Avoid ResourceWarning about still running subprocess when quitting. + warnings.filterwarnings( + "ignore", + category=ResourceWarning, + message=f"subprocess {proc.pid} is still running", + ) return True def shutdown(self, status: int = 0, diff --git a/tests/end2end/test_invocations.py b/tests/end2end/test_invocations.py index 1589bdd69..e31aa3ecb 100644 --- a/tests/end2end/test_invocations.py +++ b/tests/end2end/test_invocations.py @@ -4,6 +4,8 @@ """Test starting qutebrowser with special arguments/environments.""" +import os +import signal import configparser import subprocess import sys @@ -916,3 +918,25 @@ def test_logfilter_arg_does_not_crash(request, quteproc_new): # Waiting for quit to make sure no other warning is emitted quteproc_new.send_cmd(':quit') quteproc_new.wait_for_quit() + + +def test_restart(request, quteproc_new): + args = _base_args(request.config) + ['--temp-basedir'] + quteproc_new.start(args) + quteproc_new.send_cmd(':restart') + + prefix = "New process PID: " + line = quteproc_new.wait_for(message=f"{prefix}*") + quteproc_new.wait_for_quit() + + assert line.message.startswith(prefix) + pid = int(line.message[len(prefix):]) + os.kill(pid, signal.SIGTERM) + + try: + # If the new process hangs, this will hang too. + # Still better than just ignoring it, so we can fix it if something is broken. + os.waitpid(pid, 0) # pid, options... positional-only :( + except ChildProcessError: + # Already gone + pass -- cgit v1.2.3-54-g00ecf