@@ -276,19 +276,19 @@ def _recv(sock, n):
276276 return sock .recv (n ).decode ('ascii' )
277277
278278def latency_client (addr , nb_pings , interval ):
279- sock = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
280- _time = time .time
281- _sleep = time .sleep
282- def _ping ():
283- _sendto (sock , "%r\n " % _time (), addr )
284- # The first ping signals the parent process that we are ready.
285- _ping ()
286- # We give the parent a bit of time to notice.
287- _sleep (1.0 )
288- for i in range (nb_pings ):
289- _sleep (interval )
279+ with socket .socket (socket .AF_INET , socket .SOCK_DGRAM ) as sock :
280+ _time = time .time
281+ _sleep = time .sleep
282+ def _ping ():
283+ _sendto (sock , "%r\n " % _time (), addr )
284+ # The first ping signals the parent process that we are ready.
290285 _ping ()
291- _sendto (sock , LAT_END + "\n " , addr )
286+ # We give the parent a bit of time to notice.
287+ _sleep (1.0 )
288+ for i in range (nb_pings ):
289+ _sleep (interval )
290+ _ping ()
291+ _sendto (sock , LAT_END + "\n " , addr )
292292
293293def run_latency_client (** kwargs ):
294294 cmd_line = [sys .executable , '-E' , os .path .abspath (__file__ )]
@@ -363,6 +363,7 @@ def run():
363363 for t in threads :
364364 t .join ()
365365 process .wait ()
366+ sock .close ()
366367
367368 for recv_time , chunk in chunks :
368369 # NOTE: it is assumed that a line sent by a client wasn't received
0 commit comments