neo4j / neo4j-dotnet-driver

Neo4j Bolt driver for .NET
Apache License 2.0
222 stars 69 forks source link

Driver.ExecuteAsync not cancelling/killing the query #804

Closed mfornaris closed 1 month ago

mfornaris commented 1 month ago

ExecuteAsync will not cancel the query until it finishes running

To Reproduce simple controller action:

[HttpPost]
public async Task<IActionResult> Neo4J(CancellationToken cancellationToken)
{
    try
    {
        // gets driver's instance
        var driver = Driver.Get(configuration);
        try
        {
            // wait for executing the query
            await driver
                .ExecutableQuery("MATCH...")
                .ExecuteAsync(cancellationToken);
        }
        finally
        {
            // dispose driver
            driver.Dispose();
        }
    }
    catch (Exception ex)
    {
        // catch exception
        Console.WriteLine(ex);
    }
    return Ok();   
}

post from javascript using some sort of:

const controller = new AbortController()
const signal = controller.signal;

fetch(url, {
        signal,
        method: 'POST',
        headers: {
"Content-Type": "application/json",
...

and cancelling the request for testing purposes

setTimeout(() => controller.abort(), 3000);

the query takes about 20 seconds to execute without the cancellation path, so here is the execution sequence:

Expected behavior Version Info (please complete the following information):

Additional context if in line 230 https://github.com/neo4j/neo4j-dotnet-driver/blob/5.0/Neo4j.Driver/Neo4j.Driver/Internal/Driver.cs#L230 instead of

var session = Session(x => ApplyConfig(config, x), false);

we change it to

var session = Session(x => ApplyConfig(config, x), true);

everything will work as (at least I) expected.

Is there any resolution to this or another way of cancelling the query execution as soon as possible?