Skip to content

Commit 27f5e83

Browse files
authored
Fail hard on using closed driver (#1182)
* Raise `DriverError` on using the closed driver. * Calling `driver.close()` again is now a no-op.
1 parent 2659cb3 commit 27f5e83

File tree

7 files changed

+125
-66
lines changed

7 files changed

+125
-66
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ See also https://github.com/neo4j/neo4j-python-driver/wiki for a full changelog.
8888
- `transient_errors`
8989
- Raise `ConfigurationError` instead of ignoring the routing context (URI query parameters) when creating a direct
9090
driver ("bolt[+s[sc]]://" scheme).
91+
- Change behavior of closed drivers:
92+
- Raise `DriverError` on using the closed driver.
93+
- Calling `driver.close()` again is now a no-op.
9194

9295

9396
## Version 5.28

docs/source/api.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ Closing a driver will immediately shut down all connections in the pool.
370370
:returns: the result of the ``result_transformer_``
371371
:rtype: T
372372

373+
:raises DriverError: if the driver has been closed.
374+
373375
.. versionadded:: 5.5
374376

375377
.. versionchanged:: 5.8
@@ -384,6 +386,9 @@ Closing a driver will immediately shut down all connections in the pool.
384386
The ``query_`` parameter now also accepts a :class:`.Query` object
385387
instead of only :class:`str`.
386388

389+
.. versionchanged:: 6.0
390+
Raise :exc:`DriverError` if the driver has been closed.
391+
387392

388393
.. _driver-configuration-ref:
389394

docs/source/async_api.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ Closing a driver will immediately shut down all connections in the pool.
352352
:returns: the result of the ``result_transformer_``
353353
:rtype: T
354354

355+
:raises DriverError: if the driver has been closed.
356+
355357
.. versionadded:: 5.5
356358

357359
.. versionchanged:: 5.8
@@ -366,6 +368,9 @@ Closing a driver will immediately shut down all connections in the pool.
366368
The ``query_`` parameter now also accepts a :class:`.Query` object
367369
instead of only :class:`str`.
368370

371+
.. versionchanged:: 6.0
372+
Raise :exc:`DriverError` if the driver has been closed.
373+
369374

370375
.. _async-driver-configuration-ref:
371376

src/neo4j/_async/driver.py

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@
8282
AsyncClientCertificateProvider,
8383
ClientCertificate,
8484
)
85-
from ..exceptions import Neo4jError
85+
from ..exceptions import (
86+
DriverError,
87+
Neo4jError,
88+
)
8689
from .auth_management import _AsyncStaticClientCertificateProvider
8790
from .bookmark_manager import (
8891
AsyncNeo4jBookmarkManager,
@@ -526,13 +529,7 @@ def __del__(
526529

527530
def _check_state(self):
528531
if self._closed:
529-
# TODO: 6.0 - raise the error
530-
# raise DriverError("Driver closed")
531-
deprecation_warn(
532-
"Using a driver after it has been closed is deprecated. "
533-
"Future versions of the driver will raise an error.",
534-
stack_level=3,
535-
)
532+
raise DriverError("Driver closed")
536533

537534
@property
538535
def encrypted(self) -> bool:
@@ -584,6 +581,11 @@ def session(self, **config) -> AsyncSession:
584581
key-word arguments.
585582
586583
:returns: new :class:`neo4j.AsyncSession` object
584+
585+
:raises DriverError: if the driver has been closed.
586+
587+
.. versionchanged:: 6.0
588+
Raise :exc:`DriverError` if the driver has been closed.
587589
"""
588590
if "warn_notification_severity" in config:
589591
# Would work just fine, but we don't want to introduce yet
@@ -621,9 +623,8 @@ async def close(self) -> None:
621623
spawned from it (such as sessions or transactions) while calling
622624
this method. Failing to do so results in unspecified behavior.
623625
"""
624-
# TODO: 6.0 - NOOP if already closed
625-
# if self._closed:
626-
# return
626+
if self._closed:
627+
return
627628
try:
628629
await self._pool.close()
629630
except asyncio.CancelledError:
@@ -887,6 +888,8 @@ async def example(driver: neo4j.AsyncDriver) -> neo4j.Record::
887888
:returns: the result of the ``result_transformer_``
888889
:rtype: T
889890
891+
:raises DriverError: if the driver has been closed.
892+
890893
.. versionadded:: 5.5
891894
892895
.. versionchanged:: 5.8
@@ -900,6 +903,9 @@ async def example(driver: neo4j.AsyncDriver) -> neo4j.Record::
900903
.. versionchanged:: 5.15
901904
The ``query_`` parameter now also accepts a :class:`.Query` object
902905
instead of only :class:`str`.
906+
907+
.. versionchanged:: 6.0
908+
Raise :exc:`DriverError` if the driver has been closed.
903909
''' # noqa: E501 example code isn't too long
904910
self._check_state()
905911
invalid_kwargs = [
@@ -1043,11 +1049,15 @@ async def verify_connectivity(self, **config) -> None:
10431049
:raises Exception: if the driver cannot connect to the remote.
10441050
Use the exception to further understand the cause of the
10451051
connectivity problem.
1052+
:raises DriverError: if the driver has been closed.
10461053
10471054
.. versionchanged:: 5.0
10481055
The undocumented return value has been removed.
10491056
If you need information about the remote server, use
10501057
:meth:`get_server_info` instead.
1058+
1059+
.. versionchanged:: 6.0
1060+
Raise :exc:`DriverError` if the driver has been closed.
10511061
"""
10521062
self._check_state()
10531063
if config:
@@ -1120,8 +1130,12 @@ async def get_server_info(self, **config) -> ServerInfo:
11201130
:raises Exception: if the driver cannot connect to the remote.
11211131
Use the exception to further understand the cause of the
11221132
connectivity problem.
1133+
:raises DriverError: if the driver has been closed.
11231134
11241135
.. versionadded:: 5.0
1136+
1137+
.. versionchanged:: 6.0
1138+
Raise :exc:`DriverError` if the driver has been closed.
11251139
"""
11261140
self._check_state()
11271141
if config:
@@ -1136,15 +1150,20 @@ async def supports_multi_db(self) -> bool:
11361150
"""
11371151
Check if the server or cluster supports multi-databases.
11381152
1139-
:returns: Returns true if the server or cluster the driver connects to
1140-
supports multi-databases, otherwise false.
1141-
11421153
.. note::
11431154
Feature support query based solely on the Bolt protocol version.
11441155
The feature might still be disabled on the server side even if this
11451156
function return :data:`True`. It just guarantees that the driver
11461157
won't throw a :exc:`.ConfigurationError` when trying to use this
11471158
driver feature.
1159+
1160+
:returns: Returns true if the server or cluster the driver connects to
1161+
supports multi-databases, otherwise false.
1162+
1163+
:raises DriverError: if the driver has been closed.
1164+
1165+
.. versionchanged:: 6.0
1166+
Raise :exc:`DriverError` if the driver has been closed.
11481167
"""
11491168
self._check_state()
11501169
session_config = self._read_session_config({})
@@ -1212,10 +1231,14 @@ async def verify_authentication(
12121231
:raises Exception: if the driver cannot connect to the remote.
12131232
Use the exception to further understand the cause of the
12141233
connectivity problem.
1234+
:raises DriverError: if the driver has been closed.
12151235
12161236
.. versionadded:: 5.8
12171237
12181238
.. versionchanged:: 5.14 Stabilized from experimental.
1239+
1240+
.. versionchanged:: 6.0
1241+
Raise :exc:`DriverError` if the driver has been closed.
12191242
"""
12201243
self._check_state()
12211244
if config:
@@ -1245,18 +1268,23 @@ async def supports_session_auth(self) -> bool:
12451268
"""
12461269
Check if the remote supports connection re-authentication.
12471270
1248-
:returns: Returns true if the server or cluster the driver connects to
1249-
supports re-authentication of existing connections, otherwise
1250-
false.
1251-
12521271
.. note::
12531272
Feature support query based solely on the Bolt protocol version.
12541273
The feature might still be disabled on the server side even if this
12551274
function return :data:`True`. It just guarantees that the driver
12561275
won't throw a :exc:`.ConfigurationError` when trying to use this
12571276
driver feature.
12581277
1278+
:returns: Returns true if the server or cluster the driver connects to
1279+
supports re-authentication of existing connections, otherwise
1280+
false.
1281+
1282+
:raises DriverError: if the driver has been closed.
1283+
12591284
.. versionadded:: 5.8
1285+
1286+
.. versionchanged:: 6.0
1287+
Raise :exc:`DriverError` if the driver has been closed.
12601288
"""
12611289
self._check_state()
12621290
session_config = self._read_session_config({})

src/neo4j/_sync/driver.py

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

0 commit comments

Comments
 (0)