Skip to content

Commit 1709940

Browse files
flavorjonesdjmb
andcommitted
Clear AR connections in tests before forking for parallelization
Fixes rails#41176 which seems to affect the mysql2 adapter on MacOS. The underlying mysql problem was diagnosed in this comment by @jgloudon: rails#41176 (comment) It seems like on MacOS the mysql8 client library is using kqueue which creates a file descriptor that is not carried into the child process, allowing for the FD to be re-used. Co-authored-by: Donal McBreen <[email protected]>
1 parent 454ecdd commit 1709940

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Introduce a before-fork hook in `ActiveSupport::Testing::Parallelization` to clear existing
2+
connections, to avoid fork-safety issues with the mysql2 adapter.
3+
4+
Fixes #41776
5+
6+
*Mike Dalessio*, *Donal McBreen*
7+
18
* PoolConfig no longer keeps a reference to the connection class.
29

310
Keeping a reference to the class caused subtle issues when combined with reloading in

activerecord/lib/active_record/test_databases.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
module ActiveRecord
66
module TestDatabases # :nodoc:
7+
ActiveSupport::Testing::Parallelization.before_fork_hook do
8+
ActiveRecord::Base.connection_handler.clear_all_connections!
9+
end
10+
711
ActiveSupport::Testing::Parallelization.after_fork_hook do |i|
812
create_and_load_schema(i, env_name: ActiveRecord::ConnectionHandling::DEFAULT_ENV.call)
913
end

activesupport/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* `ActiveSupport::Testing::Parallelization.before_fork_hook` allows declaration of callbacks that
2+
are invoked immediately before forking test workers.
3+
4+
*Mike Dalessio*
5+
16
* Allow the `#freeze_time` testing helper to accept a date or time argument.
27

38
```ruby

activesupport/lib/active_support/testing/parallelization.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
module ActiveSupport
1010
module Testing
1111
class Parallelization # :nodoc:
12+
@@before_fork_hooks = []
13+
14+
def self.before_fork_hook(&blk)
15+
@@before_fork_hooks << blk
16+
end
17+
18+
cattr_reader :before_fork_hooks
19+
1220
@@after_fork_hooks = []
1321

1422
def self.after_fork_hook(&blk)
@@ -32,7 +40,12 @@ def initialize(worker_count)
3240
@url = DRb.start_service("drbunix:", @queue_server).uri
3341
end
3442

43+
def before_fork
44+
Parallelization.before_fork_hooks.each(&:cb)
45+
end
46+
3547
def start
48+
before_fork
3649
@worker_pool = @worker_count.times.map do |worker|
3750
Worker.new(worker, @url).start
3851
end

0 commit comments

Comments
 (0)