Browse Source

Fixing threading issue?

Former-commit-id: 9dfcb11ad66196f32d3b1d13777e77862ac7fd67
af/merge-core
James South 12 years ago
parent
commit
cd37ded85d
  1. 41
      src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs

41
src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs

@ -157,12 +157,11 @@ namespace ImageProcessor.Web.HttpModules
/// The id representing the <see cref="T:System.Threading.SemaphoreSlim"/>. /// The id representing the <see cref="T:System.Threading.SemaphoreSlim"/>.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="T:System.Threading.Mutex"/> for the given id. /// The <see cref="T:System.Threading.SemaphoreSlim"/> for the given id.
/// </returns> /// </returns>
private static SemaphoreSlim GetSemaphoreSlim(string id) private static SemaphoreSlim GetSemaphoreSlim(string id)
{ {
SemaphoreSlim semaphore = SemaphoreSlims.GetOrAdd(id, new SemaphoreSlim(1, 1)); return SemaphoreSlims.GetOrAdd(id, new SemaphoreSlim(1, 1));
return semaphore;
} }
/// <summary> /// <summary>
@ -385,6 +384,15 @@ namespace ImageProcessor.Web.HttpModules
{ {
if (isRemote) if (isRemote)
{ {
SemaphoreSlim semaphore = GetSemaphoreSlim(cachedPath);
try
{
// This should not happen :(
if (semaphore != null)
{
semaphore.Wait();
}
Uri uri = new Uri(requestPath + "?" + urlParameters); Uri uri = new Uri(requestPath + "?" + urlParameters);
RemoteFile remoteFile = new RemoteFile(uri, false); RemoteFile remoteFile = new RemoteFile(uri, false);
@ -392,11 +400,6 @@ namespace ImageProcessor.Web.HttpModules
// Prevent response blocking. // Prevent response blocking.
WebResponse webResponse = await remoteFile.GetWebResponseAsync().ConfigureAwait(false); WebResponse webResponse = await remoteFile.GetWebResponseAsync().ConfigureAwait(false);
SemaphoreSlim semaphore = GetSemaphoreSlim(cachedPath);
try
{
semaphore.Wait();
using (MemoryStream memoryStream = new MemoryStream()) using (MemoryStream memoryStream = new MemoryStream())
{ {
using (WebResponse response = webResponse) using (WebResponse response = webResponse)
@ -429,12 +432,25 @@ namespace ImageProcessor.Web.HttpModules
} }
} }
finally finally
{
// This should not happen :(
if (semaphore != null)
{ {
semaphore.Release(); semaphore.Release();
} }
} }
}
else else
{ {
SemaphoreSlim semaphore = GetSemaphoreSlim(cachedPath);
try
{
// This should not happen :(
if (semaphore != null)
{
semaphore.Wait();
}
// Check to see if the file exists. // Check to see if the file exists.
// ReSharper disable once AssignNullToNotNullAttribute // ReSharper disable once AssignNullToNotNullAttribute
FileInfo fileInfo = new FileInfo(requestPath); FileInfo fileInfo = new FileInfo(requestPath);
@ -444,11 +460,6 @@ namespace ImageProcessor.Web.HttpModules
throw new HttpException(404, "No image exists at " + fullPath); throw new HttpException(404, "No image exists at " + fullPath);
} }
SemaphoreSlim semaphore = GetSemaphoreSlim(cachedPath);
try
{
semaphore.Wait();
// Process the Image // Process the Image
imageFactory.Load(requestPath) imageFactory.Load(requestPath)
.AutoProcess(queryString) .AutoProcess(queryString)
@ -464,12 +475,16 @@ namespace ImageProcessor.Web.HttpModules
await cache.TrimCachedFolderAsync(cachedPath); await cache.TrimCachedFolderAsync(cachedPath);
} }
finally finally
{
// This should not happen :(
if (semaphore != null)
{ {
semaphore.Release(); semaphore.Release();
} }
} }
} }
} }
}
string incomingEtag = context.Request.Headers["If-None-Match"]; string incomingEtag = context.Request.Headers["If-None-Match"];

Loading…
Cancel
Save