Lingering File Handles (#71)

Fixing a bug where in various situations, the files used for temporary storage of query results would be leftover. In particular, the following changes were made:
* When the dispose query request is submitted, the corresponding query is now disposed in addition from being removed from the list of active queries
* When a query is cancelled, it is disposed after it is cancelled
* If a query already exists for a given ownerURI, the existing query is disposed before creating a new query
* All queries are disposed when the query execution service is disposed (ie, at shutdown of the service)

A unit test to verify the action of the dispose method for a ResultSet was added.

* Ensuring queries are disposed

Adding logic to dispose any queries when:
* URI that already has a query executes another query
* A request to dispose a query is submitted
* A request to cancel a query is submitted

* Small tweaks for cleanup of query execution service
This commit is contained in:
Benjamin Russell
2016-10-03 11:35:58 -07:00
committed by GitHub
parent f16d0104d0
commit 1b8e9c1e86
2 changed files with 72 additions and 2 deletions

View File

@@ -13,6 +13,7 @@ using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage;
using Microsoft.SqlTools.ServiceLayer.SqlContext;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.ServiceLayer.Workspace;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
using Newtonsoft.Json;
@@ -207,6 +208,9 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
return;
}
// Cleanup the query
result.Dispose();
// Success
await requestContext.SendResult(new QueryDisposeResult
{
@@ -237,6 +241,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
// Cancel the query
result.Cancel();
result.Dispose();
// Attempt to dispose the query
if (!ActiveQueries.TryRemove(cancelParams.OwnerUri, out result))
@@ -268,7 +273,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
/// <summary>
/// Process request to save a resultSet to a file in CSV format
/// </summary>
public async Task HandleSaveResultsAsCsvRequest( SaveResultsAsCsvRequestParams saveParams,
public async Task HandleSaveResultsAsCsvRequest(SaveResultsAsCsvRequestParams saveParams,
RequestContext<SaveResultRequestResult> requestContext)
{
// retrieve query for OwnerUri
@@ -341,7 +346,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
/// <summary>
/// Process request to save a resultSet to a file in JSON format
/// </summary>
public async Task HandleSaveResultsAsJsonRequest( SaveResultsAsJsonRequestParams saveParams,
public async Task HandleSaveResultsAsJsonRequest(SaveResultsAsJsonRequestParams saveParams,
RequestContext<SaveResultRequestResult> requestContext)
{
// retrieve query for OwnerUri
@@ -422,6 +427,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
await requestContext.SendError(ex.Message);
}
}
#endregion
#region Private Helpers
@@ -445,6 +451,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
Query oldQuery;
if (ActiveQueries.TryGetValue(executeParams.OwnerUri, out oldQuery) && oldQuery.HasExecuted)
{
oldQuery.Dispose();
ActiveQueries.TryRemove(executeParams.OwnerUri, out oldQuery);
}
@@ -546,8 +553,22 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
{
foreach (var query in ActiveQueries)
{
if (!query.Value.HasExecuted)
{
try
{
query.Value.Cancel();
}
catch (Exception e)
{
// We don't particularly care if we fail to cancel during shutdown
string message = string.Format("Failed to cancel query {0} during query service disposal: {1}", query.Key, e);
Logger.Write(LogLevel.Warning, message);
}
}
query.Value.Dispose();
}
ActiveQueries.Clear();
}
disposed = true;