{"record":{"id":"97ae39e237e7ffd2","repo":"deeplearning4j/deeplearning4j","slug":"assertion-failed-expected-workspace-for-array-typ","errorCode":null,"errorMessage":"Assertion failed: expected workspace for array type \" + arrayType + \" to be open: \" + msg","messagePattern":"Assertion failed: expected workspace for array type \" \\+ arrayType \\+ \" to be open: \" \\+ msg","errorType":"validation","errorClass":"ND4JWorkspaceException","httpStatus":null,"severity":"error","filePath":"nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/workspace/BaseWorkspaceMgr.java","lineNumber":250,"sourceCode":"            scopeOutOfWs.remove(forEnum);\n        }\n        setWorkspaceName(forEnum, wsName);\n        setConfiguration(forEnum, configuration);\n    }\n\n    @Override\n    public boolean isWorkspaceOpen(@NonNull T arrayType) {\n        validateConfig(arrayType);\n        if(!scopeOutOfWs.contains(arrayType)) {\n            return Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(getWorkspaceName(arrayType));\n        }\n        return true;\n    }\n\n    @Override\n    public void assertOpen(T arrayType, String msg) throws ND4JWorkspaceException {\n        if(!scopeOutOfWs.contains(arrayType) && !isWorkspaceOpen(arrayType)) {\n            throw new ND4JWorkspaceException(\"Assertion failed: expected workspace for array type \" + arrayType\n                    + \" to be open: \" + msg);\n        }\n    }\n\n    @Override\n    public void assertNotOpen(@NonNull T arrayType, @NonNull String msg) {\n        if(!scopeOutOfWs.contains(arrayType) && isWorkspaceOpen(arrayType)){\n            throw new ND4JWorkspaceException(\"Assertion failed: expected workspace for array type \" + arrayType\n                    + \" to not be open: \" + msg);\n        }\n    }\n\n    @Override\n    public void setCurrentWorkspace(T arrayType) {\n        validateConfig(arrayType);\n        if(scopeOutOfWs.contains(arrayType)) {\n            Nd4j.getMemoryManager().setCurrentWorkspace(null);\n        } else {","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/deeplearning4j/deeplearning4j/blob/4c22ac5fe4a8350d05d224e7f4499429f7f69c93/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/workspace/BaseWorkspaceMgr.java#L232-L268","documentation":"BaseWorkspaceMgr.assertOpen(arrayType, msg) verifies that the MemoryWorkspace associated with the given array type is currently open before code touches arrays scoped to it. If the workspace is not open — and the array type is not on the scopeOutOfWs exemption list — it throws ND4JWorkspaceException. ND4J workspaces must be opened with ws.scopeOutOfWorkspace()/try-with-resources before allocating or consuming scoped arrays; this assert catches code that forgot.","triggerScenarios":"Calling assertOpen (or an API that delegates to it) while the workspace for arrayType is closed: accessing/creating arrays of that type outside a workspace.open() scope, leaving a try-with-resources block too early, nested code expecting an inherited workspace that was never opened, or misconfigured scopeOutOfWs for a type that legitimately runs without a workspace.","commonSituations":"Forgetting try(MemoryWorkspace ws = workspace.notifyScopeEntered()) before forward/backward passes; calling training/eval code from a thread where the workspace was never opened; mismatched open/close after an exception; configuring workspace modes (e.g. for inference) without registering the array type as scope-out-of-workspace.","solutions":["Open the workspace before the call: wrap the code in try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(...).notifyScopeEntered()) { ... }.","Ensure the workspace is opened on the same thread that consumes the arrays — workspaces are thread-local, so open it inside the worker thread/loop.","If this array type is intentionally used without a workspace, register it via workspaceMgr.setScopeOutOfWs(arrayType) (or use the appropriate WorkspaceMgr configuration) so assertOpen exempts it.","Check that no early return/exception skips the scope entry, and that nested scope usage isn't closing the workspace prematurely (use scopeOutOfWorkspace for nested blocks)."],"exampleFix":"// before\nINDArray out = network.output(features); // throws: workspace for INFERENCE not open\n\n// after\ntry (MemoryWorkspace ws = Nd4j.getWorkspaceManager()\n        .getWorkspaceForCurrentThread(WorkspacesNames.WORKSPACE_INFERENCE)\n        .notifyScopeEntered()) {\n    INDArray out = network.output(features);\n}","handlingStrategy":"validation","validationCode":"if (!Nd4j.getMemoryManager().getCurrentWorkspace().equals(expectedWs)\n        || !workspaceMgr.isWorkspaceOpen(arrayType)) {\n    throw new IllegalStateException(\"Workspace for \" + arrayType + \" must be open before this call\");\n}","typeGuard":"public static boolean workspaceReady(BaseWorkspaceMgr<?> mgr, Enum<?> arrayType) {\n    return mgr.isWorkspaceOpen(arrayType);\n}","tryCatchPattern":"try {\n    workspaceMgr.assertOpen(arrayType, \"forward pass\");\n    // scoped array work here\n} catch (ND4JWorkspaceException e) {\n    log.error(\"Workspace not open for {}; open it with notifyScopeEntered() on this thread\", arrayType, e);\n    throw e;\n}","preventionTips":["Always open workspaces with try-with-resources: try(MemoryWorkspace ws = wsMgr.notifyScopeEntered()) { ... }.","Open workspaces on the same thread that allocates/consumes the arrays (they are thread-local).","For types that legitimately run without a workspace, set them scope-out-of-workspace in the WorkspaceMgr configuration.","In loops (training/eval), open/close the workspace per iteration and verify no early return skips scope entry."],"tags":["workspace","memory-management","nd4j","java","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"4c22ac5fe4a8350d05d224e7f4499429f7f69c93","analyzedAt":"2026-09-07T05:27:04.714Z","contentChangedAt":"2026-09-07T05:27:04.714Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}