@@ -40,7 +40,7 @@ def __init__(self,
4040 stream_id ,
4141 window_manager ,
4242 connection ,
43- send_cb ,
43+ send_outstanding_data ,
4444 recv_cb ,
4545 close_cb ):
4646 self .stream_id = stream_id
@@ -72,11 +72,11 @@ 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.
79- self ._send_cb = send_cb
79+ self ._send_outstanding_data = send_outstanding_data
8080 self ._recv_cb = recv_cb
8181 self ._close_cb = close_cb
8282
@@ -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_outstanding_data ()
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_outstanding_data ()
193195
194196 def receive_end_stream (self , event ):
195197 """
@@ -278,15 +280,14 @@ def close(self, error_code=None):
278280 # FIXME: I think this is overbroad, but for now it's probably ok.
279281 if not (self .remote_closed and self .local_closed ):
280282 try :
281- self ._conn .reset_stream (self .stream_id , error_code or 0 )
283+ with self ._conn as conn :
284+ conn .reset_stream (self .stream_id , error_code or 0 )
282285 except h2 .exceptions .ProtocolError :
283- # If for any reason we can't reset the stream, just tolerate
284- # it.
286+ # If for any reason we can't reset the stream, just
287+ # tolerate it.
285288 pass
286289 else :
287- self ._send_cb (
288- self ._conn .data_to_send (), tolerate_peer_gone = True
289- )
290+ self ._send_outstanding_data (tolerate_peer_gone = True )
290291 self .remote_closed = True
291292 self .local_closed = True
292293
@@ -297,7 +298,9 @@ def _out_flow_control_window(self):
297298 """
298299 The size of our outbound flow control window.
299300 """
300- return self ._conn .local_flow_control_window (self .stream_id )
301+
302+ with self ._conn as conn :
303+ return conn .local_flow_control_window (self .stream_id )
301304
302305 def _send_chunk (self , data , final ):
303306 """
@@ -321,10 +324,11 @@ def _send_chunk(self, data, final):
321324 end_stream = True
322325
323326 # 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 ())
327+ with self ._conn as conn :
328+ conn .send_data (
329+ stream_id = self .stream_id , data = data , end_stream = end_stream
330+ )
331+ self ._send_outstanding_data ()
328332
329333 if end_stream :
330334 self .local_closed = True
0 commit comments