@@ -63,33 +63,37 @@ def _closed(sock):
63
63
class SocketInfo (object ):
64
64
"""Store a socket with some metadata
65
65
"""
66
- def __init__ (self , sock , pool ):
66
+ def __init__ (self , sock , poolref ):
67
67
self .sock = sock
68
68
69
69
# We can't strongly reference the Pool, because the Pool
70
70
# references this SocketInfo as long as it's in pool
71
- self .poolref = weakref . ref ( pool )
71
+ self .poolref = poolref
72
72
73
73
self .authset = set ()
74
74
self .closed = False
75
75
self .last_checkout = time .time ()
76
- self .pool_id = pool .pool_id
76
+ self .pool_id = poolref () .pool_id
77
77
78
78
def close (self ):
79
- self .sock .close ()
80
79
self .closed = True
80
+ # Avoid exceptions on interpreter shutdown.
81
+ try :
82
+ self .sock .close ()
83
+ except :
84
+ pass
81
85
82
86
def __del__ (self ):
83
87
if not self .closed :
84
88
# This socket was given out, but not explicitly returned. Perhaps
85
89
# the socket was assigned to a thread local for a request, but the
86
90
# request wasn't ended before the thread died. Reclaim the socket
87
91
# for the pool.
88
- pool = self .poolref and self . poolref ()
92
+ pool = self .poolref ()
89
93
if pool :
90
94
# Return a copy of self rather than self -- the Python docs
91
95
# discourage postponing deletion by adding a reference to self.
92
- copy = SocketInfo (self .sock , pool )
96
+ copy = SocketInfo (self .sock , self . poolref )
93
97
copy .authset = self .authset
94
98
pool .return_socket (copy )
95
99
else :
@@ -220,7 +224,7 @@ def connect(self, pair):
220
224
"not be configured with SSL support." )
221
225
222
226
sock .settimeout (self .net_timeout )
223
- return SocketInfo (sock , self )
227
+ return SocketInfo (sock , weakref . ref ( self ) )
224
228
225
229
def get_socket (self , pair = None ):
226
230
"""Get a socket from the pool.
@@ -384,18 +388,6 @@ def __init__(self, *args, **kwargs):
384
388
self .local = _Local ()
385
389
super (Pool , self ).__init__ (* args , ** kwargs )
386
390
387
- def __del__ (self ):
388
- # If we're being deleted on a thread that started a request, then the
389
- # request socket might still be in a thread-local; get it and close it.
390
- # The 'hasattr' checks avoid TypeError during interpreter shutdown.
391
- request_sock = self ._get_request_state ()
392
- if hasattr (request_sock , 'close' ):
393
- request_sock .close ()
394
-
395
- for sock_info in self .sockets :
396
- if hasattr (sock_info , 'close' ):
397
- sock_info .close ()
398
-
399
391
def _set_request_state (self , sock_info ):
400
392
self .local .sock_info = sock_info
401
393
@@ -419,16 +411,6 @@ def __init__(self, *args, **kwargs):
419
411
self ._refs = {}
420
412
super (GreenletPool , self ).__init__ (* args , ** kwargs )
421
413
422
- def __del__ (self ):
423
- # The 'hasattr' checks avoid TypeError during interpreter shutdown.
424
- for sock_info in self ._gr_id_to_sock .values ():
425
- if hasattr (sock_info , 'close' ):
426
- sock_info .close ()
427
-
428
- for sock_info in self .sockets :
429
- if hasattr (sock_info , 'close' ):
430
- sock_info .close ()
431
-
432
414
# Overrides
433
415
def _set_request_state (self , sock_info ):
434
416
current = greenlet .getcurrent ()
0 commit comments