Skip to content

Commit

Permalink
Add locking to Servicese dispose (#58)
Browse files Browse the repository at this point in the history
Make dispose safe
  • Loading branch information
tsibelman authored Jul 24, 2017
1 parent 37f796a commit 16a4b42
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 23 deletions.
17 changes: 14 additions & 3 deletions Gigya.Microdot.Hosting/Service/GigyaServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ protected virtual void Dispose(bool disposing)
if (disposed)
return;

StopEvent?.Dispose();
WindowsService?.Dispose();
MonitoredShutdownProcess?.Dispose();
SafeDispose(StopEvent);
SafeDispose(WindowsService);
SafeDispose(MonitoredShutdownProcess);

disposed = true;
}
Expand All @@ -278,6 +278,17 @@ public void Dispose()
GC.SuppressFinalize(this);
}

protected void SafeDispose(IDisposable disposable)
{
try
{
disposable?.Dispose();
}
catch (Exception e)
{
Trace.TraceError(e.ToString());
}
}


private class DelegatingServiceBase : ServiceBase
Expand Down
39 changes: 28 additions & 11 deletions Gigya.Microdot.Ninject.Host/MicrodotServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#endregion

using System;
using System.Diagnostics;
using Gigya.Microdot.Hosting.HttpService;
using Gigya.Microdot.Hosting.Service;
using Gigya.Microdot.Interfaces;
Expand All @@ -40,6 +41,8 @@ namespace Gigya.Microdot.Ninject.Host
public abstract class MicrodotServiceHost<TInterface> : GigyaServiceHost
{
private bool disposed;

private readonly object disposeLockHandale = new object();

private IKernel Kernel { get; set; }

Expand Down Expand Up @@ -116,15 +119,6 @@ protected virtual IKernel CreateKernel()
}


protected override void Dispose(bool disposing)
{
if(disposed)
return;
Kernel?.Dispose();
disposed = true;
base.Dispose(disposing);
}

/// <summary>
/// Used to configure Kernel in abstract base-classes, which should apply to any concrete service that inherits from it.
/// Should be overridden when creating a base-class that should include common behaviour for a family of services, without
Expand Down Expand Up @@ -157,9 +151,32 @@ protected virtual void PreConfigure(IKernel kernel)
/// method.
/// </summary>
protected override void OnStop()
{
Dispose();
}


protected override void Dispose(bool disposing)
{
Listener.Dispose();
Kernel.Dispose();
lock (disposeLockHandale)
{
try
{
if(disposed)
return;

SafeDispose(Listener);

if(!Kernel.IsDisposed)
SafeDispose(Kernel);

base.Dispose(disposing);
}
finally
{
disposed = true;
}
}
}
}
}
24 changes: 18 additions & 6 deletions Gigya.Microdot.Orleans.Ninject.Host/MicrodotOrleansServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,26 @@ protected override void OnStop()
Dispose();
}


private readonly object lockHandale=new object();
protected override void Dispose(bool disposing)
{
if (disposed)
return;
Kernel?.Dispose();
disposed = true;
base.Dispose(disposing);
lock(lockHandale)
{
try
{
if (disposed)
return;

if (!Kernel.IsDisposed)
SafeDispose( Kernel);

base.Dispose(disposing);
}
finally
{
disposed = true;
}
}
}
}
}
6 changes: 3 additions & 3 deletions SolutionVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
[assembly: AssemblyCopyright("© 2017 Gigya Inc.")]
[assembly: AssemblyDescription("Microdot Framework")]

[assembly: AssemblyVersion("1.2.2.0")]
[assembly: AssemblyFileVersion("1.2.2.0")]
[assembly: AssemblyInformationalVersion("1.2.2.0")]
[assembly: AssemblyVersion("1.2.3.0")]
[assembly: AssemblyFileVersion("1.2.3.0")]
[assembly: AssemblyInformationalVersion("1.2.3.0")]


// Setting ComVisible to false makes the types in this assembly not visible
Expand Down

0 comments on commit 16a4b42

Please sign in to comment.