Skip to content

Commit

Permalink
Fix a crash when an Executor wrapped fork exit.
Browse files Browse the repository at this point in the history
Fix rails#51298

forking inside an Execution Wrapper crashes when running the completion callbacks.

Rails 7.0 was not Execution wrapping the `runner` command.
Rails 7.2 changed the definition of `active_connection?`, the new definition doesn't contain the bug.

Therefore forking inside a script intended to be run with the `runner` command on 7.1 crashes. (see rails#51298)
  • Loading branch information
JoeDupuis committed May 9, 2024
1 parent 0e14713 commit a665022
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/query_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def self.run
end

def self.complete(pools)
pools.each { |pool| pool.disable_query_cache! }
pools.each { |pool| pool.disable_query_cache! unless pool.discarded? }

ActiveRecord::Base.connection_handler.each_connection_pool do |pool|
pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
Expand Down
31 changes: 31 additions & 0 deletions activerecord/test/cases/query_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,37 @@ def test_clear_query_cache_is_called_on_all_connections
ActiveRecord::Base.connection_pool.lock_thread = false
end

test "query cache callbacks exit gracefully from a fork" do
# Regression test for #51298
pid = nil
status = nil

# Run forking Rack app
middleware { |env|
pid = fork
_, status = Process.wait2(pid) if pid.present?
[200, {}, nil]
}.call({})


# Happy path, the fork exit 0 and the main process
# asserts on the exit status of the fork
if pid.nil?
exit 0
else
assert_equal 0, status.exitstatus
end
rescue
# Unhappy path, the fork exit 1.
# The main process should not reach here, but in case
# something's wrong we need to re-raise the error.
if pid.nil?
exit 1
else
raise
end
end

private
def with_temporary_connection_pool(&block)
pool_config = ActiveRecord::Base.connection.pool.pool_config
Expand Down

0 comments on commit a665022

Please sign in to comment.