Skip to content

Commit

Permalink
Basic protection against users not Disposing the MembaseClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
enyim committed Dec 14, 2010
1 parent f592565 commit e399cc8
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Membase/MessageStreamListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ public MessageStreamListener(Uri[] urls, Func<WebClientWithTimeout, Uri, Uri> co

this.RetryCount = 0;
this.RetryTimeout = new TimeSpan(0, 0, 0, 0, 500);

// domain unloads are not guaranteed to call the finalizers
// and when users do not abide the IDIsposabel contract
// we end up a job in the thread pool which never quits
// and prevents the unloading of the app domain
// this is not big deal in normal applications because the
// process exit aborts everything, but it's a huge issue in
// asp.net because the app domain will not be unloaded
// but still, the best way to deal with this is issue to dispose the client
AppDomain.CurrentDomain.DomainUnload += CurrentDomain_DomainUnload;
}

void CurrentDomain_DomainUnload(object sender, EventArgs e)
{
this.Dispose();
}

~MessageStreamListener()
{
try { this.Dispose(); }
catch { }
}

protected event Action<string> MessageReceived;
Expand Down Expand Up @@ -388,6 +409,8 @@ void IDisposable.Dispose()

protected void Dispose()
{
AppDomain.CurrentDomain.DomainUnload -= CurrentDomain_DomainUnload;

this.CleanupRequests();

if (this.client != null)
Expand Down

0 comments on commit e399cc8

Please sign in to comment.