Skip to content

Commit

Permalink
Fixing concurrency handling when we are disposed.
Browse files Browse the repository at this point in the history
  • Loading branch information
ayende committed Aug 24, 2011
1 parent 4a66860 commit 2b51434
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
29 changes: 17 additions & 12 deletions Raven.Storage.Esent/TransactionalStorage.cs
Expand Up @@ -327,23 +327,27 @@ private bool EnsureDatabaseIsCreatedAndAttachToDatabase()
[DebuggerHidden, DebuggerNonUserCode, DebuggerStepThrough] [DebuggerHidden, DebuggerNonUserCode, DebuggerStepThrough]
public void Batch(Action<IStorageActionsAccessor> action) public void Batch(Action<IStorageActionsAccessor> action)
{ {
if (disposed) if (disposerLock.IsReadLockHeld) // we are currently in a nested Batch call
{ {
Trace.WriteLine("TransactionalStorage.Batch was called after it was disposed, call was ignored."); if (current.Value != null) // check again, just to be sure
return; // this may happen if someone is calling us from the finalizer thread, so we can't even throw on that {
} action(current.Value);
if (current.Value != null) return;
{ }
action(current.Value); }
return;
}
disposerLock.EnterReadLock(); disposerLock.EnterReadLock();
try try
{ {
ExecuteBatch(action); ExecuteBatch(action);
} }
catch (EsentErrorException e) catch (EsentErrorException e)
{ {
if (disposed)
{
Trace.WriteLine("TransactionalStorage.Batch was called after it was disposed, call was ignored.");
return; // this may happen if someone is calling us from the finalizer thread, so we can't even throw on that
}

switch (e.Error) switch (e.Error)
{ {
case JET_err.WriteConflict: case JET_err.WriteConflict:
Expand All @@ -357,7 +361,8 @@ public void Batch(Action<IStorageActionsAccessor> action)
finally finally
{ {
disposerLock.ExitReadLock(); disposerLock.ExitReadLock();
current.Value = null; if(disposed == false)
current.Value = null;
} }
} }


Expand Down
26 changes: 15 additions & 11 deletions Raven.Storage.Managed/TransactionalStorage.cs
Expand Up @@ -86,20 +86,23 @@ public Guid Id
[DebuggerNonUserCode] [DebuggerNonUserCode]
public void Batch(Action<IStorageActionsAccessor> action) public void Batch(Action<IStorageActionsAccessor> action)
{ {
if (disposed) if (disposerLock.IsReadLockHeld) // we are currently in a nested Batch call
{ {
Trace.WriteLine("TransactionalStorage.Batch was called after it was disposed, call was ignored."); if (current.Value != null) // check again, just to be sure
return; // this may happen if someone is calling us from the finalizer thread, so we can't even throw on that {
action(current.Value);
return;
}
} }

disposerLock.EnterReadLock();
if(current.Value != null)
{
action(current.Value);
return;
}
disposerLock.EnterReadLock();
try try
{ {
if (disposed)
{
Trace.WriteLine("TransactionalStorage.Batch was called after it was disposed, call was ignored.");
return; // this may happen if someone is calling us from the finalizer thread, so we can't even throw on that
}



Interlocked.Exchange(ref lastUsageTime, SystemTime.Now.ToBinary()); Interlocked.Exchange(ref lastUsageTime, SystemTime.Now.ToBinary());
using (tableStroage.BeginTransaction()) using (tableStroage.BeginTransaction())
Expand All @@ -115,7 +118,8 @@ public void Batch(Action<IStorageActionsAccessor> action)
finally finally
{ {
disposerLock.ExitReadLock(); disposerLock.ExitReadLock();
current.Value = null; if(disposed ==false)
current.Value = null;
} }
} }


Expand Down
2 changes: 2 additions & 0 deletions Raven.Tryouts/Program.cs
@@ -1,4 +1,6 @@
using System; using System;
using Raven.Client.Listeners;
using Raven.Json.Linq;
using Raven.Tests.Bugs; using Raven.Tests.Bugs;


namespace Raven.Tryouts namespace Raven.Tryouts
Expand Down

0 comments on commit 2b51434

Please sign in to comment.