Skip to content
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

Query execution threads are not interrupted/terminated when the driver is closed (neo4j driver version 5.9.0 and above) #1436

Closed
aevangelista81 opened this issue Jun 26, 2023 · 8 comments

Comments

@aevangelista81
Copy link

Hi guys,
It seems that closing a driver via the Driver.close() method from another thread than the one owning the driver instance will not interrupt long running transactions busy on the owning thread. The issue also shows that the owning thread will never finish, even after the transaction query should have been completed.

Probably a piece of code is better to explain the behaviour :

package org.example;

import org.neo4j.driver.AuthToken;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Config;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.SessionConfig;
import org.neo4j.driver.TransactionConfig;

import java.time.Duration;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;


public class Main {

    static Driver driver;

    public static void main(String[] args) {
        System.out.println("PID: " + ProcessHandle.current().pid());


        Config config = Config.builder()
                .withMaxConnectionLifetime(30, TimeUnit.MINUTES)
                .withMaxConnectionPoolSize(50)
                .withConnectionAcquisitionTimeout(2, TimeUnit.MINUTES)
                .build();

        String boltUri = "bolt://localhost:7687";
        AuthToken authTokens = AuthTokens.basic("neo4j", "password");
        driver = GraphDatabase.driver(boltUri, authTokens, config);
        final Timer timer = new Timer("Closing Driver");

        final TimerTask task = new TimerTask() {

            @Override
            public void run() {
                System.out.println("Closing Driver");
                //Here I'm excepting that the thread running the query (main application thread) will be interrupted and this program will finish the execution, but this will never happen.
                driver.close();
                timer.cancel(); // stop timer after execution

            }
        };
        timer.schedule(task, 10000);

        SessionConfig sessionConfig = SessionConfig.defaultConfig();

        try (Session session = driver.session(sessionConfig)) {
            session.executeWrite((tx) -> {
                //Simulate a long running transaction
                Result result = tx.run("call apoc.util.sleep(30000)\n" + "MATCH (n) RETURN n");
                result.forEachRemaining(r -> {
                    //Do nothing
                });
                return null;
            }, TransactionConfig.builder().withTimeout(Duration.ofMinutes(2)).build());
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
            throw new RuntimeException(e);
        }

        System.out.println("Finished main thread");
    }
}

In this specific case the previous application will never end.

Thanks in advance,
Andrea Evangelista

@ikwattro
Copy link
Contributor

👀

@ikwattro
Copy link
Contributor

For info, this issue appears since version 4.2.2, downgrading to 4.2.1 shows the test above do what's expected.

@AndyHeap-NeoTech
Copy link

AndyHeap-NeoTech commented Jun 28, 2023

Thank you for the bug report. We will start an investigation into this.

@ikwattro Is it an option to upgrade the driver? 4.4 is our LTS version, and the latest is 5.9. We recommend upgrading to the latest version whenever possible. Any fixes required will go onto the 4.4 LTS and the next release of the 5.x driver.

@ikwattro
Copy link
Contributor

ikwattro commented Jun 28, 2023 via email

@AndyHeap-NeoTech
Copy link

Fix now in place and released in driver version 5.10

@ikwattro
Copy link
Contributor

Thanks a lot for the fast resolution !

@injectives
Copy link
Contributor

Thanks!

The issue should be fixed in 5.10.0 and 4.4.12.

@aevangelista81
Copy link
Author

Thanks a lot, I can confirm the 5.10.0 release fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants