avoiding caller exit errors
ruslandoga opened this issue · comments
👋
When a caller starts a query and doesn't finish it before exiting, db_connection
pool terminates the db_connection
process.
iex(37)> pid = spawn(fn -> Repo.query("select sleep(3)") end)
#PID<0.3079.0>
iex(38)> Process.exit(pid, :shutdown) # shutdown the spawned process within 3sec
true
iex(39)> [info] Clickhousex.Protocol (#PID<0.896.0>) disconnected: ** (DBConnection.ConnectionError) client #PID<0.3079.0> exited
Would it be possible for db_connection
to add an optional "cancel query" callback for adapters that support it? Stopping the process completely seems a bit suboptimal and noisy, but I understand the reasoning here as it's similar to linking and probably solves many edge cases.
For context, this error gets logged when users switch between the month M
and year Y
periods on plausible.io faster than responses from clickhouse can arrive:
- prev http requests get cancelled
- caller processes
:cowboy_stream_h.request_process
exit - DB connection processes get terminated
Yes, this is something we could do but note this is complicated because you may not know what was written on the socket when the query is aborted. For example, if you are in the middle of writing a query and aborts, then writing another query will leave you with mixed state. If you can guarantee this won't ever be the case for you, we could accept a PR that adds an optional callback that configures what happens in such cases (if such callback is not implemented, then we fallback to the current behaviour).
@josevalim Thank you for the information! I'll try and explore if we can ensure no incomplete requests.
And if I'm successful, I'll either reopen this issue or open a PR :)