Skip to content
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
10 changes: 10 additions & 0 deletions lib/puma/plugin/solid_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def start_forked(launcher)
@solid_queue_pid = fork do
Thread.new { monitor_puma }
SolidQueue::Supervisor.start(mode: :fork)

# Same as Processes::Supervised#create_fork: skip at-exit
# finalization, which can deadlock on database handles when a
# thread was killed while inside a query
exit!(0)
end
end

Expand All @@ -43,6 +48,11 @@ def start_forked(launcher)
@solid_queue_pid = fork do
Thread.new { monitor_puma }
start_solid_queue(mode: :fork)

# Same as Processes::Supervised#create_fork: skip at-exit
# finalization, which can deadlock on database handles when a
# thread was killed while inside a query
exit!(0)
end
end

Expand Down
8 changes: 8 additions & 0 deletions lib/solid_queue/processes/supervised.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ def create_fork(&block)
fork do
register_signal_handlers
block.call

# Exit skipping at-exit hooks and finalizers, like Puma's cluster
# workers do: Ruby would finalize every object still alive in the
# fork, including SQLite database handles whose mutex can be left
# locked when a thread is killed while waiting in SQLite's busy
# handler, deadlocking the exit. Everything the process needs to do
# on shutdown has already run by now.
exit!(0)
end
end

Expand Down
4 changes: 0 additions & 4 deletions test/dummy/config/puma_async.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
#
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT") { 3000 }

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }
Expand Down
4 changes: 0 additions & 4 deletions test/dummy/config/puma_fork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
#
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT") { 3000 }

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }
Expand Down
29 changes: 29 additions & 0 deletions test/unit/supervised_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require "test_helper"

class SupervisedTest < ActiveSupport::TestCase
class FakeProcess
include SolidQueue::Processes::Supervised

def stop
end
end

test "forked processes exit immediately, without running inherited at-exit hooks" do
reader, writer = IO.pipe

pid = FakeProcess.new.send(:create_fork) do
at_exit { writer.write("at_exit ran") }
writer.write("block ran")
end

writer.close
_, status = Process.waitpid2(pid)

assert_equal 0, status.exitstatus
assert_equal "block ran", reader.read
ensure
reader.close
end
end
Loading