You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-dev@logging.apache.org by Tomer Guri <to...@retalix.com> on 2012/03/21 11:34:41 UTC

Does log4cxx is thread safe

Hi all,

 

Though this sounds like silly question, we found production issue which
may expose a small problem with log4cxx thread safety.

We got application crash when trying to write a log line due to level
object being null and accessed.

When deeping our research we found that the way "get levels" functions
are implemented looks to us as not fully thread safety.

The way the function implemented for all log level is:

LevelPtr Level::getError() {

   static LevelPtr level(new Level(Level::ERROR_INT,
LOG4CXX_STR("ERROR"), 3));

   return level;

}

 

Now in case two threads are accessing the same time (and at the first
time) to this function the level may return null as static member is not
thread safe on windows by design.

You can see it in the below article:

C++ scoped static initialization is not thread-safe, on purpose!

 

http://blogs.msdn.com/b/oldnewthing/archive/2004/03/08/85901.aspx

 

This can be easily worked around by write dummy log line for each level
when application starts - making sure that level object is initialized
properly.

We succeed reproducing this issue with test application causing same
race condition and found the suggested workaround solving it.

 

My question to you:

1)      Can you approve this is a hole in log4cxx thread safety ?

2)      Did you encounter this issue before ?

3)      Do you agree with the suggested workaround or did you
implemented something else ?

4)      Does log4cxx should be thread safety also on multi-processor
machines ?

 

Best Regards,

Tomer Guri

Development manager


This mail was sent via Mail-SeCure System.



RE: Does log4cxx is thread safe

Posted by Roger Orr <ro...@howzatt.demon.co.uk>.
The original work around (calling the method early) works.
Failing that one could add a mutex of some kind around the call, for VC on
Windows.
 
Roger.

-----Original Message-----
From: chandpriyankara@gmail.com [mailto:chandpriyankara@gmail.com] On Behalf
Of chand priyankara
Sent: 24 March 2012 17:14
To: Log4CXX Dev
Subject: Re: Does log4cxx is thread safe


Shall we continue with double locking?


On Sat, Mar 24, 2012 at 9:03 PM, Roger Orr <ro...@howzatt.demon.co.uk>
wrote:


Rob Riggs wrote:

> What version of the compiler are you using? The new C++11 standard
> requires that scoped static variables are initialized in a thread-safe
> manner.  I would expect that VC10/VC11 works correctly.


Sadly it seems even VC 11 beta doesn't implement the new standard in this
area.
I am a little disappointed :-(

Regards,
Roger.






-- 




 
<http://uploads.wisestamp.com/6a678a296303219cfebd5c31f3be26f0/1319134154.pn
g>   
	Chand Priyankara 


|(094) 773-361566 
|chand@engineering.com
|www.blog.friendly.com <http://www.blog.friendly.com/> 
 <http://www.facebook.com/chand.priyankara> Facebook
<http://lk.linkedin.com/in/chandpriyankara> LinkedIn
<http://chandpriyankara.blogspot.com/> Blogger
<http://plus.google.com/104246340732624023499> Google Plus

 <http://www.iucnredlist.org/amazing-species> 
  <http://images.wisestamp.com/widgets/green_32.png> 	Please consider your
environmental responsibility. Before printing this e-mail message, ask
yourself whether you really need a hard copy.	

 
<http://p1.wisestamp.com/pixel.png?p=chrome&v=3.9.4.0&t=1319139616414&u=0c64
9d2e16a678a2> 





Re: Does log4cxx is thread safe

Posted by chand priyankara <ch...@engineering.com>.
Shall we continue with double locking?

On Sat, Mar 24, 2012 at 9:03 PM, Roger Orr <ro...@howzatt.demon.co.uk>wrote:

> Rob Riggs wrote:
>
> > What version of the compiler are you using? The new C++11 standard
> > requires that scoped static variables are initialized in a thread-safe
> > manner.  I would expect that VC10/VC11 works correctly.
>
> Sadly it seems even VC 11 beta doesn't implement the new standard in this
> area.
> I am a little disappointed :-(
>
> Regards,
> Roger.
>
>


-- 


*Chand Priyankara*
|(094) 773-361566
|chand@engineering.com
|www.blog.friendly.com
[image: Facebook] <http://www.facebook.com/chand.priyankara> [image:
LinkedIn] <http://lk.linkedin.com/in/chandpriyankara> [image:
Blogger]<http://chandpriyankara.blogspot.com/>
 [image: Google Plus] <http://plus.google.com/104246340732624023499>
 <http://www.iucnredlist.org/amazing-species>
 Please consider your environmental responsibility. Before printing this
e-mail message, ask yourself whether you really need a hard copy.
*

*

RE: Does log4cxx is thread safe

Posted by Roger Orr <ro...@howzatt.demon.co.uk>.
Rob Riggs wrote:

> What version of the compiler are you using? The new C++11 standard
> requires that scoped static variables are initialized in a thread-safe
> manner.  I would expect that VC10/VC11 works correctly. 

Sadly it seems even VC 11 beta doesn't implement the new standard in this
area.
I am a little disappointed :-(

Regards,
Roger.


Re: Does log4cxx is thread safe

Posted by Rob Riggs <ro...@pangalactic.org>.
On 03/21/2012 05:34 AM, Tomer Guri wrote:
>
> Hi all,
>
> Though this sounds like silly question, we found production issue 
> which may expose a small problem with log4cxx thread safety.
>
> We got application crash when trying to write a log line due to level 
> object being null and accessed.
>
> When deeping our research we found that the way “get levels” functions 
> are implemented looks to us as not fully thread safety.
>
> The way the function implemented for all log level is:
>
> LevelPtr Level::getError() {
>
> *static LevelPtr level(new Level(Level::ERROR_INT, 
> LOG4CXX_STR("ERROR"), 3));*
>
> return level;
>
> }
>
> Now in case two threads are accessing the same time (_and at the first 
> time_) to this function the level may return null as static member is 
> not thread safe on windows by design.
>
> You can see it in the below article:
>
> *C++ scoped static initialization is not thread-safe, on purpose!*
>
> http://blogs.msdn.com/b/oldnewthing/archive/2004/03/08/85901.aspx
>
> This can be easily worked around by write dummy log line for each 
> level when application starts – making sure that level object is 
> initialized properly.
>
> We succeed reproducing this issue with test application causing same 
> race condition and found the suggested workaround solving it.
>
> My question to you:
>
> 1)Can you approve this is a hole in log4cxx thread safety ?
>
> 2)Did you encounter this issue before ?
>
> 3)Do you agree with the suggested workaround or did you implemented 
> something else ?
>
> 4)Does log4cxx should be thread safety also on multi-processor machines ?
>
> Best Regards,
>
> Tomer Guri
>
> Development manager
>
>
>
> This mail was sent via Mail-SeCure System.

What version of the compiler are you using? The new C++11 standard 
requires that scoped static variables are initialized in a thread-safe 
manner.  I would expect that VC10/VC11 works correctly.  That said, the 
function as it stands is not thread-safe on older MS compilers.  Your 
workaround sounds reasonable.

Rob