Skip to content

Commit 65d4494

Browse files
André DietrichAndré Dietrich
authored andcommitted
fix: heroko health check 2
1 parent 6904a78 commit 65d4494

1 file changed

Lines changed: 32 additions & 16 deletions

File tree

server.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import tempfile
66
import json
77
import logging
8+
import socket
89
from typing import List
910
import pathlib
1011
import base64
@@ -23,25 +24,35 @@
2324
class NewWebSocketHandler (WebSocketHandler):
2425
def read_http_headers(self):
2526
headers = {}
26-
http_get = self.rfile.readline().decode().strip()
27-
28-
if not http_get.upper().startswith('GET'):
29-
logging.debug("Non-GET HTTP request, treating as health check: %r", http_get)
30-
31-
# headers lesen
32-
while True:
33-
header = self.rfile.readline().decode().strip()
34-
if not header:
35-
break
36-
try:
37-
head, value = header.split(':', 1)
38-
headers[head.lower().strip()] = value.strip()
39-
except ValueError:
40-
logging.debug("Malformed header line ignored: %r", header)
27+
try:
28+
self.connection.settimeout(10) # prevent indefinite blocking on health-check reads
29+
http_get = self.rfile.readline().decode().strip()
30+
31+
if not http_get.upper().startswith('GET'):
32+
logging.debug("Non-GET HTTP request, treating as health check: %r", http_get)
33+
34+
# headers lesen
35+
while True:
36+
header = self.rfile.readline().decode().strip()
37+
if not header:
38+
break
39+
try:
40+
head, value = header.split(':', 1)
41+
headers[head.lower().strip()] = value.strip()
42+
except ValueError:
43+
logging.debug("Malformed header line ignored: %r", header)
44+
except Exception as e:
45+
logging.debug("Error reading HTTP headers: %s", e)
46+
finally:
47+
self.connection.settimeout(None) # restore blocking mode for WebSocket reads
4148
return headers
4249

4350
def handshake(self):
44-
headers = self.read_http_headers()
51+
try:
52+
headers = self.read_http_headers()
53+
except Exception as e:
54+
logging.debug("handshake: failed to read headers: %s", e)
55+
headers = {}
4556

4657
if 'upgrade' in headers:
4758
try:
@@ -79,6 +90,11 @@ def handshake(self):
7990
try:
8091
with self._send_lock:
8192
self.request.sendall(response)
93+
# Send TCP FIN so the client knows the response is complete
94+
try:
95+
self.request.shutdown(socket.SHUT_WR)
96+
except Exception:
97+
pass
8298
except BrokenPipeError:
8399
logging.debug("Client closed before health-check response.")
84100
except Exception as e:

0 commit comments

Comments
 (0)