Index: include/debugger/dbgPgConn.h
===================================================================
--- include/debugger/dbgPgConn.h	(revision 8217)
+++ include/debugger/dbgPgConn.h	(working copy)
@@ -77,18 +77,23 @@
     PGconn *getConnection();                // Returns the libpq connection handle
     void Close();                           // Close this connection
     void Cancel();                          // Cancel any ongoing queries
+    void SetCancelConn();                   // Set connection for the Cancellation
+    void ResetCancelConn();                 // Reset cancellation connection
 
     void startCommand( const wxString &command, wxEvtHandler * caller, wxEventType eventType = wxEVT_NULL, dbgPgParams *params = NULL );    // Starts executing a command    
     void setNoticeHandler( PQnoticeProcessor handler, void * arg ); // Registers a NOTICE handler
     PGresult *waitForCommand( const wxString &command );            // Starts a command and waits for completion
+    void stopDebugging();
 
   private:
 
     void Init( const wxString &server, const wxString &database, const wxString &userName, const wxString &password, const wxString &port, int sslmode, const wxString &applicationname, bool startThread );
 
-    PGconn *m_pgConn;               // libpq connection handler
-    dbgPgThread *m_workerThread;    // Worker thread (this thread interacts with the server)
+    PGconn      *m_pgConn;                 // libpq connection handler
+    PGcancel    *m_cancelConn;             // libpq cancellation connection handler
+    dbgPgThread *m_workerThread;           // Worker thread (this thread interacts with the server)
     frmDebugger *m_frame;
+    wxMutex      m_pgcancelMutex;          // Mutex to serialize access to m_cancelConn
     int m_minorVersion, m_majorVersion;
     bool m_isEdb;
     bool m_isGreenplum;
Index: include/debugger/dbgPgThread.h
===================================================================
--- include/debugger/dbgPgThread.h	(revision 8217)
+++ include/debugger/dbgPgThread.h	(working copy)
@@ -62,6 +62,7 @@
     virtual void * Entry();
     void startCommand( const wxString &command, wxEvtHandler * caller, wxEventType eventType = wxEVT_NULL, dbgPgParams *params = NULL );
     void Die();
+    void clearAllCommands();
 	
 private:
 
Index: debugger/dbgPgConn.cpp
===================================================================
--- debugger/dbgPgConn.cpp	(revision 8217)
+++ debugger/dbgPgConn.cpp	(working copy)
@@ -44,13 +44,13 @@
 //    The constructor creates a new thread and connects to the specified server
 
 dbgPgConn::dbgPgConn(frmDebugger *frame, const wxString &server, const wxString &database, const wxString &userName, const wxString &password, const wxString &port, int sslmode, const wxString &applicationname)
-   : m_frame(frame)
+   : m_frame(frame), m_pgcancelMutex()
 {
     Init( server, database, userName, password, port, sslmode, applicationname, true );
 }
 
 dbgPgConn::dbgPgConn(frmDebugger *frame, const dbgConnProp & props, bool startThread )
-   : m_frame(frame)
+   : m_frame(frame), m_pgcancelMutex()
 {
     Init(  props.m_host, props.m_database, props.m_userName, props.m_password, props.m_port, props.m_sslMode, props.m_applicationName, startThread );
 }
@@ -61,6 +61,7 @@
     bool libcConnectString = false;
 
     m_pgConn       = NULL;
+    m_cancelConn   = NULL;
     m_majorVersion = 0;
     m_minorVersion = 0;
     m_debuggerApiVersion = DEBUGGER_UNKNOWN_API;
@@ -339,6 +340,15 @@
     return( result );
 }
 
+void dbgPgConn::stopDebugging()
+{
+    // Clear all other commands before cancelling the current command
+    m_workerThread->clearAllCommands();
+
+    // Cancel the current command execution
+    Cancel();
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // setNoticeHandler()
 //
@@ -381,14 +391,15 @@
 
 void dbgPgConn::Cancel()
 {
+    m_pgcancelMutex.Lock();
     // Attempt to cancel any ongoing query
-    if (m_pgConn)
+    if (m_cancelConn)
     {
-        PGcancel *cancel = PQgetCancel(m_pgConn);
         char errbuf[256];
-        PQcancel(cancel, errbuf, sizeof(errbuf));
-        PQfreeCancel(cancel);
+        PQcancel(m_cancelConn, errbuf, sizeof(errbuf));
     }
+    m_pgcancelMutex.Unlock();
+    ResetCancelConn();
 }
 
 // Check the backend version
@@ -516,5 +527,37 @@
     return m_isGreenplum; 
 }
 
