From 2ba39008794e034848215cfae9f915ffe8ab7e3b Mon Sep 17 00:00:00 2001 From: Steve Powell Date: Wed, 5 Oct 2011 17:36:54 +0100 Subject: [PATCH 1/3] Catch AlreadyClosedException in Connection.Close() for overlapping closes. --- .../src/client/impl/ConnectionBase.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs b/projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs index 4b4408630..30e8086aa 100644 --- a/projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs +++ b/projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs @@ -449,7 +449,18 @@ public void Close(ShutdownEventArgs reason, bool abort, int timeout) m_session0.Transmit(ConnectionCloseWrapper(reason.ReplyCode, reason.ReplyText)); } - catch (IOException ioe) { + catch (AlreadyClosedException ace) + { + if (abort) { + if (!m_appContinuation.WaitOne(BlockingCell.validatedTimeout(timeout), true)) + m_frameHandler.Close(); + return; + } else { + throw ace; + } + } + catch (IOException ioe) + { if (m_model0.CloseReason == null) { if (!abort) From 150a5b086fc9e04ebf6c10861ae4a8198893e624 Mon Sep 17 00:00:00 2001 From: Steve Powell Date: Tue, 29 Nov 2011 09:55:55 +0000 Subject: [PATCH 2/3] Rewrote section to only have to call WaitOne once. --- .../src/client/impl/ConnectionBase.cs | 70 ++++++++----------- 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs b/projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs index 3d1af2c8f..a00f528bf 100644 --- a/projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs +++ b/projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs @@ -424,50 +424,42 @@ public void Abort(ushort reasonCode, string reasonText, int timeout) /// public void Close(ShutdownEventArgs reason, bool abort, int timeout) { - if (!SetCloseReason(reason)) - if (abort) - { - if (!m_appContinuation.WaitOne(BlockingCell.validatedTimeout(timeout), true)) - m_frameHandler.Close(); - return; - } else { - throw new AlreadyClosedException(m_closeReason); - } - - OnShutdown(); - m_session0.SetSessionClosing(false); + bool reasonSet = SetCloseReason(reason); + if (!reasonSet && !abort) + throw new AlreadyClosedException(m_closeReason); - try - { - // Try to send connection.close - // Wait for CloseOk in the MainLoop - m_session0.Transmit(ConnectionCloseWrapper(reason.ReplyCode, - reason.ReplyText)); - } - catch (AlreadyClosedException ace) - { - if (abort) { - if (!m_appContinuation.WaitOne(BlockingCell.validatedTimeout(timeout), true)) - m_frameHandler.Close(); - return; - } else { - throw ace; + if (reasonSet) + { + OnShutdown(); + m_session0.SetSessionClosing(false); + + try + { + // Try to send connection.close + // Wait for CloseOk in the MainLoop + m_session0.Transmit(ConnectionCloseWrapper(reason.ReplyCode, + reason.ReplyText)); } - } - catch (IOException ioe) - { - if (m_model0.CloseReason == null) + catch (AlreadyClosedException ace) { if (!abort) - throw ioe; - else - LogCloseError("Couldn't close connection cleanly. " - + "Socket closed unexpectedly", ioe); + throw ace; + } + catch (IOException ioe) + { + if (m_model0.CloseReason == null) + { + if (!abort) + throw ioe; + else + LogCloseError("Couldn't close connection cleanly. " + + "Socket closed unexpectedly", ioe); + } + } + finally + { + TerminateMainloop(); } - } - finally - { - TerminateMainloop(); } if (!m_appContinuation.WaitOne(BlockingCell.validatedTimeout(timeout),true)) m_frameHandler.Close(); From 8e407c47afd54c10f1228763c0970a5fd6b7e2f5 Mon Sep 17 00:00:00 2001 From: Emile Joubert Date: Tue, 29 Nov 2011 11:32:42 +0000 Subject: [PATCH 3/3] Minimize diff slightly --- .../src/client/impl/ConnectionBase.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs b/projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs index a00f528bf..430297ab8 100644 --- a/projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs +++ b/projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs @@ -424,12 +424,13 @@ public void Abort(ushort reasonCode, string reasonText, int timeout) /// public void Close(ShutdownEventArgs reason, bool abort, int timeout) { - bool reasonSet = SetCloseReason(reason); - if (!reasonSet && !abort) - throw new AlreadyClosedException(m_closeReason); - - if (reasonSet) - { + if (!SetCloseReason(reason)) + { + if (!abort) + throw new AlreadyClosedException(m_closeReason); + } + else + { OnShutdown(); m_session0.SetSessionClosing(false); @@ -452,7 +453,7 @@ public void Close(ShutdownEventArgs reason, bool abort, int timeout) if (!abort) throw ioe; else - LogCloseError("Couldn't close connection cleanly. " + LogCloseError("Couldn't close connection cleanly. " + "Socket closed unexpectedly", ioe); } }