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

Improve AR connection fork safety #31173

Merged
merged 1 commit into from Nov 25, 2017

Conversation

Projects
None yet
5 participants
@matthewd
Member

matthewd commented Nov 17, 2017

Use whatever adapter-provided means we have available to ensure forked children don't send quit/shutdown/goodbye messages to the server on connections that belonged to their parent.

  • mysql2 provides a method specifically for this purpose: brianmario/mysql2#684
  • pg is not quite so helpful, but it does expose a socket_io, which is close enough.
  • I think sqlite3 is immune to this problem just because there is no server to confuse.

I believe this should eliminate the need for advice like this, along with much of what's currently described in #29807.

yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Nov 17, 2017

Show outdated Hide outdated activerecord/test/cases/connection_adapters/connection_handler_test.rb Outdated
@jeremy

jeremy approved these changes Nov 17, 2017

Nice 👍

pools.values.compact.each(&:discard!) unless pid == Process.pid
end
end

This comment has been minimized.

@jeremy

jeremy Nov 17, 2017

Member

:nodoc: these?

@jeremy

jeremy Nov 17, 2017

Member

:nodoc: these?

Improve AR connection fork safety
Use whatever adapter-provided means we have available to ensure forked
children don't send quit/shutdown/goodbye messages to the server on
connections that belonged to their parent.

@matthewd matthewd merged commit 3313912 into rails:master Nov 25, 2017

2 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@matthewd matthewd deleted the matthewd:connection-fork-safety branch Nov 25, 2017

@kaspth

This comment has been minimized.

Show comment
Hide comment
@kaspth

kaspth Nov 25, 2017

Member

@matthewd nice! I guess we should remove

# If you are preloading your application and using Active Record, it's
# recommended that you close any connections to the database before workers
# are forked to prevent connection leakage.
#
# before_fork do
# ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
# end
# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted, this block will be run. If you are using the `preload_app!`
# option, you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, as Ruby
# cannot share connections between processes.
#
# on_worker_boot do
# ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
# end
#
from the default Puma template.

Member

kaspth commented Nov 25, 2017

@matthewd nice! I guess we should remove

# If you are preloading your application and using Active Record, it's
# recommended that you close any connections to the database before workers
# are forked to prevent connection leakage.
#
# before_fork do
# ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
# end
# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted, this block will be run. If you are using the `preload_app!`
# option, you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, as Ruby
# cannot share connections between processes.
#
# on_worker_boot do
# ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
# end
#
from the default Puma template.

yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Nov 27, 2017

yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Nov 27, 2017

yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Nov 27, 2017

yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Nov 27, 2017

kamipo added a commit to kamipo/rails that referenced this pull request Nov 27, 2017

Drop mysql2 version less than 0.4.3 to guarantee fork safety
Since rails#31173, mysql2 adapter depends on `automatic_close` which is
introduced since mysql2 0.4.3. So the adapter with the mysql2 version
before doesn't work with fork now.

```
% ARCONN=mysql2 be ruby -w -Itest test/cases/connection_adapters/connection_handler_test.rb -n test_forked_child_doesnt_mangle_parent_connection
Using mysql2
Run options: -n test_forked_child_doesnt_mangle_parent_connection --seed 19988

/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb:108:in `discard!': undefined method `automatic_close=' for #<Mysql2::Client:0x00007fedaa91dfd0> (NoMethodError)
```

This drops mysql2 version less than 0.4.3 to guarantee fork safety.

kamipo added a commit that referenced this pull request Nov 27, 2017

Drop mysql2 version less than 0.4.3 to guarantee fork safety (#31244)
Since #31173, mysql2 adapter depends on `automatic_close` which is
introduced since mysql2 0.4.3. So the adapter with the mysql2 version
before doesn't work with fork now.

```
% ARCONN=mysql2 be ruby -w -Itest test/cases/connection_adapters/connection_handler_test.rb -n test_forked_child_doesnt_mangle_parent_connection
Using mysql2
Run options: -n test_forked_child_doesnt_mangle_parent_connection --seed 19988

/Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb:108:in `discard!': undefined method `automatic_close=' for #<Mysql2::Client:0x00007fedaa91dfd0> (NoMethodError)
```

This drops mysql2 version less than 0.4.3 to guarantee fork safety.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment