Skip to content

Cancel current postgres query on rollback #42767

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

Merged
merged 2 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ def commit_db_transaction # :nodoc:

# Aborts a transaction.
def exec_rollback_db_transaction # :nodoc:
@connection.cancel unless @connection.transaction_status == PG::PQTRANS_IDLE
@connection.block
execute("ROLLBACK", "TRANSACTION")
end

Expand Down
19 changes: 19 additions & 0 deletions activerecord/test/cases/adapters/postgresql/transaction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ class Sample < ActiveRecord::Base
end
end

test "raises Interrupt when canceling statement via interrupt" do
start_time = Time.now
thread = Thread.new do
Sample.transaction do
Sample.connection.execute("SELECT pg_sleep(10)")
end
rescue Exception => e
e
end

sleep(0.5)
thread.raise Interrupt
thread.join
duration = Time.now - start_time

assert_instance_of Interrupt, thread.value
assert_operator duration, :<, 5
end

private
def with_warning_suppression
log_level = ActiveRecord::Base.connection.client_min_messages
Expand Down