|
5 | 5 | import socket
|
6 | 6 | import ssl
|
7 | 7 | import sys
|
| 8 | +import warnings |
8 | 9 | import weakref
|
9 | 10 | from abc import abstractmethod
|
10 | 11 | from itertools import chain
|
@@ -204,6 +205,24 @@ def __init__(
|
204 | 205 | raise ConnectionError("protocol must be either 2 or 3")
|
205 | 206 | self.protocol = protocol
|
206 | 207 |
|
| 208 | + def __del__(self, _warnings: Any = warnings): |
| 209 | + # For some reason, the individual streams don't get properly garbage |
| 210 | + # collected and therefore produce no resource warnings. We add one |
| 211 | + # here, in the same style as those from the stdlib. |
| 212 | + if getattr(self, "_writer", None): |
| 213 | + _warnings.warn( |
| 214 | + f"unclosed Connection {self!r}", ResourceWarning, source=self |
| 215 | + ) |
| 216 | + self._close() |
| 217 | + |
| 218 | + def _close(self): |
| 219 | + """ |
| 220 | + Internal method to silently close the connection without waiting |
| 221 | + """ |
| 222 | + if self._writer: |
| 223 | + self._writer.close() |
| 224 | + self._writer = self._reader = None |
| 225 | + |
207 | 226 | def __repr__(self):
|
208 | 227 | repr_args = ",".join((f"{k}={v}" for k, v in self.repr_pieces()))
|
209 | 228 | return f"{self.__class__.__name__}<{repr_args}>"
|
@@ -1017,7 +1036,7 @@ def __repr__(self):
|
1017 | 1036 |
|
1018 | 1037 | def reset(self):
|
1019 | 1038 | self._available_connections = []
|
1020 |
| - self._in_use_connections = set() |
| 1039 | + self._in_use_connections = weakref.WeakSet() |
1021 | 1040 |
|
1022 | 1041 | def can_get_connection(self) -> bool:
|
1023 | 1042 | """Return True if a connection can be retrieved from the pool."""
|
|
0 commit comments