Skip to content

Commit 58bb7c8

Browse files
mtopolnikclaude
andcommitted
Fix mapping leak on openExisting error path
openExisting creates the RW mapping only after the recovery scan completes. The catch block used to close the fd but not the mapping, so a throw between a successful Files.mmap and the constructor return (realistically only an OOME allocating the MmapSegment) leaked the mapping. Track the address across the try block and munmap it in the catch before closing the fd, mirroring the cleanup pattern create() uses. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 0a345a1 commit 58bb7c8

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

  • core/src/main/java/io/questdb/client/cutlass/qwp/client/sf/cursor

core/src/main/java/io/questdb/client/cutlass/qwp/client/sf/cursor/MmapSegment.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ public static MmapSegment openExisting(FilesFacade ff, String path) {
296296
if (fd < 0) {
297297
throw new MmapSegmentException("openRW failed for " + path);
298298
}
299+
long addr = Files.FAILED_MMAP_ADDRESS;
299300
try {
300301
RecoveryScan scan = scanFile(ff, fd, path, fileSize);
301302
if (scan.tornTailBytes > 0) {
@@ -306,13 +307,16 @@ public static MmapSegment openExisting(FilesFacade ff, String path) {
306307
+ "Investigate disk health or unexpected writer crash.",
307308
path, scan.tornTailBytes, scan.lastGood, fileSize, scan.frameCount);
308309
}
309-
long addr = Files.mmap(fd, fileSize, 0, Files.MAP_RW, MemoryTag.MMAP_DEFAULT);
310+
addr = Files.mmap(fd, fileSize, 0, Files.MAP_RW, MemoryTag.MMAP_DEFAULT);
310311
if (addr == Files.FAILED_MMAP_ADDRESS) {
311312
throw new MmapSegmentException("mmap failed for " + path);
312313
}
313314
return new MmapSegment(path, fd, addr, fileSize, scan.baseSeq,
314315
scan.lastGood, scan.frameCount, false, scan.tornTailBytes);
315316
} catch (Throwable t) {
317+
if (addr != Files.FAILED_MMAP_ADDRESS) {
318+
Files.munmap(addr, fileSize, MemoryTag.MMAP_DEFAULT);
319+
}
316320
ff.close(fd);
317321
throw t;
318322
}

0 commit comments

Comments
 (0)