diff --git a/presto-main/src/main/java/com/facebook/presto/execution/SqlQueryManager.java b/presto-main/src/main/java/com/facebook/presto/execution/SqlQueryManager.java
index <HASH>..<HASH> 100644
--- a/presto-main/src/main/java/com/facebook/presto/execution/SqlQueryManager.java
+++ b/presto-main/src/main/java/com/facebook/presto/execution/SqlQueryManager.java
@@ -476,17 +476,22 @@ public class SqlQueryManager
                         }
                     }
                 });
-                queryExecutor.execute(new Runnable()
-                {
-                    @Override
-                    public void run()
+                if (queryExecution.getQueryInfo().getState().isDone()) {
+                    settableFuture.set(null);
+                }
+                else {
+                    queryExecutor.execute(new Runnable()
                     {
-                        try (SetThreadName setThreadName = new SetThreadName("Query-%s", queryExecution.getQueryInfo().getQueryId())) {
-                            stats.queryStarted();
-                            queryExecution.start();
+                        @Override
+                        public void run()
+                        {
+                            try (SetThreadName setThreadName = new SetThreadName("Query-%s", queryExecution.getQueryInfo().getQueryId())) {
+                                stats.queryStarted();
+                                queryExecution.start();
+                            }
                         }
-                    }
-                });
+                    });
+                }
                 return settableFuture;
             }
         }
Fix resource leak in query queue

Queries can transition to the CANCELED state before they are started. In
particular, this happens if a user cancels their query while it is
queued. In this case, the state change listener is never called, because
the query is already finished, and this leaks one slot in the query
queue.