Skip to content

Commit dfd55ce

Browse files
committed
Improve releasing SemaphoreSlim for acquiring PolledSocket
1 parent 0fef0cf commit dfd55ce

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

Enyim.Caching/Memcached/MemcachedNode.cs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -639,30 +639,47 @@ private void ReleaseSocket(PooledSocket socket)
639639
// is it still working (i.e. the server is still connected)
640640
if (socket.IsAlive)
641641
{
642-
// mark the item as free
643-
this.freeItems.Push(socket);
644-
645-
// signal the event so if someone is waiting for it can reuse this item
646-
this.semaphore.Release();
642+
try
643+
{
644+
// mark the item as free
645+
this.freeItems.Push(socket);
646+
}
647+
finally
648+
{
649+
// signal the event so if someone is waiting for it can reuse this item
650+
this.semaphore.Release();
651+
}
647652
}
648653
else
649654
{
650-
// kill this item
651-
socket.Destroy();
652-
653-
// mark ourselves as not working for a while
654-
this.MarkAsDead();
655+
try
656+
{
657+
// kill this item
658+
socket.Destroy();
655659

656-
// make sure to signal the Acquire so it can create a new conenction
657-
// if the failure policy keeps the pool alive
658-
this.semaphore.Release();
660+
// mark ourselves as not working for a while
661+
this.MarkAsDead();
662+
}
663+
finally
664+
{
665+
// make sure to signal the Acquire so it can create a new conenction
666+
// if the failure policy keeps the pool alive
667+
this.semaphore.Release();
668+
}
659669
}
660670
}
661671
else
662672
{
663-
// one of our previous sockets has died, so probably all of them
664-
// are dead. so, kill the socket (this will eventually clear the pool as well)
665-
socket.Destroy();
673+
try
674+
{
675+
// one of our previous sockets has died, so probably all of them
676+
// are dead. so, kill the socket (this will eventually clear the pool as well)
677+
socket.Destroy();
678+
}
679+
finally
680+
{
681+
this.semaphore.Release();
682+
}
666683
}
667684
}
668685

0 commit comments

Comments
 (0)