Spring for .NET Community Forums    

Go Back   Spring for .NET Community Forums > General > Core Container

Reply
 
Thread Tools Display Modes
  #1  
Old 04-15-2006, 02:11 PM
natl natl is offline
Member
Spring User
 
Join Date: Aug 2005
Posts: 30
Default Singleton is not thread-safe

Hi,

[Spring.NET 1.0.1]
I use GetObject on IApplicationContext.GetObject to get a lazy-init singleton object from different threads. It seems that the method is not thread-safe. If I kick off two threads at the same time and get the object that takes time to initialize, one thread will get the object correctly while the other thread will get null back.

I think this is a very important issue that has to be fixed since whether I configure the object to be singleton or not, it should not affect the code path directly.

To workaround this problem, I need to put synchronize block around GetObject manually and I don't think it's a good idea. Is it possible to get it fixed in 1.0.2?


http://opensource.atlassian.com/proj...wse/SPRNET-170
Reply With Quote
  #2  
Old 04-18-2006, 12:04 PM
Bruno Baia Bruno Baia is offline
Senior Member
Spring TeamSpring User
 
Join Date: Oct 2005
Location: Toulouse, France
Posts: 1,239
Default

Hi,

this issue seems to be close.

Have you tried with one of the lastest nightly build ?
http://www.springframework.net/downloads/nightly/
__________________
My english is as poor as my taylor is rich
Reply With Quote
  #3  
Old 04-18-2006, 07:26 PM
natl natl is offline
Member
Spring User
 
Join Date: Aug 2005
Posts: 30
Default

It said that it is fixed in 1.0 but I'm using 1.0.1. So I think the bug is still there.
Reply With Quote
  #4  
Old 04-19-2006, 12:44 AM
Bruno Baia Bruno Baia is offline
Senior Member
Spring TeamSpring User
 
Join Date: Oct 2005
Location: Toulouse, France
Posts: 1,239
Default

Hi,

you right, the bug is still here, i reproduced it !

Before to create a singleton object, Spring.NET puts a marker object into the cache :
Code:
/// <summary>
/// Marker object to be temporarily registered in the singleton cache,
/// while instantiating an object (in order to be able to detect circular references).
/// </summary>
private static readonly object CURRENTLY_IN_CREATION = new Object();
so if the instance creation takes time, the second thread, that calls the GetObject method, will find an object into the cache and will return it.
the instance returned will be CURRENTLY_IN_CREATION = new Object().
(You said it gets a null back, can you verify please ?)


I fixed it, but need to talk with Mark or Aleks before.
We'll try to add the fix for the 1.0.2.


-Bruno
__________________
My english is as poor as my taylor is rich
Reply With Quote
  #5  
Old 04-19-2006, 01:18 AM
natl natl is offline
Member
Spring User
 
Join Date: Aug 2005
Posts: 30
Default

I think you're right. I just use "as" keyword to cast it. So if it's not castable to the type I want. It will be null.
Reply With Quote
  #6  
Old 04-23-2006, 09:05 PM
Mark Pollack Mark Pollack is offline
Spring.NET Co-Lead
Spring TeamSpring User
 
Join Date: Sep 2004
Location: New York, NY
Posts: 1,556
Default

Hi,

I've added back in some synchronization code that was accidentially removed after the initial fix. I've also added a test case for multithreaded lazy init singleton access that now pass with the current changes. These changes will be in the 1.0.2 release.

Just a small note that I will be following up one loose-end on this topic with the Spring.Java guys as it seems the Java implementation can (rarely) throw a ObjectCurrentlyInCreationException exception that seems unecessary.

Thanks for raising the issue.

Cheers,
Mark
Reply With Quote
  #7  
Old 04-24-2006, 12:27 AM
natl natl is offline
Member
Spring User
 
Join Date: Aug 2005
Posts: 30
Default

Mark,

Thanks for addressing the issue. I kinda wondered why no one else raised this issue :wink:

And yes... ObjectCurrentlyInCreationException seems to be useless. It should never throw that exception. Hopefully we don't have double locking problem that might cause such issue in IA-64 although not many people use it... haha :twisted:
Reply With Quote
  #8  
Old 04-27-2006, 04:47 AM
natl natl is offline
Member
Spring User
 
Join Date: Aug 2005
Posts: 30
Default

So did you get a word from Juergen why that exception can be thrown from the factory. I think in .NET, there should be no case we should throw that exception. The only plausible one is that when there are complex references among singletons, how can you get a singleton object with thread-safety
Reply With Quote
  #9  
Old 04-27-2006, 09:20 AM
Bruno Baia Bruno Baia is offline
Senior Member
Spring TeamSpring User
 
Join Date: Oct 2005
Location: Toulouse, France
Posts: 1,239
Default

Hi,

The ObjectCurrentlyInCreationException is raised when circular references are detected

Code:
<object id="testObject1" type="SpringTests.TestObject1, SpringTests">
        <constructor-arg name="arg" ref="testObject2" />
</object>

<object id="testObject2" type="SpringTests.TestObject2, SpringTests">
       <constructor-arg name="arg" ref="testObject1" />
</object>
-> throw ObjectCurrentlyInCreationException


Regards,
Bruno
__________________
My english is as poor as my taylor is rich
Reply With Quote
  #10  
Old 04-27-2006, 12:25 PM
natl natl is offline
Member
Spring User
 
Join Date: Aug 2005
Posts: 30
Default

Thanks. That makes sense then. But if I remember correctly, I've seen someone has a use case there circular reference is needed. In that case, should I make A refers to B through DI while make B refers to A by using ContextRegistry.GetContext().GetObject("A")?
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
singleton attribute ignored? srdjan Core Container 5 07-12-2007 03:45 PM
Dispose a singleton instance oz_ko Core Container 5 10-21-2005 06:13 AM
Singleton object creation causes stack overflow Alistair Web 13 08-01-2005 08:04 AM
Singleton or not singleton samokk Core Container 8 07-10-2005 08:25 PM
Legacy Singleton Ted Husted Core Container 7 05-13-2005 06:26 PM


All times are GMT. The time now is 09:06 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.