jbonofre commented on code in PR #1492:
URL: https://github.com/apache/arrow-java/pull/1492#discussion_r4612288212


##########
c/src/main/java/org/apache/arrow/c/ArrowArrayStreamReader.java:
##########
@@ -64,7 +64,16 @@ public boolean loadNextBatch() throws IOException {
       if (array.snapshot().release == NULL) {
         return false;
       }
-      Data.importIntoVectorSchemaRoot(allocator, array, getVectorSchemaRoot(), 
provider);
+      try {
+        Data.importIntoVectorSchemaRoot(allocator, array, 
getVectorSchemaRoot(), provider);
+      } catch (Throwable t) {
+        // If the import failed before moving the batch out of the array, e.g. 
allocating the
+        // schema, the batch is still in the array: release it.
+        if (array.snapshot().release != NULL) {
+          array.release();
+        }
+        throw t;
+      }

Review Comment:
   The PR wraps the 4-argument call:
   
   ```java
       try {
         Data.importIntoVectorSchemaRoot(allocator, array, 
getVectorSchemaRoot(), provider);
       } catch (Throwable t) {
         if (array.snapshot().release != NULL) {
           array.release();
         }
         throw t;
       }
   ```
   
   The 4-argument `Data.importIntoVectorSchemaRoot` defaults to 
`closeImportedStructs = true`. Inside the 5-argument 
`Data.importIntoVectorSchemaRoot`:
   
   1. Line 487 calls `importIntoVector(..., closeImportedStructs)` which calls 
`array.close()`.
   2. Line 488-492 then run `StructVectorUnloader.getRecordBatch()` and 
`VectorLoader.load(recordBatch)`.
   
   If anything throws during unloading or loading (for example, an 
`OutOfMemoryException` in `BaseVariableWidthVector.setReaderAndWriterIndex` 
when allocating an offset buffer, or an `IllegalArgumentException` in 
`StructVectorUnloader` / `VectorLoader`), execution enters the `catch 
(Throwable t)` block while `array` is already closed. Calling 
`array.snapshot()` then hits `checkNotNull(data, "ArrowArray is already 
closed")` in `ArrowArray.memoryAddress`, throwing `NullPointerException` and 
masking the original exception `t`.
   
   I suggest to pass `closeImportedStructs = false` since the enclosing `try 
(ArrowArray array = ArrowArray.allocateNew(allocation))` already owns closing 
`array` (this also removes the redundant double-`close()` on the happy path):
   
   ```suggestion
       try {
         Data.importIntoVectorSchemaRoot(
             allocator, array, getVectorSchemaRoot(), provider, /* 
closeImportedStructs= */ false);
       } catch (Throwable t) {
         // If the import failed before moving the batch out of the array, e.g. 
allocating the
         // schema, the batch is still in the array: release it.
         if (array.snapshot().release != NULL) {
           try {
             array.release();
           } catch (Throwable suppressed) {
             t.addSuppressed(suppressed);
           }
         }
         throw t;
       }
   ```



-- 
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