@@ -69,8 +69,10 @@ def __init__(self, aio_ctx: AioContext, connector: ConnectorInfo, conn_props: di
6969
7070 conf = CommConfigurator ()
7171 if conf .get_bool_var ("simulate_unstable_network" , default = False ):
72- self .disconn = threading .Thread (target = self ._disconnect , daemon = True )
73- self .disconn .start ()
72+ if context :
73+ # only server side
74+ self .disconn = threading .Thread (target = self ._disconnect , daemon = True )
75+ self .disconn .start ()
7476
7577 def _disconnect (self ):
7678 t = random .randint (10 , 60 )
@@ -88,9 +90,11 @@ def close(self):
8890 if self .context :
8991 self .aio_ctx .run_coro (self .context .abort (grpc .StatusCode .CANCELLED , "service closed" ))
9092 self .context = None
93+ self .logger .info ("Closed GRPC context" )
9194 if self .channel :
9295 self .aio_ctx .run_coro (self .channel .close ())
9396 self .channel = None
97+ self .logger .info ("Closed GRPC Channel" )
9498
9599 def send_frame (self , frame : BytesAlike ):
96100 try :
@@ -298,57 +302,42 @@ async def _start_connect(self, connector: ConnectorInfo, aio_ctx: AioContext, co
298302 address = get_address (params )
299303
300304 self .logger .debug (f"CLIENT: trying to connect { address } " )
305+ connection = None
301306 try :
302307 secure = ssl_required (params )
303308 if secure :
304- grpc_channel = grpc .aio .secure_channel (
309+ channel = grpc .aio .secure_channel (
305310 address , options = self .options , credentials = get_grpc_client_credentials (params )
306311 )
307312 self .logger .info (f"created secure channel at { address } " )
308313 else :
309- grpc_channel = grpc .aio .insecure_channel (address , options = self .options )
314+ channel = grpc .aio .insecure_channel (address , options = self .options )
310315 self .logger .info (f"created insecure channel at { address } " )
316+ stub = StreamerStub (channel )
311317
312- async with grpc_channel as channel :
313- self .logger .debug (f"CLIENT: connected to { address } " )
314- stub = StreamerStub (channel )
315- conn_props = {DriverParams .PEER_ADDR .value : address }
316-
317- if secure :
318- conn_props [DriverParams .PEER_CN .value ] = "N/A"
318+ self .logger .debug (f"CLIENT: connected to { address } " )
319+ conn_props = {DriverParams .PEER_ADDR .value : address }
319320
320- connection = AioStreamSession (
321- aio_ctx = aio_ctx , connector = connector , conn_props = conn_props , channel = channel
322- )
321+ if secure :
322+ conn_props [DriverParams .PEER_CN .value ] = "N/A"
323323
324- try :
325- self .logger .debug (f"CLIENT: start streaming on connection { connection } " )
326- msg_iter = stub .Stream (connection .generate_output ())
327- conn_ctx .conn = connection
328- await connection .read_loop (msg_iter )
329- except asyncio .CancelledError as error :
330- self .logger .debug (f"CLIENT: RPC cancelled: { error } " )
331- except Exception as ex :
332- if self .closing :
333- self .logger .debug (
334- f"Connection { connection } closed by { type (ex )} : { secure_format_exception (ex )} "
335- )
336- else :
337- self .logger .debug (
338- f"Connection { connection } client read exception { type (ex )} : { secure_format_exception (ex )} "
339- )
340- self .logger .debug (secure_format_traceback ())
324+ connection = AioStreamSession (aio_ctx = aio_ctx , connector = connector , conn_props = conn_props , channel = channel )
341325
342- with connection .lock :
343- connection .channel = None
344- connection .close ()
326+ self .logger .debug (f"CLIENT: start streaming on connection { connection } " )
327+ msg_iter = stub .Stream (connection .generate_output ())
328+ conn_ctx .conn = connection
329+ await connection .read_loop (msg_iter )
345330 except asyncio .CancelledError :
346331 self .logger .debug ("CLIENT: RPC cancelled" )
332+ except grpc .FutureCancelledError :
333+ self .logger .info ("CLIENT: Future cancelled" )
347334 except Exception as ex :
348335 conn_ctx .error = f"connection { connection } error: { type (ex )} : { secure_format_exception (ex )} "
349336 self .logger .debug (conn_ctx .error )
350337 self .logger .debug (secure_format_traceback ())
351-
338+ finally :
339+ if connection :
340+ connection .close ()
352341 conn_ctx .waiter .set ()
353342
354343 def connect (self , connector : ConnectorInfo ):
0 commit comments