Skip to content

Commit 5ebb15d

Browse files
author
Vlad Ionescu
committed
Frame class now throws SocketException on reader or writer IOExceptions caused by a SocketException; SocketException causes the connection main loop to die and propagates to construct ShutdownEventArgs with it - that causes the calling thread to throw an OperationInterruptedException which includes the SocketException's message
1 parent 60872e7 commit 5ebb15d

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
//---------------------------------------------------------------------------
5757
using System;
5858
using System.IO;
59+
using System.Net.Sockets;
5960
using System.Text;
6061
using System.Threading;
6162
using System.Collections;
@@ -484,9 +485,9 @@ public void TerminateMainloop()
484485

485486
public void StartMainLoop()
486487
{
487-
Thread mainloopThread = new Thread(new ThreadStart(MainLoop));
488-
mainloopThread.Name = "AMQP Connection " + Endpoint.ToString();
489-
mainloopThread.Start();
488+
Thread mainLoopThread = new Thread(new ThreadStart(MainLoop));
489+
mainLoopThread.Name = "AMQP Connection " + Endpoint.ToString();
490+
mainLoopThread.Start();
490491
}
491492

492493
public void StartHeartbeatLoops()
@@ -594,6 +595,14 @@ public void MainLoop()
594595
{
595596
shutdownCleanly = HardProtocolExceptionHandler(hpe);
596597
}
598+
catch (SocketException se)
599+
{
600+
// Possibly due to handshake timeout
601+
HandleMainLoopException(new ShutdownEventArgs(ShutdownInitiator.Library,
602+
0,
603+
"Socket exception",
604+
se));
605+
}
597606
catch (Exception ex)
598607
{
599608
HandleMainLoopException(new ShutdownEventArgs(ShutdownInitiator.Library,
@@ -625,7 +634,7 @@ public void MainLoopIteration()
625634
// counter.
626635
return;
627636
}
628-
637+
629638
if (frame.Channel == 0) {
630639
// In theory, we could get non-connection.close-ok
631640
// frames here while we're quiescing (m_closeReason !=

projects/client/RabbitMQ.Client/src/client/impl/Frame.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,17 @@ public static Frame ReadFrom(NetworkBinaryReader reader)
103103
int type;
104104
int channel;
105105

106-
type = reader.ReadByte();
106+
try
107+
{
108+
type = reader.ReadByte();
109+
}
110+
catch (IOException ioe)
111+
{
112+
if (ioe.InnerException != null)
113+
throw ioe.InnerException;
114+
else
115+
throw;
116+
}
107117

108118
if (type == 'A')
109119
{
@@ -177,11 +187,21 @@ public void FinishWriting()
177187
public void WriteTo(NetworkBinaryWriter writer)
178188
{
179189
FinishWriting();
180-
writer.Write((byte)m_type);
181-
writer.Write((ushort)m_channel);
182-
writer.Write((uint)m_payload.Length);
183-
writer.Write((byte[])m_payload);
184-
writer.Write((byte)CommonFraming.Constants.FrameEnd);
190+
try
191+
{
192+
writer.Write((byte) m_type);
193+
writer.Write((ushort) m_channel);
194+
writer.Write((uint) m_payload.Length);
195+
writer.Write((byte[]) m_payload);
196+
writer.Write((byte) CommonFraming.Constants.FrameEnd);
197+
}
198+
catch(IOException ioe)
199+
{
200+
if (ioe.InnerException != null)
201+
throw ioe.InnerException;
202+
else
203+
throw;
204+
}
185205
}
186206

187207
public NetworkBinaryReader GetReader()

projects/client/RabbitMQ.Client/src/client/impl/SocketFrameHandler_0_9.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public Frame ReadFrame()
131131
{
132132
lock (m_reader)
133133
{
134-
return Frame.ReadFrom(m_reader);
134+
return Frame.ReadFrom(m_reader);
135135
}
136136
}
137137

0 commit comments

Comments
 (0)