diff --git a/ksqldb-rest-app/src/main/java/io/confluent/ksql/rest/server/KsqlServerEndpoints.java b/ksqldb-rest-app/src/main/java/io/confluent/ksql/rest/server/KsqlServerEndpoints.java index 5c069095ce00..a0c79a47dae8 100644 --- a/ksqldb-rest-app/src/main/java/io/confluent/ksql/rest/server/KsqlServerEndpoints.java +++ b/ksqldb-rest-app/src/main/java/io/confluent/ksql/rest/server/KsqlServerEndpoints.java @@ -124,11 +124,22 @@ public CompletableFuture createQueryPublisher(final String sql, final Context context, final WorkerExecutor workerExecutor, final ApiSecurityContext apiSecurityContext) { - return executeOnWorker( - () -> new QueryEndpoint(ksqlEngine, ksqlConfig, pullQueryExecutor, pullQueryMetrics) - .createQueryPublisher(sql, properties, context, workerExecutor, - ksqlSecurityContextProvider.provide(apiSecurityContext).getServiceContext()), - workerExecutor); + final KsqlSecurityContext ksqlSecurityContext = ksqlSecurityContextProvider + .provide(apiSecurityContext); + return executeOnWorker(() -> { + try { + return new QueryEndpoint(ksqlEngine, ksqlConfig, pullQueryExecutor, pullQueryMetrics) + .createQueryPublisher( + sql, + properties, + context, + workerExecutor, + ksqlSecurityContext.getServiceContext()); + } finally { + ksqlSecurityContext.getServiceContext().close(); + } + }, + workerExecutor); } @Override