From c86f43618c32e76a35da22296f0d36aa6f44f84c Mon Sep 17 00:00:00 2001 From: udeeshagautam <46980425+udeeshagautam@users.noreply.github.com> Date: Tue, 14 May 2019 16:15:09 -0700 Subject: [PATCH] Proposed fix for : 3653 : Cancel not working even after disconnecting (#797) * Proposed changes to stop query execution if an exception has occured before/during batch execution * Sending the error message was causing a test to fail - so removing for now. We can update the test to expect this is needed. --- .../QueryExecution/Query.cs | 10 ++++++++-- .../QueryExecution/QueryExecutionService.cs | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs index e5c85450..a7c470f2 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs @@ -226,7 +226,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution { get { - if (!HasExecuted && !HasCancelled) + if (!HasExecuted && !HasCancelled && !HasErrored) { throw new InvalidOperationException("Query has not been executed."); } @@ -264,6 +264,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution /// public bool HasCancelled { get; private set; } + /// + /// if the query has errored out (before batch execution started) + /// + public bool HasErrored { get; private set; } + /// /// The text of the query to execute /// @@ -393,7 +398,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution } return; } - + // Locate and setup the connection DbConnection queryConnection = await ConnectionService.Instance.GetOrOpenConnection(editorConnection.OwnerUri, ConnectionType.Query); sqlConn = queryConnection as ReliableSqlConnection; @@ -438,6 +443,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution } catch (Exception e) { + HasErrored = true; if (e is OperationCanceledException) { await BatchMessageSent(new ResultMessage(SR.QueryServiceQueryCancelled, false, null)); diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/QueryExecutionService.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/QueryExecutionService.cs index 027ba542..2eece988 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/QueryExecutionService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/QueryExecutionService.cs @@ -638,7 +638,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution // if any oldQuery exists on the executeParams.OwnerUri but it has not yet executed, // then shouldn't we cancel and clean out that query since we are about to create a new query object on the current OwnerUri. // - if (ActiveQueries.TryGetValue(executeParams.OwnerUri, out oldQuery) && (oldQuery.HasExecuted || oldQuery.HasCancelled)) + if (ActiveQueries.TryGetValue(executeParams.OwnerUri, out oldQuery) && (oldQuery.HasExecuted || oldQuery.HasCancelled || oldQuery.HasErrored)) { oldQuery.Dispose(); ActiveQueries.TryRemove(executeParams.OwnerUri, out oldQuery);