Skip to content

Fail hard on using closed driver #1182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ See also https://github.com/neo4j/neo4j-python-driver/wiki for a full changelog.
- `transient_errors`
- Raise `ConfigurationError` instead of ignoring the routing context (URI query parameters) when creating a direct
driver ("bolt[+s[sc]]://" scheme).
- Change behavior of closed drivers:
- Raise `DriverError` on using the closed driver.
- Calling `driver.close()` again is now a no-op.


## Version 5.28
Expand Down
5 changes: 5 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ Closing a driver will immediately shut down all connections in the pool.
:returns: the result of the ``result_transformer_``
:rtype: T

:raises DriverError: if the driver has been closed.

.. versionadded:: 5.5

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

.. versionchanged:: 6.0
Raise :exc:`DriverError` if the driver has been closed.


.. _driver-configuration-ref:

Expand Down
5 changes: 5 additions & 0 deletions docs/source/async_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ Closing a driver will immediately shut down all connections in the pool.
:returns: the result of the ``result_transformer_``
:rtype: T

:raises DriverError: if the driver has been closed.

.. versionadded:: 5.5

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

.. versionchanged:: 6.0
Raise :exc:`DriverError` if the driver has been closed.


.. _async-driver-configuration-ref:

Expand Down
64 changes: 46 additions & 18 deletions src/neo4j/_async/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@
AsyncClientCertificateProvider,
ClientCertificate,
)
from ..exceptions import Neo4jError
from ..exceptions import (
DriverError,
Neo4jError,
)
from .auth_management import _AsyncStaticClientCertificateProvider
from .bookmark_manager import (
AsyncNeo4jBookmarkManager,
Expand Down Expand Up @@ -526,13 +529,7 @@ def __del__(

def _check_state(self):
if self._closed:
# TODO: 6.0 - raise the error
# raise DriverError("Driver closed")
deprecation_warn(
"Using a driver after it has been closed is deprecated. "
"Future versions of the driver will raise an error.",
stack_level=3,
)
raise DriverError("Driver closed")

@property
def encrypted(self) -> bool:
Expand Down Expand Up @@ -584,6 +581,11 @@ def session(self, **config) -> AsyncSession:
key-word arguments.

:returns: new :class:`neo4j.AsyncSession` object

:raises DriverError: if the driver has been closed.

.. versionchanged:: 6.0
Raise :exc:`DriverError` if the driver has been closed.
"""
if "warn_notification_severity" in config:
# Would work just fine, but we don't want to introduce yet
Expand Down Expand Up @@ -621,9 +623,8 @@ async def close(self) -> None:
spawned from it (such as sessions or transactions) while calling
this method. Failing to do so results in unspecified behavior.
"""
# TODO: 6.0 - NOOP if already closed
# if self._closed:
# return
if self._closed:
return
try:
await self._pool.close()
except asyncio.CancelledError:
Expand Down Expand Up @@ -887,6 +888,8 @@ async def example(driver: neo4j.AsyncDriver) -> neo4j.Record::
:returns: the result of the ``result_transformer_``
:rtype: T

:raises DriverError: if the driver has been closed.

.. versionadded:: 5.5

.. versionchanged:: 5.8
Expand All @@ -900,6 +903,9 @@ async def example(driver: neo4j.AsyncDriver) -> neo4j.Record::
.. versionchanged:: 5.15
The ``query_`` parameter now also accepts a :class:`.Query` object
instead of only :class:`str`.

.. versionchanged:: 6.0
Raise :exc:`DriverError` if the driver has been closed.
''' # noqa: E501 example code isn't too long
self._check_state()
invalid_kwargs = [
Expand Down Expand Up @@ -1043,11 +1049,15 @@ async def verify_connectivity(self, **config) -> None:
:raises Exception: if the driver cannot connect to the remote.
Use the exception to further understand the cause of the
connectivity problem.
:raises DriverError: if the driver has been closed.

.. versionchanged:: 5.0
The undocumented return value has been removed.
If you need information about the remote server, use
:meth:`get_server_info` instead.

.. versionchanged:: 6.0
Raise :exc:`DriverError` if the driver has been closed.
"""
self._check_state()
if config:
Expand Down Expand Up @@ -1120,8 +1130,12 @@ async def get_server_info(self, **config) -> ServerInfo:
:raises Exception: if the driver cannot connect to the remote.
Use the exception to further understand the cause of the
connectivity problem.
:raises DriverError: if the driver has been closed.

.. versionadded:: 5.0

.. versionchanged:: 6.0
Raise :exc:`DriverError` if the driver has been closed.
"""
self._check_state()
if config:
Expand All @@ -1136,15 +1150,20 @@ async def supports_multi_db(self) -> bool:
"""
Check if the server or cluster supports multi-databases.

:returns: Returns true if the server or cluster the driver connects to
supports multi-databases, otherwise false.

.. note::
Feature support query based solely on the Bolt protocol version.
The feature might still be disabled on the server side even if this
function return :data:`True`. It just guarantees that the driver
won't throw a :exc:`.ConfigurationError` when trying to use this
driver feature.

:returns: Returns true if the server or cluster the driver connects to
supports multi-databases, otherwise false.

:raises DriverError: if the driver has been closed.

.. versionchanged:: 6.0
Raise :exc:`DriverError` if the driver has been closed.
"""
self._check_state()
session_config = self._read_session_config({})
Expand Down Expand Up @@ -1212,10 +1231,14 @@ async def verify_authentication(
:raises Exception: if the driver cannot connect to the remote.
Use the exception to further understand the cause of the
connectivity problem.
:raises DriverError: if the driver has been closed.

.. versionadded:: 5.8

.. versionchanged:: 5.14 Stabilized from experimental.

.. versionchanged:: 6.0
Raise :exc:`DriverError` if the driver has been closed.
"""
self._check_state()
if config:
Expand Down Expand Up @@ -1245,18 +1268,23 @@ async def supports_session_auth(self) -> bool:
"""
Check if the remote supports connection re-authentication.

:returns: Returns true if the server or cluster the driver connects to
supports re-authentication of existing connections, otherwise
false.

.. note::
Feature support query based solely on the Bolt protocol version.
The feature might still be disabled on the server side even if this
function return :data:`True`. It just guarantees that the driver
won't throw a :exc:`.ConfigurationError` when trying to use this
driver feature.

:returns: Returns true if the server or cluster the driver connects to
supports re-authentication of existing connections, otherwise
false.

:raises DriverError: if the driver has been closed.

.. versionadded:: 5.8

.. versionchanged:: 6.0
Raise :exc:`DriverError` if the driver has been closed.
"""
self._check_state()
session_config = self._read_session_config({})
Expand Down
64 changes: 46 additions & 18 deletions src/neo4j/_sync/driver.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading