mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-16 09:35:36 -05:00
Fix for : 4045: Cannot cancel a Query. Query runs too long. (And SMO update) (#780)
* Fix for : 4045: Cannot cancel a Query. Query runs too long. HandleExecuteRequest was returning a task to awaiter - which was getting waited on. changed it to async function to start task in new thread and return nothing to awaiter . Also the cancellation token set by cancel request was getting checked only after making the connection to execute batches. Added an additional check for cancellation token befor the connection has been made. Fix for 4319: Error showing dbs when using AAD in... 1.5.0-alpha.74 David has already created a new version of SMO nuget with the fix. - incorporating the same (150.18096.0-preview). * Adding awaitable internal task for tests to run properly * Adding more cancel tests
This commit is contained in:
@@ -226,7 +226,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!HasExecuted)
|
||||
if (!HasExecuted && !HasCancelled)
|
||||
{
|
||||
throw new InvalidOperationException("Query has not been executed.");
|
||||
}
|
||||
@@ -259,6 +259,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// if the query has been cancelled (before execution started)
|
||||
/// </summary>
|
||||
public bool HasCancelled { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The text of the query to execute
|
||||
/// </summary>
|
||||
@@ -280,6 +285,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
}
|
||||
|
||||
// Issue the cancellation token for the query
|
||||
this.HasCancelled = true;
|
||||
cancellationSource.Cancel();
|
||||
}
|
||||
|
||||
@@ -368,9 +374,12 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
ReliableSqlConnection sqlConn = null;
|
||||
try
|
||||
{
|
||||
// check for cancellation token before actually making connection
|
||||
cancellationSource.Token.ThrowIfCancellationRequested();
|
||||
|
||||
// Mark that we've internally executed
|
||||
hasExecuteBeenCalled = true;
|
||||
|
||||
|
||||
// Don't actually execute if there aren't any batches to execute
|
||||
if (Batches.Length == 0)
|
||||
{
|
||||
@@ -429,6 +438,10 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (e is OperationCanceledException)
|
||||
{
|
||||
await BatchMessageSent(new ResultMessage(SR.QueryServiceQueryCancelled, false, null));
|
||||
}
|
||||
// Call the query failure callback
|
||||
if (QueryFailed != null)
|
||||
{
|
||||
|
||||
@@ -109,6 +109,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
/// </summary>
|
||||
internal ConcurrentDictionary<string, Query> ActiveQueries => queries.Value;
|
||||
|
||||
/// <summary>
|
||||
/// Internal task for testability
|
||||
/// </summary>
|
||||
internal Task WorkTask { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Instance of the connection service, used to get the connection info for a given owner URI
|
||||
/// </summary>
|
||||
@@ -130,7 +135,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
/// <summary>
|
||||
/// Holds a map from the simple execute unique GUID and the underlying task that is being ran
|
||||
/// </summary>
|
||||
private readonly Lazy<ConcurrentDictionary<string, Task>> simpleExecuteRequests =
|
||||
private readonly Lazy<ConcurrentDictionary<string, Task>> simpleExecuteRequests =
|
||||
new Lazy<ConcurrentDictionary<string, Task>>(() => new ConcurrentDictionary<string, Task>());
|
||||
|
||||
/// <summary>
|
||||
@@ -177,23 +182,34 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
/// <summary>
|
||||
/// Handles request to execute a selection of a document in the workspace service
|
||||
/// </summary>
|
||||
internal Task HandleExecuteRequest(ExecuteRequestParamsBase executeParams,
|
||||
internal async Task HandleExecuteRequest(ExecuteRequestParamsBase executeParams,
|
||||
RequestContext<ExecuteRequestResult> requestContext)
|
||||
{
|
||||
// Setup actions to perform upon successful start and on failure to start
|
||||
Func<Query, Task<bool>> queryCreateSuccessAction = async q => {
|
||||
await requestContext.SendResult(new ExecuteRequestResult());
|
||||
Logger.Write(TraceEventType.Stop, $"Response for Query: '{executeParams.OwnerUri} sent. Query Complete!");
|
||||
return true;
|
||||
};
|
||||
Func<string, Task> queryCreateFailureAction = message =>
|
||||
try
|
||||
{
|
||||
Logger.Write(TraceEventType.Warning, $"Failed to create Query: '{executeParams.OwnerUri}. Message: '{message}' Complete!");
|
||||
return requestContext.SendError(message);
|
||||
};
|
||||
// Setup actions to perform upon successful start and on failure to start
|
||||
Func<Query, Task<bool>> queryCreateSuccessAction = async q =>
|
||||
{
|
||||
await requestContext.SendResult(new ExecuteRequestResult());
|
||||
Logger.Write(TraceEventType.Stop, $"Response for Query: '{executeParams.OwnerUri} sent. Query Complete!");
|
||||
return true;
|
||||
};
|
||||
Func<string, Task> queryCreateFailureAction = message =>
|
||||
{
|
||||
Logger.Write(TraceEventType.Warning, $"Failed to create Query: '{executeParams.OwnerUri}. Message: '{message}' Complete!");
|
||||
return requestContext.SendError(message);
|
||||
};
|
||||
|
||||
// Use the internal handler to launch the query
|
||||
return InterServiceExecuteQuery(executeParams, null, requestContext, queryCreateSuccessAction, queryCreateFailureAction, null, null);
|
||||
// Use the internal handler to launch the query
|
||||
WorkTask = Task.Run(async () =>
|
||||
{
|
||||
await InterServiceExecuteQuery(executeParams, null, requestContext, queryCreateSuccessAction, queryCreateFailureAction, null, null);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await requestContext.SendError(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -219,14 +235,14 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
await requestContext.SendError(SR.QueryServiceQueryInvalidOwnerUri);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
ConnectParams connectParams = new ConnectParams
|
||||
{
|
||||
OwnerUri = randomUri,
|
||||
Connection = connInfo.ConnectionDetails,
|
||||
Type = ConnectionType.Default
|
||||
};
|
||||
|
||||
|
||||
Task workTask = Task.Run(async () => {
|
||||
await ConnectionService.Connect(connectParams);
|
||||
|
||||
@@ -243,26 +259,26 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
try
|
||||
{
|
||||
// check to make sure any results were recieved
|
||||
if (query.Batches.Length == 0
|
||||
|| query.Batches[0].ResultSets.Count == 0)
|
||||
if (query.Batches.Length == 0
|
||||
|| query.Batches[0].ResultSets.Count == 0)
|
||||
{
|
||||
await requestContext.SendError(SR.QueryServiceResultSetHasNoResults);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
long rowCount = query.Batches[0].ResultSets[0].RowCount;
|
||||
// check to make sure there is a safe amount of rows to load into memory
|
||||
if (rowCount > Int32.MaxValue)
|
||||
if (rowCount > Int32.MaxValue)
|
||||
{
|
||||
await requestContext.SendError(SR.QueryServiceResultSetTooLarge);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
SimpleExecuteResult result = new SimpleExecuteResult
|
||||
{
|
||||
RowCount = rowCount,
|
||||
ColumnInfo = query.Batches[0].ResultSets[0].Columns,
|
||||
Rows = new DbCellValue[0][]
|
||||
Rows = new DbCellValue[0][]
|
||||
};
|
||||
|
||||
if (rowCount > 0)
|
||||
@@ -280,8 +296,8 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
result.Rows = subset.Rows;
|
||||
}
|
||||
await requestContext.SendResult(result);
|
||||
}
|
||||
finally
|
||||
}
|
||||
finally
|
||||
{
|
||||
Query removedQuery;
|
||||
Task removedTask;
|
||||
@@ -306,7 +322,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
|
||||
ActiveSimpleExecuteRequests.TryAdd(randomUri, workTask);
|
||||
}
|
||||
catch(Exception ex)
|
||||
catch (Exception ex)
|
||||
{
|
||||
await requestContext.SendError(ex.ToString());
|
||||
}
|
||||
@@ -335,7 +351,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// Handles a request to get an execution plan
|
||||
/// </summary>
|
||||
internal async Task HandleExecutionPlanRequest(QueryExecutionPlanParams planParams,
|
||||
@@ -502,17 +518,17 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
/// <param name="queryFailureFunc">
|
||||
/// Callback to call when query has completed execution with errors. May be <c>null</c>.
|
||||
/// </param>
|
||||
public async Task InterServiceExecuteQuery(ExecuteRequestParamsBase executeParams,
|
||||
public async Task InterServiceExecuteQuery(ExecuteRequestParamsBase executeParams,
|
||||
ConnectionInfo connInfo,
|
||||
IEventSender queryEventSender,
|
||||
Func<Query, Task<bool>> queryCreateSuccessFunc,
|
||||
Func<string, Task> queryCreateFailFunc,
|
||||
Query.QueryAsyncEventHandler querySuccessFunc,
|
||||
Query.QueryAsyncEventHandler querySuccessFunc,
|
||||
Query.QueryAsyncErrorEventHandler queryFailureFunc)
|
||||
{
|
||||
Validate.IsNotNull(nameof(executeParams), executeParams);
|
||||
Validate.IsNotNull(nameof(queryEventSender), queryEventSender);
|
||||
|
||||
|
||||
Query newQuery;
|
||||
try
|
||||
{
|
||||
@@ -622,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)
|
||||
if (ActiveQueries.TryGetValue(executeParams.OwnerUri, out oldQuery) && (oldQuery.HasExecuted || oldQuery.HasCancelled))
|
||||
{
|
||||
oldQuery.Dispose();
|
||||
ActiveQueries.TryRemove(executeParams.OwnerUri, out oldQuery);
|
||||
@@ -815,7 +831,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
return GetSqlTextFromSelectionData(docRequest.OwnerUri, docRequest.QuerySelection);
|
||||
}
|
||||
|
||||
// If it is a document statement, we'll retrieve the text from the document
|
||||
// If it is a document statement, we'll retrieve the text from the document
|
||||
ExecuteDocumentStatementParams stmtRequest = request as ExecuteDocumentStatementParams;
|
||||
if (stmtRequest != null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user