{"record":{"id":"5bf53438c72dfb4b","repo":"prestodb/presto","slug":"expected-current-entry-to-be-closed-but-was-opened-5bf534","errorCode":null,"errorMessage":"Expected current entry to be closed but was opened","messagePattern":"Expected current entry to be closed but was opened","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/block/RowBlockBuilder.java","lineNumber":273,"sourceCode":"            fieldBlocks[i] = fieldBlockBuilders[i].build();\n        }\n        return createRowBlockInternal(0, positionCount, hasNullRow ? rowIsNull : null, fieldBlockOffsets, fieldBlocks);\n    }\n\n    @Override\n    public String toString()\n    {\n        return format(\"RowBlockBuilder(%d){numFields=%d, positionCount=%d\", hashCode(), numFields, getPositionCount());\n    }\n\n    @Override\n    public BlockBuilder appendStructure(Block block)\n    {\n        if (!(block instanceof AbstractSingleRowBlock)) {\n            throw new IllegalStateException(\"Expected AbstractSingleRowBlock\");\n        }\n        if (currentEntryOpened) {\n            throw new IllegalStateException(\"Expected current entry to be closed but was opened\");\n        }\n        currentEntryOpened = true;\n\n        int blockPositionCount = block.getPositionCount();\n        if (blockPositionCount != numFields) {\n            throw new IllegalArgumentException(format(\"block position count (%s) is not equal to number of fields (%s)\", blockPositionCount, numFields));\n        }\n        for (int i = 0; i < blockPositionCount; i++) {\n            if (block.isNull(i)) {\n                fieldBlockBuilders[i].appendNull();\n            }\n            else {\n                block.writePositionTo(i, fieldBlockBuilders[i]);\n            }\n        }\n\n        closeEntry();\n        return this;","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/block/RowBlockBuilder.java#L255-L291","documentation":"appendStructure requires that no entry is currently open: it sets currentEntryOpened = true itself after validating. Calling it while a previous beginBlockEntry()/appendStructure entry is still open would nest entries and desynchronize field offsets, so the library throws IllegalStateException.","triggerScenarios":"Calling appendStructure() after beginBlockEntry() without closeEntry(), or calling appendStructure() twice in a row without closing the entry opened by the first call.","commonSituations":"Bulk-copy loops copying row blocks that call appendStructure per position but forget that it auto-opens and requires closeEntry after each position; exception paths between appendStructure and closeEntry.","solutions":["Call closeEntry() after each appendStructure() before the next append","Restructure copy loops: appendStructure(block); rowBuilder.closeEntry();","Use try/finally around the copy loop body","Never mix beginBlockEntry-based writes with appendStructure for the same row"],"exampleFix":"// before\nfor (int i = 0; i < block.getPositionCount(); i++) {\n    rowBuilder.appendStructure(block.getBlock(i)); // second call throws: entry still open\n}\n// after\nfor (int i = 0; i < block.getPositionCount(); i++) {\n    rowBuilder.appendStructure(block.getBlock(i));\n    rowBuilder.closeEntry();\n}","handlingStrategy":"validation","validationCode":"// maintain open-entry state around appendStructure usage\nboolean safeAppendStructure(RowBlockBuilder b, Block row) {\n    // appendStructure must not be called while another entry is open;\n    // ensure your wrapper never leaves an entry open:\n    b.appendStructure(row);\n    b.closeEntry(); // immediately close what appendStructure opened\n    return true;\n}\n","typeGuard":null,"tryCatchPattern":"try {\n    rowBuilder.appendStructure(block);\n    rowBuilder.closeEntry();\n} catch (IllegalStateException e) {\n    if (e.getMessage().contains(\"Expected current entry to be closed\")) {\n        throw new IllegalStateException(\"nested appendStructure: previous entry not closed\", e);\n    }\n    throw e;\n}","preventionTips":["Treat appendStructure as begin+open: always follow with closeEntry()","Never interleave beginBlockEntry and appendStructure for the same row","In copy loops, close the entry inside the loop body","Wrap the loop in try/finally"],"tags":["presto","row-block","illegal-state","appendstructure"],"backgroundTag":"block-entry-still-open","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"}