[Mono-devel-list] HasShutdownStarted race?

Ben Maurer bmaurer at ximian.com
Fri Jun 3 00:37:37 EDT 2005

On Fri, 2005-06-03 at 14:11 +1000, Michi Henning wrote:
> Hi,
> System.Environment.HasShutdownStarted is the officially recommended way
> of protecting finalizers against accessing finalized objects
> (see http://msdn2.microsoft.com/library/xw9chd06(en-us,vs.80).aspx).
> What worries me is that I'm not sure this is free from race conditions.
> For example:
> ~SomeClass()
> {
>      if(!System.Environment.HasShutdownStarted)
>      {
>          _mem1.foo();
>          _mem2.bar();
>          _mem3.baz();
>          // etc...
>      }
> }
> I can find nothing in the documentation that would guarantee that the
> condition cannot change halfway through the execution of a finalizer,
> for example, if System.Environment.Exit() is called from another thread.
> The only way this would be possible would be for the GC and Exit()
> to interlock on setting HasShutDownStarted to true, so that the value
> of the property cannot change halfway through the execution of a finalizer.

Finalizers are run by one thread, so once you are in one, no new objets
will get finalized behind your back.

However, I'm not sure if the spec promises that at all.

OTOH, HasShutdownStarted probably *could* change in the middle of the
method. But it won't matter.

-- Ben

More information about the Mono-devel-list mailing list