From 310e0e27d7d7753bc45a69113efeab0cb8db9c5c Mon Sep 17 00:00:00 2001 From: mid-kid Date: Wed, 30 Oct 2024 17:07:10 +0100 Subject: [PATCH] Add locks for g_connections --- auth/server.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/auth/server.py b/auth/server.py index 66578c3..9debe2c 100755 --- a/auth/server.py +++ b/auth/server.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 from http import HTTPStatus from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler +from threading import Lock from urllib.parse import urlparse, parse_qs from configparser import ConfigParser from base64 import b64encode @@ -8,7 +9,8 @@ from os.path import getmtime import json userdata = {} -g_connections = {} +_g_connections = {} +_g_connections_lock = Lock() class HTTPRequestHandler(BaseHTTPRequestHandler): protocol_version = "HTTP/1.1" @@ -134,7 +136,8 @@ class HTTPRequestHandler(BaseHTTPRequestHandler): token, profileId = params["sessionId"][0].split(":")[1:] serverId = params["serverId"][0] - g_connections[serverId] = profileId + with _g_connections_lock: + _g_connections[serverId] = profileId if True: self.send_ok(b"OK") @@ -148,7 +151,8 @@ class HTTPRequestHandler(BaseHTTPRequestHandler): profileId = data["selectedProfile"] serverId = data["serverId"] - g_connections[serverId] = profileId + with _g_connections_lock: + _g_connections[serverId] = profileId if True: self.send_response(HTTPStatus.NO_CONTENT) @@ -164,8 +168,11 @@ class HTTPRequestHandler(BaseHTTPRequestHandler): user = params["user"][0] serverId = params["serverId"][0] - if not serverId in g_connections: - self.send_ok(b"NO") + with _g_connections_lock: + if not serverId in _g_connections: + self.send_ok(b"NO") + return + del _g_connections[serverId] self.send_ok(b"YES") @@ -173,11 +180,12 @@ class HTTPRequestHandler(BaseHTTPRequestHandler): serverId = params["serverId"][0] user = params["username"][0] - if not serverId in g_connections: - self.send_response(HTTPStatus.NO_CONTENT) - self.end_headers() - - profileId = g_connections[serverId] + with _g_connections_lock: + if not serverId in _g_connections: + self.send_response(HTTPStatus.NO_CONTENT) + self.end_headers() + del _g_connections[serverId] + profileId = _g_connections[serverId] data = self.get_profile(profileId) if data is None: