Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit ed3aff9

Browse files
committed
Close connections gracefully
On shutting down, close all streams and send a GoAway frame to the endpoint.
1 parent 86bd531 commit ed3aff9

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

hyper/http20/connection.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,14 @@ def close(self):
240240
241241
:returns: Nothing.
242242
"""
243-
# Todo: we should actually clean ourselves up if possible by sending
244-
# GoAway frames and closing all outstanding streams. For now this will
245-
# do.
243+
# Close all streams
244+
for stream in list(self.streams.values()):
245+
log.debug("Close stream %d" % stream.stream_id)
246+
stream.close()
247+
248+
# Send GoAway frame to the server
249+
self._send_cb(GoAwayFrame(0), True)
250+
246251
if self._sock is not None:
247252
self._sock.close()
248253
self.__init_state()
@@ -580,7 +585,15 @@ def _consume_frame_payload(self, frame, data):
580585

581586
# Work out to whom this frame should go.
582587
if frame.stream_id != 0:
583-
self.streams[frame.stream_id].receive_frame(frame)
588+
try:
589+
self.streams[frame.stream_id].receive_frame(frame)
590+
except KeyError:
591+
# If we receive an unexpected stream identifier then we
592+
# cancel the stream with an error of type PROTOCOL_ERROR
593+
f = RstStreamFrame(frame.stream_id)
594+
f.error_code = 1 # PROTOCOL_ERROR
595+
self._send_cb(f)
596+
log.warning("Unexpected stream identifier %d", frame.stream_id)
584597
else:
585598
self.receive_frame(frame)
586599

0 commit comments

Comments
 (0)