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

Possible race condition in SQLitePooledConnection #821

Open
nicolatimeus opened this issue Dec 20, 2022 · 6 comments
Open

Possible race condition in SQLitePooledConnection #821

nicolatimeus opened this issue Dec 20, 2022 · 6 comments
Labels
bug Something isn't working

Comments

@nicolatimeus
Copy link

Hi all,

I've noticed that at

listeners.get(i).connectionClosed(event);

the ConnectionEventListener.connectionClosed() method is called and then the subsequent lines may perform modifications on the released connection:

Should these modifications be moved before the invocation of ConnectionEventListener.connectionClosed()? Otherwise the client code may start reusing the released connection concurrently while they are being performed.

@gotson
Copy link
Collaborator

gotson commented Dec 21, 2022

Would you be able to provide an example project or unit test that would highlight the problem maybe ?

@nicolatimeus
Copy link
Author

Sure, I have created the following project:

https://github.com/nicolatimeus/sqlite-test/blob/main/src/main/java/com/github/nicolatimeus/App.java

On my machine execution stops after a few iterations with exceptions like the following:

org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (cannot rollback - no transaction is active)
        at org.sqlite.core.DB.newSQLException(DB.java:1135)
        at org.sqlite.core.DB.newSQLException(DB.java:1146)
        at org.sqlite.core.DB.throwex(DB.java:1106)
        at org.sqlite.core.DB.exec(DB.java:198)
        at org.sqlite.SQLiteConnection.rollback(SQLiteConnection.java:455)
        at org.sqlite.javax.SQLitePooledConnection$1.invoke(SQLitePooledConnection.java:110)
        at com.sun.proxy.$Proxy0.close(Unknown Source)
        at com.github.nicolatimeus.App.worker(App.java:71)
        at com.github.nicolatimeus.App.lambda$0(App.java:94)
        at java.lang.Thread.run(Thread.java:748)

@gotson
Copy link
Collaborator

gotson commented Jan 4, 2023

Thanks, i have the same error on my machine, and managed to set your sample as a unit test. I'm not sure about the fix though.

Should these modifications be moved before the invocation of ConnectionEventListener.connectionClosed()? Otherwise the client code may start reusing the released connection concurrently while they are being performed.

This doesn't fix the issue.

@gotson gotson added bug Something isn't working and removed triage labels Jan 4, 2023
@nicolatimeus
Copy link
Author

Thank you for testing this out.

I'm experimenting with change [1] that seems to fix the issue for me, other than reordering the statements in the close method I have added an extra check to avoid running the close logic twice during SQLitePooledConnection.getConnection().

[1] nicolatimeus@6a0de0a

@salvatore-coppola
Copy link

@gotson any news on that ?

@gotson
Copy link
Collaborator

gotson commented Jan 18, 2023

@gotson any news on that ?

I am not working on this, PRs are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants