1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 09:26:03 +00:00
webvirtcloud/webvirtcloud/gunicorn_worker.py
catborise 58b733d5b7 feat(webvirtcloud): a request waiting on a host holds one thread, not a worker
A host that is up but does not answer (a hung NFS mount, for one) keeps
a libvirt call waiting: libvirt's keepalive does not end it, since the
daemon still answers keepalives. Each such request held a whole sync
worker, and once they held every worker the panel stopped answering,
pages without libvirt too; F5 added another.

gunicorn now runs threaded workers, 4 threads each, and a worker takes
no more connections than it has threads, so one whose threads are all
busy leaves new requests to the others. A threaded worker keeps
reporting in while a thread waits, so gunicorn's timeout no longer ends
the request: the worker now exits itself when a request runs past the
timeout (600 s), and gunicorn starts a new one. Short claims expire on
that bound.

The address search runs one at a time in a process, as it did with one
request per worker.
2026-11-02 21:19:01 +03:00

38 lines
1.4 KiB
Python

"""gunicorn's threaded worker with the sync worker's request timeout."""
import os
import threading
import time
from gunicorn.workers.gthread import ThreadWorker
class Worker(ThreadWorker):
"""gunicorn kills a sync worker whose request runs past its timeout; a
threaded worker keeps reporting in while one of its threads waits on a
host that does not answer. This one exits when a request runs past the
timeout, and gunicorn starts a new one: a request ends by then, as the
short claims of operations.claims expect."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.started = {} # thread: when its request started
self.started_lock = threading.Lock()
def handle(self, conn):
thread = threading.get_ident()
with self.started_lock:
self.started[thread] = time.monotonic()
try:
return super().handle(conn)
finally:
with self.started_lock:
del self.started[thread]
def notify(self):
with self.started_lock:
oldest = min(self.started.values(), default=None)
if oldest is not None and time.monotonic() - oldest > self.cfg.timeout:
self.log.critical("A request ran past %s s: the worker exits", self.cfg.timeout)
os._exit(1) # its threads cannot be stopped, and exit() would wait for them
super().notify()