fix crashes when destroying message bus

fix crashes when destroying message bus

  • do not crash when calling after_fork on destroyed bus
  • do not crash on calling destroy twice
From 25459da871a3c631cfe23844b9143c025c92dc13 Mon Sep 17 00:00:00 2001
From: Michal Cichra <michal@cichra.cz>
Date: Wed, 23 Aug 2017 12:31:00 +0200
Subject: [PATCH] fix crashes when destroying message bus

* do not crash when calling after_fork on destroyed bus
* do not crash on calling destroy twice

diff --git a/lib/message_bus.rb b/lib/message_bus.rb
index aad741d..dcb3666 100644
--- a/lib/message_bus.rb
+++ b/lib/message_bus.rb
@@ -163,7 +163,7 @@ module MessageBus::Implementation
   # Enables publication to the bus
   # @return [void]
   def on
-    @off = false
+    @destroyed = @off = false
   end
 
   # Overrides existing configuration
@@ -495,6 +495,8 @@ module MessageBus::Implementation
     reliable_pub_sub.global_unsubscribe
 
     @mutex.synchronize do
+      return if @destroyed
+
       @subscriptions ||= {}
       @destroyed = true
     end
diff --git a/spec/lib/message_bus_spec.rb b/spec/lib/message_bus_spec.rb
index da14def..41ad905 100644
--- a/spec/lib/message_bus_spec.rb
+++ b/spec/lib/message_bus_spec.rb
@@ -22,6 +22,19 @@ describe MessageBus do
     @bus.off?.must_equal true
   end
 
+  it "can call destroy twice" do
+    @bus.destroy
+    @bus.destroy
+  end
+
+  it "can be turned on after destroy" do
+    @bus.destroy
+
+    @bus.on
+
+    @bus.after_fork
+  end
+
   it "can subscribe from a point in time" do
     @bus.publish("/minion", "banana")

GitHub