1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-09 23:03:09 +03:00
parent 30da02e25e
commit db1db4cb32
5 changed files with 32 additions and 3 deletions

View file

@ -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/

View file

@ -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

View file

@ -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)])

View file

@ -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,

View file

@ -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()