Skip to content

A platform for community discussion. Free, open, simple.

FIX: Ensure we dispose of MiniRacer::Context before forking daemons (#28361)

Alan Guo Xiang Tan10ff0ee0cc00
diff --git a/config/unicorn.conf.rb b/config/unicorn.conf.rb
index b8c549bc0fc..13c3f4eaf16 100644
--- a/config/unicorn.conf.rb
+++ b/config/unicorn.conf.rb
@@ -64,15 +64,7 @@ initialized = false
before_fork do |server, worker|
unless initialized
Discourse.preload_rails!
-
- # V8 does not support forking, make sure all contexts are disposed
- ObjectSpace.each_object(MiniRacer::Context) { |c| c.dispose }
-
- # get rid of rubbish so we don't share it
- # longer term we will use compact! here
- GC.start
- GC.start
- GC.start
+ Discourse.before_fork
initialized = true
diff --git a/lib/demon/base.rb b/lib/demon/base.rb
index 5ab23c2a3ad..61b7682e6e5 100644
--- a/lib/demon/base.rb
+++ b/lib/demon/base.rb
@@ -154,6 +154,8 @@ class Demon::Base
end
def run
+ Discourse.before_fork if defined?(Discourse)
+
@pid =
fork do
Process.setproctitle("discourse #{self.class.prefix}")
@@ -161,6 +163,7 @@ class Demon::Base
establish_app
after_fork
end
+
write_pid_file
end
diff --git a/lib/discourse.rb b/lib/discourse.rb
index 53ab64aecee..c1dafbfedd7 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -896,6 +896,20 @@ module Discourse
"/site/read-only"
end
+ # all forking servers must call this
+ # before forking, otherwise the forked process might
+ # be in a bad state
+ def self.before_fork
+ # V8 does not support forking, make sure all contexts are disposed
+ ObjectSpace.each_object(MiniRacer::Context) { |c| c.dispose }
+
+ # get rid of rubbish so we don't share it
+ # longer term we will use compact! here
+ GC.start
+ GC.start
+ GC.start
+ end
+
# all forking servers must call this
# after fork, otherwise Discourse will be
# in a bad state