mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-11 12:46:49 +00:00
fix(gstfsd): at most 4 connections at a time; one more is told it is busy
Every connection got a thread before any authentication, and every signed request a guestfs appliance, with no limit: with remote access allowed, anyone on that network could make the host run out of threads or memory. A connection over MAX_CLIENTS (4) now gets an unsigned busy line and is closed before it gets a thread; the panel shows it as the gstfsd error.
This commit is contained in:
parent
2cbcb8ab2c
commit
89035038e1
3 changed files with 50 additions and 1 deletions
|
|
@ -43,6 +43,9 @@ KEY = os.environ.get("GSTFSD_KEY", "").strip()
|
|||
PORT = int(os.environ.get("GSTFSD_PORT", 16510))
|
||||
ADDRESS = os.environ.get("GSTFSD_BIND_HOST", "127.0.0.1")
|
||||
MAX_REQUEST = 64 * 1024
|
||||
# connections at a time, each a thread that may run a guestfs appliance; one
|
||||
# more is told gstfsd is busy before it gets a thread
|
||||
MAX_CLIENTS = 4
|
||||
# A request, from accepting the connection to closing the appliance, must
|
||||
# finish within this many seconds, or it is stopped: the panel gives up 120 s
|
||||
# after connecting and releases the VM, which may then be started.
|
||||
|
|
@ -344,6 +347,23 @@ def serve():
|
|||
class MyTCPServer(socketserver.ThreadingTCPServer):
|
||||
allow_reuse_address = True
|
||||
address_family = socket.AF_INET6 if ":" in ADDRESS else socket.AF_INET
|
||||
clients = threading.BoundedSemaphore(MAX_CLIENTS)
|
||||
|
||||
def process_request(self, request, client_address):
|
||||
if not self.clients.acquire(blocking=False):
|
||||
try:
|
||||
request.sendall(b'{"error": "gstfsd is busy with other changes: try again in a moment"}\n')
|
||||
except OSError:
|
||||
pass
|
||||
self.shutdown_request(request)
|
||||
return
|
||||
super().process_request(request, client_address)
|
||||
|
||||
def process_request_thread(self, request, client_address):
|
||||
try:
|
||||
super().process_request_thread(request, client_address)
|
||||
finally:
|
||||
self.clients.release()
|
||||
|
||||
MyTCPServer((ADDRESS, PORT), MyTCPServerHandler).serve_forever()
|
||||
return 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue