Skip to content

Commit

Permalink
Merge PR #322 and PR #331 into master
Browse files Browse the repository at this point in the history
This change fixes #328 by resetting the "serving" flag on SIGINT and
SIGTERM. It also makes sure to properly process iterating over a
None-map returned from a closed selector.
  • Loading branch information
webknjaz committed Nov 15, 2020
3 parents 5de9b05 + 5339424 + ae47c70 commit e705cf6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
.. scm-version-title:: v8.4.6

- :issue:`328` via :pr:`322` and :pr:`331`: Fixed a
regression introduced in the earlier refactoring in v8.4.4
via :pr:`309` that caused the :py:meth:`~cheroot.server.\
HTTPRequest.serve` method to skip setting
``serving=False`` on :py:data:``SIGINT`` and
:py:data:``SIGTERM`` -- by :user:`marc1n` and
:user:`cristicbz`.

.. scm-version-title:: v8.4.5

- :issue:`312` via :pr:`313`: Fixed a regression introduced
Expand Down
10 changes: 7 additions & 3 deletions cheroot/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,13 @@ def close(self):
conn.close()
self._readable_conns.clear()

for _, key in self._selector.get_map().items():
if key.data != self.server: # server closes its own socket
key.data.socket.close()
connections = (
conn.data.socket
for _, conn in (self._selector.get_map() or {}).items()
if conn.data != self.server # server closes its own socket
)
for connection in connections:
connection.close()

self._selector.close()

Expand Down
1 change: 1 addition & 0 deletions cheroot/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,7 @@ def serve(self):
try:
self.tick()
except (KeyboardInterrupt, SystemExit):
self.serving = False
raise
except Exception:
self.error_log(
Expand Down
26 changes: 26 additions & 0 deletions cheroot/test/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,32 @@ def test_stop_interrupts_serve():
assert not serve_thread.is_alive()


def test_serving_is_false_and_stop_returns_after_ctrlc():
"""Check that stop() interrupts running of serve()."""
httpserver = HTTPServer(
bind_addr=(ANY_INTERFACE_IPV4, EPHEMERAL_PORT),
gateway=Gateway,
)

httpserver.prepare()

# Simulate a Ctrl-C on the first call to `get_conn`.
def raise_keyboard_interrupt(*args):
raise KeyboardInterrupt()

httpserver._connections.get_conn = raise_keyboard_interrupt

serve_thread = threading.Thread(target=httpserver.serve)
serve_thread.start()

# The thread should exit right away due to the interrupt.
serve_thread.join(0.5)
assert not serve_thread.is_alive()

assert not httpserver.serving
httpserver.stop()


@pytest.mark.parametrize(
'ip_addr',
(
Expand Down

0 comments on commit e705cf6

Please sign in to comment.