Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix a potential threadsafe issue

Lazily setting @mutex could lead to have two mutexes with a sample code
like:

    [MyListener.new, MailListener.new].each do |listener|
      Thread.new {
        Wisper::GlobalListeners.add_listener(listener)
      }
    end

This example is cumbersome, but well, it shows the idea. Even with a
singleton, if `add_listener` is called a first time by two different
threads, this could lead to thread safety issues for ruby implementation
without a gvl.

Signed-off-by: chatgris <jboyer@af83.com>
  • Loading branch information...
commit 38c7783887a5dabdb7d7c93910dcedbf6d94f308 1 parent ea21c55
chatgris authored
Showing with 3 additions and 4 deletions.
  1. +3 −4 lib/wisper/global_listeners.rb
View
7 lib/wisper/global_listeners.rb
@@ -3,9 +3,12 @@
module Wisper
class GlobalListeners
include Singleton
+ attr_reader :mutex
+ private :mutex
def initialize
@listeners = Set.new
+ @mutex = Mutex.new
end
def add_listener(listener, options = {})
@@ -35,10 +38,6 @@ def self.clear
private
- def mutex
- @mutex ||= Mutex.new
- end
-
def with_mutex
mutex.synchronize { yield }
end
Please sign in to comment.
Something went wrong with that request. Please try again.