From db1db4cb32c3b904dbaac3bfe001cd82a9f5123e Mon Sep 17 00:00:00 2001 From: catborise Date: Mon, 9 Nov 2026 23:03:09 +0300 Subject: [PATCH] fix(logging): the web workers rotate the panel's log without losing lines Every gunicorn worker had its own RotatingFileHandler on the panel's log. Once one rotated it, the others went on writing into the renamed file and rotated again in turn: the log then held the lines of one worker, and the others' went into the old files and were deleted with them. The handler is now concurrent-log-handler's, which rotates under a lock file and reopens the log after another process rotated it. That handler keeps no file open, so socketiod's copy of its stderr can be fd 3, the number a serial console worker logs to: dup2 onto itself kept it close-on-exec and the worker failed to start. It is made inheritable. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- Dockerfile | 2 +- conf/requirements.txt | 2 ++ console/socketiod | 1 + webvirtcloud/settings.py.template | 3 ++- webvirtcloud/tests/test_logging.py | 27 ++++++++++++++++++++++++++- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index ccb23460..87defc3f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -55,7 +55,7 @@ COPY . . # for collectstatic. RUN cp webvirtcloud/settings.py.template webvirtcloud/settings.py \ && SECRET_KEY=build-only WEBVIRTCLOUD_LOG_FILE=/tmp/build.log venv/bin/python3 manage.py collectstatic --noinput \ - && rm webvirtcloud/settings.py /tmp/build.log + && rm -f webvirtcloud/settings.py /tmp/build.log # the log handler makes the file at its first line only COPY conf/docker/nginx.conf /etc/nginx/nginx.conf COPY conf/nginx/webvirtcloud.conf /etc/nginx/conf.d/ diff --git a/conf/requirements.txt b/conf/requirements.txt index 336a4cfe..1e8b884f 100644 --- a/conf/requirements.txt +++ b/conf/requirements.txt @@ -6,6 +6,8 @@ django-otp==1.7.5 idna>=3.15 requests>=2.33.0 urllib3>=2.8.0 +# the panel's processes rotate one log file without losing each other's lines +concurrent-log-handler==0.9.31 django-axes==8.3.1 djangorestframework==3.18.3 django-rest-knox==5.1.1 diff --git a/console/socketiod b/console/socketiod index 49f5b474..c4110cbc 100755 --- a/console/socketiod +++ b/console/socketiod @@ -97,6 +97,7 @@ def connect(sid, environ, auth): # pty.fork's master is inheritable: without this each worker would # hold the other sessions' consoles open os.dup2(LOG_FD, 3) + os.set_inheritable(3, True) # LOG_FD may be 3 itself: dup2 then keeps it close-on-exec os.closerange(4, os.sysconf("SC_OPEN_MAX")) os.environ["CONSOLECALLBACK_LOG_FD"] = "3" os.execv(sys.executable, [sys.executable, CONSOLECALLBACK, str(instance.pk), str(user.pk)]) diff --git a/webvirtcloud/settings.py.template b/webvirtcloud/settings.py.template index 661d971b..03ab1aee 100644 --- a/webvirtcloud/settings.py.template +++ b/webvirtcloud/settings.py.template @@ -289,7 +289,8 @@ LOGGING = { }, 'default': { 'level': 'INFO', - 'class': 'logging.handlers.RotatingFileHandler', + # every web worker writes the file: one rotates it at a time, under a lock + 'class': 'concurrent_log_handler.ConcurrentRotatingFileHandler', 'filename': WEBVIRTCLOUD_LOG_FILE, 'maxBytes': 10 * 1024 * 1024, 'backupCount': 5, diff --git a/webvirtcloud/tests/test_logging.py b/webvirtcloud/tests/test_logging.py index f9a9a51e..97162b4e 100644 --- a/webvirtcloud/tests/test_logging.py +++ b/webvirtcloud/tests/test_logging.py @@ -18,7 +18,7 @@ from webvirtcloud.middleware import RequestIdMiddleware class LoggingConfigTestCase(SimpleTestCase): def test_default_handler_rotates(self): handler = settings.LOGGING["handlers"]["default"] - self.assertEqual(handler["class"], "logging.handlers.RotatingFileHandler") + self.assertEqual(handler["class"], "concurrent_log_handler.ConcurrentRotatingFileHandler") self.assertGreater(handler["maxBytes"], 0) self.assertGreater(handler["backupCount"], 0) @@ -49,6 +49,31 @@ class LoggingConfigTestCase(SimpleTestCase): self.assertTrue(os.path.isdir(os.path.dirname(log)), log) + def test_a_process_writes_into_the_file_another_one_rotated(self): + # each web worker has its own handler on the file; with a plain + # RotatingFileHandler the others went on writing into the rotated one + import tempfile + + from django.utils.module_loading import import_string + + tmp = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, tmp) + log = os.path.join(tmp, "wvc.log") + handler_class = import_string(settings.LOGGING["handlers"]["default"]["class"]) + first, second = (handler_class(log, maxBytes=100, backupCount=5) for _ in range(2)) + self.addCleanup(first.close) + self.addCleanup(second.close) + + def write(handler, text): + handler.emit(logging.LogRecord("wvc", logging.INFO, __file__, 0, text, None, None)) + + write(second, "second, before") + for _ in range(3): + write(first, "first " + "x" * 60) # rotates + write(second, "second, after") + with open(log) as current: + self.assertIn("second, after", current.read()) + class RequestIdTestCase(TestCase): def test_a_line_of_a_request_names_the_request_user_and_address(self): stream = io.StringIO()