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

Commit bb70299

Browse files
committed
Ensure that conn is only used inside a context so that locking is ensured
1 parent fc5a32c commit bb70299

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

hyper/http20/stream.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(self,
7272
# one for data being sent to us.
7373
self._in_window_manager = window_manager
7474

75-
# Save off a reference to the state machine.
75+
# Save off a reference to the state machine wrapped with lock.
7676
self._conn = connection
7777

7878
# Save off a data callback.
@@ -94,8 +94,9 @@ def send_headers(self, end_stream=False):
9494
Sends the complete saved header block on the stream.
9595
"""
9696
headers = self.get_headers()
97-
self._conn.send_headers(self.stream_id, headers, end_stream)
98-
self._send_cb(self._conn.data_to_send())
97+
with self._conn as conn:
98+
conn.send_headers(self.stream_id, headers, end_stream)
99+
self._send_cb(conn.data_to_send())
99100

100101
if end_stream:
101102
self.local_closed = True
@@ -186,10 +187,11 @@ def receive_data(self, event):
186187
self.data.append(event.data)
187188

188189
if increment and not self.remote_closed:
189-
self._conn.increment_flow_control_window(
190-
increment, stream_id=self.stream_id
191-
)
192-
self._send_cb(self._conn.data_to_send())
190+
with self._conn as conn:
191+
conn.increment_flow_control_window(
192+
increment, stream_id=self.stream_id
193+
)
194+
self._send_cb(conn.data_to_send())
193195

194196
def receive_end_stream(self, event):
195197
"""
@@ -277,16 +279,17 @@ def close(self, error_code=None):
277279
"""
278280
# FIXME: I think this is overbroad, but for now it's probably ok.
279281
if not (self.remote_closed and self.local_closed):
280-
try:
281-
self._conn.reset_stream(self.stream_id, error_code or 0)
282-
except h2.exceptions.ProtocolError:
283-
# If for any reason we can't reset the stream, just tolerate
284-
# it.
285-
pass
286-
else:
287-
self._send_cb(
288-
self._conn.data_to_send(), tolerate_peer_gone=True
289-
)
282+
with self._conn as conn:
283+
try:
284+
conn.reset_stream(self.stream_id, error_code or 0)
285+
except h2.exceptions.ProtocolError:
286+
# If for any reason we can't reset the stream, just tolerate
287+
# it.
288+
pass
289+
else:
290+
self._send_cb(
291+
conn.data_to_send(), tolerate_peer_gone=True
292+
)
290293
self.remote_closed = True
291294
self.local_closed = True
292295

@@ -297,7 +300,8 @@ def _out_flow_control_window(self):
297300
"""
298301
The size of our outbound flow control window.
299302
"""
300-
return self._conn.local_flow_control_window(self.stream_id)
303+
304+
return self._conn._obj.local_flow_control_window(self.stream_id)
301305

302306
def _send_chunk(self, data, final):
303307
"""
@@ -321,10 +325,11 @@ def _send_chunk(self, data, final):
321325
end_stream = True
322326

323327
# Send the frame and decrement the flow control window.
324-
self._conn.send_data(
325-
stream_id=self.stream_id, data=data, end_stream=end_stream
326-
)
327-
self._send_cb(self._conn.data_to_send())
328+
with self._conn as conn:
329+
conn.send_data(
330+
stream_id=self.stream_id, data=data, end_stream=end_stream
331+
)
332+
self._send_cb(conn.data_to_send())
328333

329334
if end_stream:
330335
self.local_closed = True

0 commit comments

Comments
 (0)