+void dbgPgConn::SetCancelConn(void)
+{
+    PGcancel   *oldCancelConn;
 
+    m_pgcancelMutex.Lock();
+    /* Free the old one if we have one */
+    oldCancelConn = m_cancelConn;
+    /* be sure handle_sigint doesn't use pointer while freeing */
+    m_cancelConn = NULL;
 
+    if (oldCancelConn != NULL)
+        PQfreeCancel(oldCancelConn);
+
+    m_cancelConn = PQgetCancel(m_pgConn);
+
+    m_pgcancelMutex.Unlock();
+}
+
+void dbgPgConn::ResetCancelConn(void)
+{
+    PGcancel   *oldCancelConn;
+
+    m_pgcancelMutex.Lock();
+    oldCancelConn = m_cancelConn;
+    /* be sure handle_sigint doesn't use pointer while freeing */
+    m_cancelConn = NULL;
+
+    if (oldCancelConn != NULL)
+       PQfreeCancel(oldCancelConn);
+
+    m_pgcancelMutex.Unlock();
+
+}
+
Index: debugger/ctlCodeWindow.cpp
===================================================================
--- debugger/ctlCodeWindow.cpp	(revision 8217)
+++ debugger/ctlCodeWindow.cpp	(working copy)
@@ -1260,7 +1260,10 @@
 
 void ctlCodeWindow::stopDebugging()
 {
-    m_dbgConn->startCommand( wxString::Format( m_commandAbortTarget, m_sessionHandle.c_str()), GetEventHandler(), RESULT_ID_ABORT_TARGET );
+    wxLogInfo( _("Stop Debugging"), _( "" ));
+    m_dbgConn->stopDebugging();
+    //m_dbgConn->startCommand( wxString::Format( m_commandAbortTarget, m_sessionHandle.c_str()), GetEventHandler(), RESULT_ID_ABORT_TARGET );
+    m_targetAborted = true;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
Index: debugger/dbgPgThread.cpp
===================================================================
--- debugger/dbgPgThread.cpp	(revision 8217)
+++ debugger/dbgPgThread.cpp	(working copy)
@@ -67,6 +67,8 @@
 
     wxLogInfo( wxT( "Queueing: %s" ), command.c_str());
 
+    fprintf(stdout, "%s", (char*)(command.c_str()));
+
     m_queueMutex.Unlock();
 
     m_queueCounter.Post();
@@ -108,6 +110,12 @@
         m_owner.setNoticeHandler( noticeHandler, this );
 
         m_currentCommand = getNextCommand();
+        if (m_currentCommand == NULL)
+        {
+            Yield();
+            wxMilliSleep(10);
+            continue;
+        }
         wxString command = m_currentCommand->getCommand();
 
         wxLogInfo( wxT( "Executing: %s" ), command.c_str());
@@ -142,6 +150,7 @@
                 return this;
             }
 
+            m_owner.SetCancelConn();
             int ret = PQiSendQueryPreparedOut(m_owner.getConnection(), 
                                                 stmt.mb_str(wxConvUTF8),
                                                 params->nParams,
@@ -151,6 +160,7 @@
                                                 1);
             if (ret != 1)
             {
+                m_owner.ResetCancelConn();
                 wxLogError(_( "Couldn't execute the callable statement: %s" ), stmt.c_str());
                 PQclear(res);
                 return this;
@@ -165,7 +175,7 @@
         	{
         		if (die || TestDestroy())
         		{
-        		    PQrequestCancel(m_owner.getConnection());
+                            m_owner.Cancel();
         		    return this;
         	    }
         			
@@ -193,6 +203,7 @@
         	    PQclear(dummy);
                 result = PQiGetOutResult(m_owner.getConnection());
         	}
+            m_owner.ResetCancelConn();
         }
         else
         {
@@ -202,11 +213,13 @@
         	// Note that this is all async code as far as libpq is concerned to
         	// ensure we can always bail out when required, without leaving threads
         	// hanging around.
+            m_owner.SetCancelConn();
             int ret = PQsendQuery(m_owner.getConnection(), command.mb_str(wxConvUTF8));
         	
             if (ret != 1)
             {
                 wxLogError(_( "Couldn't execute the query (%s): %s" ), command.c_str(), wxString(PQerrorMessage(m_owner.getConnection()), *conv).c_str());
+                m_owner.ResetCancelConn();
                 return this;
             }
         	
@@ -215,6 +228,7 @@
         	{
         		if (die || TestDestroy())
         		{
+                        m_owner.Cancel();
         		    PQrequestCancel(m_owner.getConnection());
         		    return this;
         	    }
@@ -237,6 +251,7 @@
         			
                 result = part;
         	}
+                m_owner.ResetCancelConn();
         	
 #if defined (__WXMSW__) || (EDB_LIBPQ)
         }
@@ -346,6 +361,12 @@
 
     wxLogInfo( wxT( "%d commands in queue" ), m_commandQueue.GetCount());
 
+    if (m_commandQueue.GetCount() == 0)
+    {
+        m_queueMutex.Unlock();
+        return NULL;
+    }
+
     ThreadCommandList::Node * node = m_commandQueue.GetFirst();
         
     result = node->GetData();
@@ -356,3 +377,15 @@
 
     return( result );
 }
+
+void dbgPgThread::clearAllCommands()
+{
+    m_queueMutex.Lock();
+    while (!m_commandQueue.IsEmpty())
+    {
+        ThreadCommandList::Node * node = m_commandQueue.GetFirst();
+        m_commandQueue.DeleteNode( node );
+    }
+    m_queueMutex.Unlock();
+}
+
