[Mono-devel-list] What to do with TypeInitialization in case of exceptions on second attempt to access class ? Singleton pattern

Paolo Molaro lupus at ximian.com
Tue Nov 2 09:21:43 EST 2004


On 10/25/04 Andriy G. Tereshchenko wrote:
> In this case pattern using static properties fail very-very hard:
> class Singleton
> {
>     Singleton() { throw new ArgumentException("I feel headache. Try again next night"); }
>     public static Singleton Instance  { get {  return Singleton.instance; } }
>     private static Singleton instance = new Singleton(); 
> }
> 
> First of all - user receive System.TypeInitializationException exceptions. 
> This is unexpected to user - but documented feature of CLR.
> But this email not about this. But about future user actions. 
> 
> In case if code will try to access this static property at second (third as so on) attempt - here is most funny things happen. 
> In Microsoft implementation -  TypeInitialization thrown for all future access. 
> This is somethat good - as all users of incorrectly constructed singleton will be notified about this. 
> But in the same time - this can result some parts of singleton code executed more that once and result in side-effects. 
> 
> In Java - all future access to non-fully constructed classes result in ClassNotFound exception !!  Not good ;-)
> 
> But now then trying to test the same application (Program.cs) on Mono - it looks like Mono decided to implement this in different
> way.
> All future access to property return null and no exceptions ! Not good. 

We need a field in MonoClass that tells us if the needed metadata
for the class could be loaded correctly. In the same way we need
a field in MonoVTable that tells us if the the static ctor has thrown
an exception. Then in the jit, in the places where a new MonoClass
or a new MonoVTable is loaded, we need to check for errors and
throw the appropriate exception (there are quite a few such places,
and not all of them happen when it's safe to throw an exception, so
in a few places we need to propagate them through MonoCompile,
for example).
If you're willing to help with this it will be appreciated:-)
Please file bugzilla bugs so we don't forget about this issue.

lupus

-- 
-----------------------------------------------------------------
lupus at debian.org                                     debian/rules
lupus at ximian.com                             Monkeys do it better



More information about the Mono-devel-list mailing list