FIX: Avoid leaking Redis connections. (#229)

FIX: Avoid leaking Redis connections. (#229)

diff --git a/CHANGELOG b/CHANGELOG
index fb7cdf6..9f52f78 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,7 @@
+- Unreleased
+
+  - FIX: `MessageBus::Backends::Redis#global_subscribe` not closing Redis connections.
+
 - Version 3.3.0
 
    - FEATURE: `MessageBus.base_route=` to alter the route that message bus will listen on.
diff --git a/lib/message_bus/backends/redis.rb b/lib/message_bus/backends/redis.rb
index df54493..6c1d398 100644
--- a/lib/message_bus/backends/redis.rb
+++ b/lib/message_bus/backends/redis.rb
@@ -251,11 +251,11 @@ LUA
 
       # (see Base#global_unsubscribe)
       def global_unsubscribe
-        if @redis_global
-          # new connection to avoid deadlock
-          new_redis_connection.publish(redis_channel_name, UNSUB_MESSAGE)
-          @redis_global.disconnect
-          @redis_global = nil
+        begin
+          new_redis = new_redis_connection
+          new_redis.publish(redis_channel_name, UNSUB_MESSAGE)
+        ensure
+          new_redis&.disconnect!
         end
       end
 
@@ -278,13 +278,13 @@ LUA
         end
 
         begin
-          @redis_global = new_redis_connection
+          global_redis = new_redis_connection
 
           if highest_id
             clear_backlog.call(&blk)
           end
 
-          @redis_global.subscribe(redis_channel_name) do |on|
+          global_redis.subscribe(redis_channel_name) do |on|
             on.subscribe do
               if highest_id
                 clear_backlog.call(&blk)
@@ -298,7 +298,7 @@ LUA
 
             on.message do |_c, m|
               if m == UNSUB_MESSAGE
-                @redis_global.unsubscribe
+                global_redis.unsubscribe
                 return
               end
               m = MessageBus::Message.decode m
@@ -321,6 +321,8 @@ LUA
           @logger.warn "#{error} subscribe failed, reconnecting in 1 second. Call stack #{error.backtrace}"
           sleep 1
           retry
+        ensure
+          global_redis.disconnect!
         end
       end
 

GitHub sha: e3260cd1

1 Like

This commit appears in #229 which was merged by tgxworld.