BIND 10 #2399: make Logger::getLoggerPtr() thread safe

BIND 10 Development do-not-reply at isc.org
Wed Oct 24 03:32:39 UTC 2012


#2399: make Logger::getLoggerPtr() thread safe
-------------------------------------+-------------------------------------
            Reporter:  jinmei        |                        Owner:
                Type:  defect        |                       Status:  new
            Priority:  medium        |                    Milestone:  Next-
           Component:  logging       |  Sprint-Proposed
           Sensitive:  0             |                     Keywords:
         Sub-Project:  DNS           |              Defect Severity:  N/A
Estimated Difficulty:  0             |  Feature Depending on Ticket:
         Total Hours:  0             |          Add Hours to Ticket:  0
                                     |                    Internal?:  0
-------------------------------------+-------------------------------------
 If I understand it correctly, the delayed initialization of
 the `Logger` class is not thread safe:
 {{{#!cpp
     LoggerImpl* getLoggerPtr() {
         if (!loggerptr_) {
             initLoggerImpl();
         }
         return (loggerptr_);
     }
 }}}

 initLoggerImpl() could be called by multiple threads at the same time,
 and due to this:

 {{{#!cpp
 void Logger::initLoggerImpl() {
     if (isLoggingInitialized()) {
         loggerptr_ = new LoggerImpl(name_);
     } else {
         isc_throw(LoggingNotInitialized, "attempt to access logging
 function "
                   "before logging has been initialized");
     }
 }
 }}}
 creating `LoggerImpl` could be done multiple times.

 (btw: the implementation of isLoggingInitialized could also be
 controversial, but this is probably safe in practice due to the
 assumption of initLogger()).

 The (bad) effect of this is probably not catastrophic: maybe it's just
 losing some fixed (and small) amount of memory in the worst case, at
 least in practice.  But I may be wrong, and in any case such leak
 isn't good anyway.

 One way of fixing this would be to introduce some global mutex that is
 initialized at the time of initLogger(), and do something like this:
 {{{#!cpp
     if (isLoggingInitialized()) {
         Locker locker(the_global_mutex);
         if (loggerptr_ == NULL) {
             loggerptr_ = new LoggerImpl(name_);
         }
 }}}

-- 
Ticket URL: <http://bind10.isc.org/ticket/2399>
BIND 10 Development <http://bind10.isc.org>
BIND 10 Development


More information about the bind10-tickets mailing list