A platform for community discussion. Free, open, simple.
MT: Keep a forked worker from inheriting an unguarded source connection
Gerhard Schlagerdf3084ccfd9b
diff --git a/migrations/converters/lib/migrations/converters/adapter/postgres.rb b/migrations/converters/lib/migrations/converters/adapter/postgres.rbindex 0401ad8b51f..30812ff6087 100644--- a/migrations/converters/lib/migrations/converters/adapter/postgres.rb+++ b/migrations/converters/lib/migrations/converters/adapter/postgres.rb@@ -12,12 +12,18 @@ module Migrationsenddef initialize(settings)- @connection = PG::Connection.new(settings)- @connection.type_map_for_results = PG::BasicTypeMapForResults.new(@connection)- @connection.field_name_type = :symbol- configure_connection-@fork_hook = ForkManager.after_fork_child { discard! }++ begin+ @connection = PG::Connection.new(settings)+ @connection.type_map_for_results = PG::BasicTypeMapForResults.new(@connection)+ @connection.field_name_type = :symbol+ configure_connection+ rescue StandardError+ ForkManager.remove_after_fork_child(@fork_hook)+ @fork_hook = nil+ raise+ endenddef exec(sql)diff --git a/migrations/converters/spec/lib/migrations/converters/adapter/postgres_spec.rb b/migrations/converters/spec/lib/migrations/converters/adapter/postgres_spec.rbindex 2a2e8705550..e15a1870c07 100644--- a/migrations/converters/spec/lib/migrations/converters/adapter/postgres_spec.rb+++ b/migrations/converters/spec/lib/migrations/converters/adapter/postgres_spec.rb@@ -39,6 +39,18 @@ RSpec.describe Migrations::Converters::Adapter::Postgres doendend+ it "registers its fork hook before it opens the socket" do+ hooks_at_connect = nil+ allow(PG::Connection).to receive(:new) do+ hooks_at_connect = Migrations::ForkManager.hook_count+ connection+ end++ create_adapter { |adapter| adapter.close }++ expect(hooks_at_connect).to eq(1)+ end+it "implements the whole PartitionSource interface" docreate_adapter do |adapter|expect(Migrations::Conversion::PartitionSource.missing_from(adapter)).to be_emptydiff --git a/migrations/core/lib/migrations/common/fork_manager.rb b/migrations/core/lib/migrations/common/fork_manager.rbindex 602f65c0639..39f13205dd2 100644--- a/migrations/core/lib/migrations/common/fork_manager.rb+++ b/migrations/core/lib/migrations/common/fork_manager.rb@@ -81,20 +81,11 @@ module Migrations# In a batch the parent-side hooks already ran around the whole batch.execute_parent = !Thread.current[BATCHED_FORKS_KEY]- # Snapshot the child hooks under the lock for a consistent list, but fork- # outside it so the child can't inherit the mutex held.- child_hooks =- if execute_parent- @mutex.synchronize do- run_before_fork_hooks- @after_fork_child_hooks.dup- end- else- @mutex.synchronize { @after_fork_child_hooks.dup }- end+ @mutex.synchronize { run_before_fork_hooks } if execute_parentpid =Process.fork do+ child_hooks = @mutex.synchronize { @after_fork_child_hooks.dup }child_hooks.each(&:call)yieldenddiff --git a/migrations/core/spec/lib/common/fork_manager_spec.rb b/migrations/core/spec/lib/common/fork_manager_spec.rbindex 50eaea56ec0..81acc815d50 100644--- a/migrations/core/spec/lib/common/fork_manager_spec.rb+++ b/migrations/core/spec/lib/common/fork_manager_spec.rb@@ -27,6 +27,22 @@ RSpec.describe Migrations::ForkManager doread_io.closeend+ # A source connection registers its hook, then opens its socket. If another+ # thread forks in between, the child has to run that hook, or the socket it+ # inherits stays open until its exit ends the parent's session too.+ it "runs a hook registered after the fork began but before the process split" do+ read_io, write_io = IO.pipe+ allow(Process).to receive(:_fork).and_wrap_original do |original, *args|+ described_class.after_fork_child { write_io.write("late hook") }+ original.call(*args)+ end++ Process.waitpid(described_class.fork {})+ write_io.close++ expect(read_io.read).to eq("late hook")+ end+it "returns the hook so it can be removed again" dohook = described_class.after_fork_child { raise "this hook should not run" }expect(described_class.hook_count).to eq(1)