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

Don't fail lock release if the connection was disconnected #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions lib/with_advisory_lock/postgresql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def release_lock
pg_function = "pg_advisory_unlock#{shared ? '_shared' : ''}"
execute_successful?(pg_function)
rescue ActiveRecord::StatementInvalid => e
# If something goes real bad, pg will close the connection. in that case the lock is no longer held
return if e.message =~ /PG::ConnectionBad:/
raise unless e.message =~ / ERROR: +current transaction is aborted,/

begin
Expand Down
6 changes: 6 additions & 0 deletions test/lock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@

thread_with_lock.kill
end

it 'handles lost connection gracefully' do
Tag.with_advisory_lock(lock_name) { Tag.connection.disconnect! }

assert_nil(Tag.current_advisory_lock)
end
end

describe '.with_advisory_lock!' do
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def env_db
module MiniTest
class Spec
before do
ActiveRecord::Base.establish_connection
ENV['FLOCK_DIR'] = Dir.mktmpdir
Tag.delete_all
TagAudit.delete_all
Expand Down