{"record":{"id":"5048a77a4861a3ec","repo":"prestodb/presto","slug":"current-entry-must-be-closed-before-the-block-can-5048a7","errorCode":null,"errorMessage":"Current entry must be closed before the block can be built","messagePattern":"Current entry must be closed before the block can be built","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/block/VariableWidthBlockBuilder.java","lineNumber":400,"sourceCode":"    {\n        int positionCount = getPositionCount();\n        checkValidRegion(positionCount, positionOffset, length);\n\n        int[] newOffsets = compactOffsets(offsets, positionOffset, length);\n        boolean[] newValueIsNull = null;\n        if (hasNullValue) {\n            newValueIsNull = compactArray(valueIsNull, positionOffset, length);\n        }\n        Slice slice = compactSlice(sliceOutput.getUnderlyingSlice(), offsets[positionOffset], newOffsets[length]);\n\n        return new VariableWidthBlock(0, length, slice, newOffsets, newValueIsNull);\n    }\n\n    @Override\n    public Block build()\n    {\n        if (currentEntrySize > 0) {\n            throw new IllegalStateException(\"Current entry must be closed before the block can be built\");\n        }\n        return new VariableWidthBlock(0, positions, sliceOutput.slice(), offsets, hasNullValue ? valueIsNull : null);\n    }\n\n    @Override\n    public BlockBuilder newBlockBuilderLike(BlockBuilderStatus blockBuilderStatus)\n    {\n        int currentSizeInBytes = positions == 0 ? positions : (getOffset(positions) - getOffset(0));\n        return new VariableWidthBlockBuilder(blockBuilderStatus, calculateBlockResetSize(positions), calculateBlockResetSize(currentSizeInBytes));\n    }\n\n    @Override\n    public BlockBuilder newBlockBuilderLike(BlockBuilderStatus blockBuilderStatus, int expectedEntries)\n    {\n        int newSize = max(calculateBlockResetSize(positions), expectedEntries);\n        int currentSizeInBytes = offsets[positions];\n        return new VariableWidthBlockBuilder(blockBuilderStatus, newSize, BlockUtil.calculateNestedStructureResetSize(currentSizeInBytes, positions, newSize));\n    }","sourceCodeStart":382,"sourceCodeEnd":418,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/block/VariableWidthBlockBuilder.java#L382-L418","documentation":"build() refuses to materialize a Block while an entry write is still in progress (currentEntrySize > 0). The library requires every open entry to be closed (closeEntry) so the offsets array is consistent before a valid VariableWidthBlock can be constructed.","triggerScenarios":"Calling build(), getSliceKeysBlock, or any of the block-producing helpers (sliceBlockAllNulls, sliceDictBlockSomeNulls) after writeBytes/writeSlice without a matching closeEntry(), or when the last entry's size is still buffered in currentEntrySize.","commonSituations":"Forgetting closeEntry() after the final write in a loop; an exception mid-write leaving an entry open; refactoring that moves build() earlier than entry completion; writing a null via writeNull instead of closeEntry ordering mistakes.","solutions":["Call closeEntry() after every writeBytes/writeSlice sequence, including the last one before build()","Audit control flow so build() is only reached when no entry is open","Wrap entry-writing loops in try/finally to close entries on failure paths, or discard the builder on error","Replace an unfinished partial entry with writeNull() if the value should be null"],"exampleFix":"// before\nbuilder.writeBytes(value, 0, value.length());\nBlock block = builder.build(); // throws\n// after\nbuilder.writeBytes(value, 0, value.length());\nbuilder.closeEntry();\nBlock block = builder.build();","handlingStrategy":"validation","validationCode":"// track open-entry state in your writer helper\nboolean entryOpen = false;\n// before build(): if (entryOpen) { builder.closeEntry(); entryOpen = false; }\nBlock block = builder.build();","typeGuard":null,"tryCatchPattern":"try { return builder.build(); } catch (IllegalStateException e) { builder.closeEntry(); return builder.build(); }","preventionTips":["Call closeEntry immediately after each write sequence, including the last","Use a helper class that encapsulates write+closeEntry pairs","On exception during entry writing, abandon the builder rather than reusing it"],"tags":["block-builder","unclosed-entry","illegal-state"],"backgroundTag":"illegal-state-object-not-initialized","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}