Skip to content

Commit c532189

Browse files
committed
Address review comments
1 parent 047136e commit c532189

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

cheroot/server.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,11 @@ def _close_kernel_socket(self):
15201520
raise
15211521

15221522

1523+
# Unique identifier used to indicate the thread that handles unservicable
1524+
# connections should shut down.
1525+
_SHUT_DOWN_UNSERVICABLES_THREAD = object()
1526+
1527+
15231528
class HTTPServer:
15241529
"""An HTTP server."""
15251530

@@ -1872,23 +1877,30 @@ def _serve_unservicable(self):
18721877
"""Serve connections we can't handle a 503."""
18731878
while self.ready:
18741879
conn = self._unservicable_conns.get()
1875-
if conn is None:
1880+
if conn is _SHUT_DOWN_UNSERVICABLES_THREAD:
18761881
return
18771882
request = HTTPRequest(self, conn)
18781883
try:
18791884
request.simple_response('503 Service Unavailable')
1885+
except (socket.error, errors.FatalSSLAlert):
1886+
# We're sending the 503 error to be polite, it it fails that's
1887+
# fine.
1888+
continue
18801889
except Exception as ex:
18811890
self.server.error_log(
18821891
repr(ex),
18831892
level=logging.ERROR,
18841893
traceback=True,
18851894
)
1886-
conn.linger = True
18871895
conn.close()
18881896

18891897
def serve(self):
18901898
"""Serve requests, after invoking :func:`prepare()`."""
1899+
# This thread will handle unservicable connections, as added to
1900+
# self._unservicable_conns queue. It will run forever, until
1901+
# self.stop() tells it to shut down.
18911902
threading.Thread(target=self._serve_unservicable).start()
1903+
18921904
while self.ready and not self.interrupt:
18931905
try:
18941906
self._connections.run(self.expiration_interval)
@@ -2221,7 +2233,11 @@ def stop(self): # noqa: C901 # FIXME
22212233
return # already stopped
22222234

22232235
self.ready = False
2224-
self._unservicable_conns.put(None)
2236+
2237+
# This tells the thread that handles unservicable connections to shut
2238+
# down:
2239+
self._unservicable_conns.put(_SHUT_DOWN_UNSERVICABLES_THREAD)
2240+
22252241
if self._start_time is not None:
22262242
self._run_time += time.time() - self._start_time
22272243
self._start_time = None

cheroot/test/test_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,5 +602,5 @@ def test_overload_results_in_suitable_http_error(request):
602602
# requests fail:
603603
httpserver.requests._queue.put(None)
604604

605-
response = requests.get(f'http://127.0.0.1:{port}', timeout=20)
606-
assert response.status_code == HTTPStatus.SERVICE_UNAVAILABLE.value
605+
response = requests.get(f'http://{localhost}:{port}', timeout=20)
606+
assert response.status_code == HTTPStatus.SERVICE_UNAVAILABLE

0 commit comments

Comments
 (0)