Skip to content

Commit fb7ebde

Browse files
committed
Catch SocketException
1 parent 06ed59d commit fb7ebde

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

Enyim.Caching/Memcached/MemcachedNode.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Diagnostics;
1010
using System.IO;
1111
using System.Net;
12+
using System.Net.Sockets;
1213
using System.Runtime.Serialization;
1314
using System.Security;
1415
using System.Threading;
@@ -825,7 +826,14 @@ protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperati
825826
{
826827
_logger.LogError(nameof(MemcachedNode), e);
827828

828-
result.Fail("Exception reading response", e);
829+
result.Fail("IOException reading response", e);
830+
return result;
831+
}
832+
catch (SocketException e)
833+
{
834+
_logger.LogError(nameof(MemcachedNode), e);
835+
836+
result.Fail("SocketException reading response", e);
829837
return result;
830838
}
831839
finally

Enyim.Caching/Memcached/PooledSocket.cs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,12 @@ public int ReadByte()
254254
{
255255
return _inputStream.ReadByte();
256256
}
257-
catch (IOException)
257+
catch (Exception ex)
258258
{
259-
_isAlive = false;
259+
if (ex is IOException || ex is SocketException)
260+
{
261+
_isAlive = false;
262+
}
260263

261264
throw;
262265
}
@@ -270,9 +273,12 @@ public int ReadByteAsync()
270273
{
271274
return _inputStream.ReadByte();
272275
}
273-
catch (IOException)
276+
catch (Exception ex)
274277
{
275-
_isAlive = false;
278+
if (ex is IOException || ex is SocketException)
279+
{
280+
_isAlive = false;
281+
}
276282
throw;
277283
}
278284
}
@@ -296,9 +302,13 @@ public async Task ReadAsync(byte[] buffer, int offset, int count)
296302
offset += currentRead;
297303
shouldRead -= currentRead;
298304
}
299-
catch (IOException)
305+
catch (Exception ex)
300306
{
301-
_isAlive = false;
307+
if (ex is IOException || ex is SocketException)
308+
{
309+
_isAlive = false;
310+
}
311+
302312
throw;
303313
}
304314
}
@@ -330,9 +340,12 @@ public void Read(byte[] buffer, int offset, int count)
330340
offset += currentRead;
331341
shouldRead -= currentRead;
332342
}
333-
catch (IOException)
343+
catch (Exception ex)
334344
{
335-
_isAlive = false;
345+
if (ex is IOException || ex is SocketException)
346+
{
347+
_isAlive = false;
348+
}
336349
throw;
337350
}
338351
}
@@ -369,9 +382,13 @@ public void Write(IList<ArraySegment<byte>> buffers)
369382
ThrowHelper.ThrowSocketWriteError(_endpoint, status);
370383
}
371384
}
372-
catch (IOException)
385+
catch (Exception ex)
373386
{
374-
_isAlive = false;
387+
if (ex is IOException || ex is SocketException)
388+
{
389+
_isAlive = false;
390+
}
391+
_logger.LogError(ex, nameof(PooledSocket.Write));
375392
throw;
376393
}
377394
}
@@ -390,9 +407,12 @@ public async Task WriteAsync(IList<ArraySegment<byte>> buffers)
390407
ThrowHelper.ThrowSocketWriteError(_endpoint);
391408
}
392409
}
393-
catch (IOException ex)
410+
catch (Exception ex)
394411
{
395-
_isAlive = false;
412+
if (ex is IOException || ex is SocketException)
413+
{
414+
_isAlive = false;
415+
}
396416
_logger.LogError(ex, nameof(PooledSocket.WriteAsync));
397417
throw;
398418
}

0 commit comments

Comments
 (0)