deeplearning4j/deeplearning4j · error · ND4JIllegalStateException

Input array was closed! For argument

Error message

Input array was closed! For argument '<argName>' at index <i> for op '<ownName>' (<opName>) with id <id>

What it means

After resolving each input, InferenceSession checks inputArrays[i].wasClosed() and throws ND4JIllegalStateException if the array has been closed (deallocated via the workspaces/memory-management layer). Using a closed INDArray would crash native code, so the session fails fast. This almost always means an array from a previous execution/workspace step was released but the reference was reused.

Solutions

  1. Detach arrays you keep beyond their workspace: arr.detach() before storing/reusing them
  2. Create/attach the input array inside the same workspace scope as the execution, or disable problematic workspaces (Nd4j.getWorkspaceManager().setDefaultWorkspaceConfiguration(...))
  3. Recreate the input array each execution instead of caching the same INDArray reference
  4. Check for double-close patterns (calling close() yourself while the framework also manages the array)

Example fix

// before: array from a closed workspace reused in next iteration
INDArray cached = firstRunOutput; // attached to now-closed workspace
sd.output(inputs, "out"); // -> Input array was closed!
// after: detach before caching
INDArray cached = firstRunOutput.detach();
sd.output(Map.of("in", cached), "out");
Defensive patterns

Strategy: validation

Validate before calling

for (INDArray a : inputArrays) {
    if (a != null && a.wasClosed()) throw new IllegalStateException("Refusing to run: input array already closed (id=" + a.getId() + ")");
}

Type guard

boolean usable(INDArray a) { return a != null && !a.wasClosed(); }

Try / catch

try { sd.output(inputs, outName); } catch (ND4JIllegalStateException e) { if (e.getMessage().contains("Input array was closed")) { refreshInputArrays(); return sd.output(inputs, outName); } throw e; }

Prevention

When it happens

Trigger: Reusing INDArrays from a prior sameDiff.output()/fit() call after workspaces were closed; holding arrays across workspace scope boundaries (e.g. outside a try(Workspace) block) and re-feeding them; GC-driven leak tracking closing arrays; attaching the same array as input to multiple executions with MMAP/indirect reference management.

Common situations: Inference loops reusing cached input arrays with workspace inheritance misconfigured; converting results of an inner execution to feed an outer execution; migrating ND4J versions where workspace defaults changed.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/autodiff/samediff/internal/InferenceSession.java:782

                            inputArrays[i] = null;  //Default to null
                            if (list != null && !list.isEmpty()) {
                                for (INDArray arr : list) {
                                    if (arr != null && !arr.wasClosed()) {
                                        inputArrays[i] = arr;
                                        break;
                                    }
                                }
                            }
                            break;
                        default:
                            throw new IllegalStateException("Unsupported SDValue type for op " + opName + " input " + argName + ": " + argValue.getSdValueType());
                    }
                }

                // Centralized check for null or closed arrays, performed immediately after resolving each input.
                // This prevents nulls from ever reaching the OpContext.
                if (inputArrays[i] != null && inputArrays[i].wasClosed()) {
                    throw new ND4JIllegalStateException("Input array was closed! For argument '" + argName + "' at index " + i + " for op '" + op.getOwnName() + "' (" + op.opName() + ") with id " + inputArrays[i].getId());
                }
            }

            for(int i = 0; i < inputArrays.length; i++) {
                if(inputArrays[i] == null) {
                    // This should not happen for regular ops if control flow is handled correctly by Merge nodes.
                    // The previous implementation would silently skip the op and propagate nulls, hiding the root cause.
                    // By throwing an exception, we pinpoint the exact location where control flow resolution failed.
                    throw new ND4JIllegalStateException("Unexpected null input array at index " + i + " for operation '" +
                            op.getOwnName() + "' (op name: " + op.opName() + "). Input variable name: '" + argNames[i] + "'. " +
                            "This indicates a failure in control flow management, where a null from an inactive branch " +
                            "was propagated past a Merge node. \nDependent values for this operation:\n" +
                            getDependentValuesString(variableValues, argNames[i]));
                }
            }

            opContext.setInputArrays(inputArrays);
        }

View on GitHub (pinned to 4c22ac5fe4)