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

Add Exqlite.Sqlite3.interrupt/1. #282

Merged
merged 3 commits into from
Apr 3, 2024

Conversation

ColaCheng
Copy link
Contributor

@ColaCheng ColaCheng commented Apr 3, 2024

We encounter the db connection can not be closed when a long-running query gets a timeout(Ecto environment) but the query is still running in the background and takes the CPU resource. For this function, we can manually trigger and force-stop the query. Then we can close and release the resource successfully.

Ref: https://www.sqlite.org/c3ref/interrupt.html

@warmwaffles
Copy link
Member

@ColaCheng yes I've been wanting to add this. Although it will not solve the issue of that long running query in the background because of the connection pool. If your connection pool is 2 then there is no guarantee that the interrupt will be routed to the problematic "connection".

That being said, thank you for implementing this and I'll get this merged. 😄

@warmwaffles warmwaffles merged commit cc13f1f into elixir-sqlite:main Apr 3, 2024
19 checks passed
@warmwaffles
Copy link
Member

Released under v0.21.0

@warmwaffles
Copy link
Member

warmwaffles commented Jan 20, 2025

@ColaCheng we are starting to see some weird issues with interrupt here in #315

Per the documentation:

** ^It is safe to call this routine from a thread different from the
** thread that is currently running the database operation.  But it
** is not safe to call this routine with a [database connection] that
** is closed or might close before sqlite3_interrupt() returns.

This means that if you call interrupt at just the right moment before the connection is torn down, it'll fall into undefined behavior territory. I believe this is only visible in tests due to how rapid connections are stood up and torn down.

Have you ran this in production and noticed any sort of sigaborts?

Edit: I'm fairly certain it's happening here

      Process.sleep(100)
      :ok = Sqlite3.interrupt(conn)

      :ok = Sqlite3.close(conn)

The close is called immediately after interrupt which I believe is what the docs call out for. I'll need to add documentation about calling close on the connection.

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

Successfully merging this pull request may close these issues.

2 participants