deeplearning4j/deeplearning4j · error · ND4JWorkspaceException

Assertion failed: expected workspace for array type " +…

Error message

Assertion failed: expected workspace for array type " + arrayType + " to be open: " + msg

What it means

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.

Solutions

  1. Open the workspace before the call: wrap the code in try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(...).notifyScopeEntered()) { ... }.
  2. 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.
  3. 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.
  4. 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).

Example fix

// before
INDArray out = network.output(features); // throws: workspace for INFERENCE not open

// after
try (MemoryWorkspace ws = Nd4j.getWorkspaceManager()
        .getWorkspaceForCurrentThread(WorkspacesNames.WORKSPACE_INFERENCE)
        .notifyScopeEntered()) {
    INDArray out = network.output(features);
}
Defensive patterns

Strategy: validation

Validate before calling

if (!Nd4j.getMemoryManager().getCurrentWorkspace().equals(expectedWs)
        || !workspaceMgr.isWorkspaceOpen(arrayType)) {
    throw new IllegalStateException("Workspace for " + arrayType + " must be open before this call");
}

Type guard

public static boolean workspaceReady(BaseWorkspaceMgr<?> mgr, Enum<?> arrayType) {
    return mgr.isWorkspaceOpen(arrayType);
}

Try / catch

try {
    workspaceMgr.assertOpen(arrayType, "forward pass");
    // scoped array work here
} catch (ND4JWorkspaceException e) {
    log.error("Workspace not open for {}; open it with notifyScopeEntered() on this thread", arrayType, e);
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07). Data as JSON: /api/errors/97ae39e237e7ffd2. Report an issue: GitHub.

Appendix: source

Thrown at nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/workspace/BaseWorkspaceMgr.java:250

            scopeOutOfWs.remove(forEnum);
        }
        setWorkspaceName(forEnum, wsName);
        setConfiguration(forEnum, configuration);
    }

    @Override
    public boolean isWorkspaceOpen(@NonNull T arrayType) {
        validateConfig(arrayType);
        if(!scopeOutOfWs.contains(arrayType)) {
            return Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(getWorkspaceName(arrayType));
        }
        return true;
    }

    @Override
    public void assertOpen(T arrayType, String msg) throws ND4JWorkspaceException {
        if(!scopeOutOfWs.contains(arrayType) && !isWorkspaceOpen(arrayType)) {
            throw new ND4JWorkspaceException("Assertion failed: expected workspace for array type " + arrayType
                    + " to be open: " + msg);
        }
    }

    @Override
    public void assertNotOpen(@NonNull T arrayType, @NonNull String msg) {
        if(!scopeOutOfWs.contains(arrayType) && isWorkspaceOpen(arrayType)){
            throw new ND4JWorkspaceException("Assertion failed: expected workspace for array type " + arrayType
                    + " to not be open: " + msg);
        }
    }

    @Override
    public void setCurrentWorkspace(T arrayType) {
        validateConfig(arrayType);
        if(scopeOutOfWs.contains(arrayType)) {
            Nd4j.getMemoryManager().setCurrentWorkspace(null);
        } else {

View on GitHub (pinned to 4c22ac5fe4)