AMashenkov commented on code in PR #2846:
URL: https://github.com/apache/ignite-3/pull/2846#discussion_r1422236504


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/SessionImpl.java:
##########
@@ -543,55 +542,77 @@ List<AsyncSqlCursor<?>> openedCursors() {
     }
 
     private class ScriptHandler {
-
         private final CompletableFuture<Void> resFut;
 
+        private final AtomicReference<Throwable> cursorCloseErrHolder = new 
AtomicReference<>();
+
         private ScriptHandler(CompletableFuture<Void> resFut) {
             this.resFut = resFut;
         }
 
-        void processFirstResult(AsyncSqlCursor<InternalSqlRow> cursor, 
Throwable t) {
-            if (t != null) {
-                resFut.completeExceptionally(t);
-            } else {
-                int cursorId = registerCursor(cursor);
-                processCursor(cursor, cursorId);
+        void processCursor(AsyncSqlCursor<InternalSqlRow> cursor, Throwable 
scriptError) {
+            if (scriptError != null) {
+                // Stop script execution.
+                completeScriptExecution(scriptError);
+
+                return;
             }
+
+            cursor.closeAsync().whenComplete((ignored, cursorCloseError) -> {
+                if (cursorCloseError != null) {
+                    // Just save the error for later and continue fetching 
cursors.
+                    saveError(cursorCloseError);
+                }
+
+                if (!busyLock.enterBusy()) {
+                    completeScriptExecution(sessionIsClosedException());
+                    return;
+                }
+
+                try {
+                    if (cursor.hasNextResult()) {
+                        cursor.nextResult().whenComplete(this::processCursor);
+                    } else {
+                        completeScriptExecution(null);
+                    }
+                } finally {
+                    busyLock.leaveBusy();
+                }
+            });
         }
 
-        void processCursor(AsyncSqlCursor<InternalSqlRow> cursor, int 
cursorId) {
-            if (!busyLock.enterBusy()) {
-                closeCursor(cursor, cursorId);
+        private void completeScriptExecution(@Nullable Throwable err) {
+            Throwable savedError = cursorCloseErrHolder.get();
+
+            if (err == null && savedError == null) {
+                resFut.complete(null);

Review Comment:
   Is it ok to complete the future synchronously under the busylock?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to