Author: jgomes
Date: Wed Dec 11 19:57:05 2013
New Revision: 1550242

URL: http://svn.apache.org/r1550242
Log:
Move the blocking code outside of the lock section to avoid race condition when 
shutting down.
Fixes [AMQNET-338]. (See https://issues.apache.org/jira/browse/AMQNET-338)

Modified:
    
activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/Tcp/TcpTransport.cs

Modified: 
activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/Tcp/TcpTransport.cs
URL: 
http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/Tcp/TcpTransport.cs?rev=1550242&r1=1550241&r2=1550242&view=diff
==============================================================================
--- 
activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/Tcp/TcpTransport.cs
 (original)
+++ 
activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/Tcp/TcpTransport.cs
 Wed Dec 11 19:57:05 2013
@@ -40,8 +40,8 @@ namespace Apache.NMS.Stomp.Transport.Tcp
         private readonly Atomic<bool> closed = new Atomic<bool>(false);
         private volatile bool seenShutdown;
         private readonly Uri connectedUri;
-               private int timeout = -1;
-               private int asynctimeout = -1;
+        private int timeout = -1;
+        private int asynctimeout = -1;
 
         private CommandHandler commandHandler;
         private ExceptionHandler exceptionHandler;
@@ -168,6 +168,8 @@ namespace Apache.NMS.Stomp.Transport.Tcp
 
         public void Close()
         {
+            Thread theReadThread = null;
+
             if(closed.CompareAndSet(false, true))
             {
                 lock(myLock)
@@ -218,24 +220,30 @@ namespace Apache.NMS.Stomp.Transport.Tcp
                     {
                     }
 
-                    if(null != readThread)
-                    {
-                        if(Thread.CurrentThread != readThread
+                    theReadThread = this.readThread;
+                    this.readThread = null;
+                    started = false;
+                }
+            }
+
+            if(null != theReadThread)
+            {
+                try
+                {
+                    if(Thread.CurrentThread != theReadThread
 #if !NETCF
- && readThread.IsAlive
+ && theReadThread.IsAlive
 #endif
 )
+                    {
+                        if(!theReadThread.Join((int) 
MAX_THREAD_WAIT.TotalMilliseconds))
                         {
-                            if(!readThread.Join((int) 
MAX_THREAD_WAIT.TotalMilliseconds))
-                            {
-                                readThread.Abort();
-                            }
+                            theReadThread.Abort();
                         }
-
-                        readThread = null;
                     }
-
-                    started = false;
+                }
+                catch
+                {
                 }
             }
         }
@@ -316,27 +324,27 @@ namespace Apache.NMS.Stomp.Transport.Tcp
 
         // Implementation methods
 
-               /// <summary>
-               /// Timeout in milliseconds to wait for sending synchronous 
messages or commands.
-               /// Set to -1 for infinite timeout.
-               /// </summary>
-               public int Timeout
-               {
-                       get { return this.timeout; }
-                       set { this.timeout = value; }
-               }
-
-               /// <summary>
-               /// Timeout in milliseconds to wait for sending asynchronous 
messages or commands.
-               /// Set to -1 for infinite timeout.
-               /// </summary>
-               public int AsyncTimeout
-               {
-                       get { return this.asynctimeout; }
-                       set { this.asynctimeout = value; }
-               }
+        /// <summary>
+        /// Timeout in milliseconds to wait for sending synchronous messages 
or commands.
+        /// Set to -1 for infinite timeout.
+        /// </summary>
+        public int Timeout
+        {
+            get { return this.timeout; }
+            set { this.timeout = value; }
+        }
+
+        /// <summary>
+        /// Timeout in milliseconds to wait for sending asynchronous messages 
or commands.
+        /// Set to -1 for infinite timeout.
+        /// </summary>
+        public int AsyncTimeout
+        {
+            get { return this.asynctimeout; }
+            set { this.asynctimeout = value; }
+        }
 
-               public CommandHandler Command
+        public CommandHandler Command
         {
             get { return commandHandler; }
             set { this.commandHandler = value; }


Reply via email to