Skip to content

Explicit AsyncTransaction randomly ignores specified timeout #1028

@v-v-d

Description

@v-v-d

Bug Report

First of all, I want to thank you for the amazing Neo4j driver for Python with full asyncio support. It's really very cool and performs wonderfully! Thank you!

Now to the point. While using the driver, I've noticed that if explicit transactions are used and a timeout argument is passed at their initialization, the driver does not always respect this timeout. This leads to transactions containing queries that run for minutes when a timeout of just a few seconds is specified.

I have prepared a simple pytest test that confirms the above. You can run it via docker-compose up --build
neo4j_test.zip

Could you please tell me if this is a bug or expected behavior? Or maybe I'm doing something wrong?

This is a snippet of the test:

from typing import LiteralString, cast

import neo4j
import pytest
from _pytest.fixtures import SubRequest
from neo4j import AsyncGraphDatabase


@pytest.fixture()
async def driver() -> neo4j.AsyncDriver:
    async with AsyncGraphDatabase.driver(
        uri="neo4j://neo4j:7687",
        auth=("neo4j", "top-secret"),
    ) as driver:
        yield driver
        await driver.close()


@pytest.fixture()
async def session(driver: neo4j.AsyncDriver) -> neo4j.AsyncSession:
    session = driver.session(database="neo4j")
    yield session
    await session.close()


@pytest.fixture()
async def trx(request: SubRequest, session: neo4j.AsyncSession) -> neo4j.AsyncTransaction:
    trx = await session.begin_transaction(timeout=request.param)
    yield trx
    await trx.close()


@pytest.mark.parametrize("trx", [0.1, 0.3, 0.6, 0.9], indirect=True)
async def test_trx_timeout(trx: neo4j.AsyncTransaction) -> None:
    with pytest.raises(neo4j.exceptions.ClientError):
        await trx.run(query=cast(LiteralString, "CALL apoc.util.sleep(1000)"))
        await trx.commit()

and this is one of the outputs:

============================= test session starts ==============================
platform linux -- Python 3.11.6, pytest-8.1.1, pluggy-1.4.0
rootdir: /app/code
plugins: asyncio-0.23.6
asyncio: mode=Mode.AUTO
collected 4 items

app/test_neo4j.py .F.F                                                   [100%]

=================================== FAILURES ===================================
____________________________ test_trx_timeout[0.3] _____________________________

trx = <neo4j._async.work.transaction.AsyncTransaction object at 0xffff860d4a50>

    @pytest.mark.parametrize("trx", [0.1, 0.3, 0.6, 0.9], indirect=True)
    async def test_trx_timeout(trx: neo4j.AsyncTransaction) -> None:
>       with pytest.raises(neo4j.exceptions.ClientError):
E       Failed: DID NOT RAISE <class 'neo4j.exceptions.ClientError'>

app/test_neo4j.py:35: Failed
____________________________ test_trx_timeout[0.9] _____________________________

trx = <neo4j._async.work.transaction.AsyncTransaction object at 0xffff860e9c50>

    @pytest.mark.parametrize("trx", [0.1, 0.3, 0.6, 0.9], indirect=True)
    async def test_trx_timeout(trx: neo4j.AsyncTransaction) -> None:
>       with pytest.raises(neo4j.exceptions.ClientError):
E       Failed: DID NOT RAISE <class 'neo4j.exceptions.ClientError'>

app/test_neo4j.py:35: Failed
=========================== short test summary info ============================
FAILED app/test_neo4j.py::test_trx_timeout[0.3] - Failed: DID NOT RAISE <clas...
FAILED app/test_neo4j.py::test_trx_timeout[0.9] - Failed: DID NOT RAISE <clas...
========================= 2 failed, 2 passed in 3.63s ==========================

My Environment

Python Version: 3.11 (python3.11-slim docker image)
Driver Version: 5.18.0 Neo4j Bolt driver for Python
Server Version and Edition: 5.18.0 community (from neo4j:5.18.0-community docker image)
Operating System: debian bookworm (from python3.11-slim docker image)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions