@@ -66,17 +66,17 @@ async def __anext__(self):
66
66
async def probe_host (host , port , payload , connect_timeout , read_timeout ):
67
67
buf = bytearray ()
68
68
69
- async with timeout (connect_timeout ):
70
- r , w = await asyncio .open_connection (host , port )
71
- remote = w .get_extra_info ('peername' )
72
- if remote [0 ] == host :
73
- module .log ('{}:{} - Connected' .format (host , port ), level = 'debug' )
74
- else :
75
- module .log ('{}({}):{} - Connected' .format (host , * remote ), level = 'debug' )
76
- w .write (payload )
77
- await w .drain ()
78
-
79
69
try :
70
+ async with timeout (connect_timeout ):
71
+ r , w = await asyncio .open_connection (host , port )
72
+ remote = w .get_extra_info ('peername' )
73
+ if remote [0 ] == host :
74
+ module .log ('{}:{} - Connected' .format (host , port ), level = 'debug' )
75
+ else :
76
+ module .log ('{}({}):{} - Connected' .format (host , * remote ), level = 'debug' )
77
+ w .write (payload )
78
+ await w .drain ()
79
+
80
80
async with timeout (read_timeout ):
81
81
while len (buf ) < 4096 :
82
82
data = await r .read (4096 )
@@ -90,6 +90,12 @@ async def probe_host(host, port, payload, connect_timeout, read_timeout):
90
90
pass
91
91
else :
92
92
raise
93
+ finally :
94
+ try :
95
+ w .close ()
96
+ except Exception :
97
+ # Either we got something and the socket got in a bad state, or the
98
+ # original error will point to the root cause
99
+ pass
93
100
94
- w .close ()
95
101
return buf
0 commit comments