Skip to content

Commit 7ad32dd

Browse files
fix(jni): release the schema export in VortexWriter.build() (#18)
## Summary `VortexWriter.build()` exports an Arrow schema that native creation borrows. Closing only the schema struct leaks the child allocations. Release the export before closing the struct, for both URI and stream writers. The cleanup uses separate try-with-resources entries: the release callback runs before the struct closes, both cleanup steps are attempted, and cleanup failures are suppressed onto an existing export or creation failure. The release callback is checked before invocation so an export that did not install one can still be cleaned up. ## Review follow-up - Parameterize the allocation regression across URI/stream writers, flat/nested schemas, and successful/rejected creation (eight cases). - Check that native metadata validation errors retain their cause and leave no Arrow allocations. - Use a child of the shared root allocator for isolated leak assertions. - Keep native schema borrowing unchanged. The separate address-based batch-schema leak from the review is tracked in #53. ## Validation The original revision was checked with `./gradlew --parallel --continue javadoc check`. The latest cleanup and test changes have not been run; build, test, lint, and formatting execution remain the user's choice under repository guidance. I used AI tools to help write and review this change. --------- Signed-off-by: gitRasheed <[email protected]> Signed-off-by: Christopher Wirt <[email protected]> Co-authored-by: Christopher Wirt <[email protected]>
1 parent 85d8c8a commit 7ad32dd

2 files changed

Lines changed: 65 additions & 4 deletions

File tree

‎java/vortex-jni/src/main/java/dev/vortex/api/VortexWriter.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import dev.vortex.VortexCleaner;
88
import dev.vortex.io.NativeWritable;
99
import dev.vortex.jni.NativeWriter;
10+
import java.io.Closeable;
1011
import java.io.IOException;
1112
import java.util.HashMap;
1213
import java.util.Map;
@@ -146,8 +147,14 @@ public Builder putMetadata(String key, byte[] value) {
146147
* metadata
147148
*/
148149
public VortexWriter build() throws IOException {
149-
ArrowSchema ffi = ArrowSchema.allocateNew(allocator);
150-
try {
150+
// Native code borrows the schema. Release its children before closing the struct;
151+
// separate resources ensure both cleanups run and preserve the original failure.
152+
try (ArrowSchema ffi = ArrowSchema.allocateNew(allocator);
153+
Closeable releaseSchema = () -> {
154+
if (ffi.snapshot().release != 0) {
155+
ffi.release();
156+
}
157+
}) {
151158
Data.exportSchema(allocator, arrowSchema, null, ffi);
152159
final long pointer;
153160
try {
@@ -164,8 +171,6 @@ public VortexWriter build() throws IOException {
164171
throw new IOException("failed to create writer for " + destination() + " (ptr=" + pointer + ")");
165172
}
166173
return new VortexWriter(pointer);
167-
} finally {
168-
ffi.close();
169174
}
170175
}
171176

‎java/vortex-jni/src/test/java/dev/vortex/jni/JNIWriterTest.java‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import dev.vortex.api.VortexWriteSummary;
2020
import dev.vortex.api.VortexWriter;
2121
import dev.vortex.arrow.ArrowAllocation;
22+
import dev.vortex.io.NativeWritable;
23+
import java.io.ByteArrayOutputStream;
2224
import java.io.IOException;
2325
import java.nio.file.Files;
2426
import java.nio.file.Path;
@@ -49,6 +51,8 @@
4951
import org.junit.jupiter.api.Disabled;
5052
import org.junit.jupiter.api.Test;
5153
import org.junit.jupiter.api.io.TempDir;
54+
import org.junit.jupiter.params.ParameterizedTest;
55+
import org.junit.jupiter.params.provider.CsvSource;
5256

5357
public final class JNIWriterTest {
5458
private static final String ARROW_EXTENSION_NAME = "ARROW:extension:name";
@@ -169,6 +173,58 @@ public void testCreateWriter() throws IOException {
169173
source.arrowSchema(allocator));
170174
}
171175

176+
@ParameterizedTest(name = "stream={0}, nested={1}, rejected={2}")
177+
@CsvSource({
178+
"false, false, false",
179+
"false, true, false",
180+
"true, false, false",
181+
"true, true, false",
182+
"false, false, true",
183+
"false, true, true",
184+
"true, false, true",
185+
"true, true, true"
186+
})
187+
public void testCreateWriterReleasesSchemaExport(boolean stream, boolean nested, boolean rejected)
188+
throws IOException {
189+
String writePath = tempDir.resolve("test_schema_release.vortex").toUri().toString();
190+
Session session = Session.create();
191+
Schema schema = nested ? mapSchema() : personSchema();
192+
ByteArrayOutputStream output = new ByteArrayOutputStream();
193+
NativeWritable writable = new NativeWritable() {
194+
@Override
195+
public void write(byte[] buffer, int offset, int length) {
196+
output.write(buffer, offset, length);
197+
}
198+
199+
@Override
200+
public void flush() {}
201+
202+
@Override
203+
public void close() {}
204+
};
205+
try (BufferAllocator allocator =
206+
ArrowAllocation.rootAllocator().newChildAllocator("schema-release", 0, Long.MAX_VALUE)) {
207+
VortexWriter.Builder builder = stream
208+
? VortexWriter.builder(session, writable, schema, allocator)
209+
: VortexWriter.builder(session, writePath, schema, allocator);
210+
if (rejected) {
211+
// Native validation rejects this after the schema has been exported, for both destinations.
212+
builder.putMetadata("k".repeat(1024), new byte[] {1});
213+
IOException failure = assertThrows(IOException.class, builder::build);
214+
assertNotNull(failure.getCause());
215+
assertTrue(failure.getCause().getMessage().contains("metadata key"));
216+
} else {
217+
try (VortexWriter writer = builder.build()) {
218+
assertEquals(0, allocator.getAllocatedMemory());
219+
}
220+
if (stream) {
221+
assertTrue(output.size() > 0, "stream writer should have finalized a file");
222+
}
223+
}
224+
assertEquals(0, allocator.getAllocatedMemory());
225+
}
226+
}
227+
172228
@Test
173229
public void testCreateWriterPlainLocalPath() throws IOException {
174230
Path outputPath = tempDir.resolve("test_create_plain_path.vortex");

0 commit comments

Comments
 (0)