Skip to content

Commit 0e4d772

Browse files
authored
Don't close drivers & sessions in finalizer (#1183)
No longer implicitly closing drivers and sessions in `__del__()` (finalizer/destructor). Make sure to call `.close()` on them explicitly or use them in a `with` statement. A deprecation warning has been emitted in such cases before. Relying on `__del__` leads to hard to follow control flow and can result in surprising behavior as there is no guarantee when that the finalizer will be called if ever. A ResourceWarning is emitted instead. See also: https://docs.python.org/3/reference/datamodel.html#object.__del__
1 parent 27f5e83 commit 0e4d772

File tree

5 files changed

+8
-72
lines changed

5 files changed

+8
-72
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ See also https://github.com/neo4j/neo4j-python-driver/wiki for a full changelog.
9191
- Change behavior of closed drivers:
9292
- Raise `DriverError` on using the closed driver.
9393
- Calling `driver.close()` again is now a no-op.
94+
- No longer implicitly closing drivers and sessions in `__del__()` (finalizer/destructor).
95+
Make sure to call `.close()` on them explicitly or use them in a `with` statement.
9496

9597

9698
## Version 5.28

src/neo4j/_async/driver.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
SECURITY_TYPE_SELF_SIGNED_CERTIFICATE,
4040
TelemetryAPI,
4141
)
42-
from .._async_compat.util import AsyncUtil
4342
from .._conf import (
4443
Config,
4544
ConfigurationError,
@@ -509,23 +508,9 @@ async def __aexit__(self, exc_type, exc_value, traceback):
509508
def __del__(
510509
self,
511510
_unclosed_resource_warn=unclosed_resource_warn,
512-
_is_async_code=AsyncUtil.is_async_code,
513-
_deprecation_warn=deprecation_warn,
514511
):
515512
if not self._closed:
516513
_unclosed_resource_warn(self)
517-
# TODO: 6.0 - remove this
518-
if _is_async_code:
519-
return
520-
if not self._closed:
521-
_deprecation_warn(
522-
"Relying on AsyncDriver's destructor to close the session "
523-
"is deprecated. Please make sure to close the session. "
524-
"Use it as a context (`with` statement) or make sure to "
525-
"call `.close()` explicitly. Future versions of the "
526-
"driver will not close drivers automatically."
527-
)
528-
self.close()
529514

530515
def _check_state(self):
531516
if self._closed:

src/neo4j/_async/work/workspace.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@
2222
from ..._async_compat.util import AsyncUtil
2323
from ..._auth_management import to_auth_dict
2424
from ..._conf import WorkspaceConfig
25-
from ..._warnings import (
26-
deprecation_warn,
27-
unclosed_resource_warn,
28-
)
25+
from ..._warnings import unclosed_resource_warn
2926
from ...api import Bookmarks
3027
from ...exceptions import (
3128
ServiceUnavailable,
3229
SessionError,
33-
SessionExpired,
3430
)
3531
from .._debug import AsyncNonConcurrentMethodChecker
3632
from ..io import (
@@ -74,29 +70,15 @@ def __init__(self, pool, config):
7470
self._closed = False
7571
super().__init__()
7672

73+
# Copy globals as function locals to make sure that they are available
74+
# during Python shutdown when the Session is destroyed.
7775
def __del__(
7876
self,
7977
_unclosed_resource_warn=unclosed_resource_warn,
80-
_is_async_code=AsyncUtil.is_async_code,
81-
_deprecation_warn=deprecation_warn,
8278
):
8379
if self._closed:
8480
return
8581
_unclosed_resource_warn(self)
86-
# TODO: 6.0 - remove this
87-
if _is_async_code:
88-
return
89-
try:
90-
_deprecation_warn(
91-
"Relying on AsyncSession's destructor to close the session "
92-
"is deprecated. Please make sure to close the session. Use it "
93-
"as a context (`with` statement) or make sure to call "
94-
"`.close()` explicitly. Future versions of the driver will "
95-
"not close sessions automatically."
96-
)
97-
self.close()
98-
except (OSError, ServiceUnavailable, SessionExpired):
99-
pass
10082

10183
async def __aenter__(self) -> AsyncWorkspace:
10284
return self

src/neo4j/_sync/driver.py

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/neo4j/_sync/work/workspace.py

Lines changed: 3 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)