{"record":{"id":"a941678b99207a37","repo":"deeplearning4j/deeplearning4j","slug":"input-array-was-closed-for-argument-argname-a","errorCode":null,"errorMessage":"Input array was closed! For argument '<argName>' at index <i> for op '<ownName>' (<opName>) with id <id>","messagePattern":"Input array was closed! For argument '<argName>' at index <i> for op '<ownName>' \\(<opName>\\) with id <id>","errorType":"exception","errorClass":"ND4JIllegalStateException","httpStatus":null,"severity":"error","filePath":"nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/autodiff/samediff/internal/InferenceSession.java","lineNumber":782,"sourceCode":"                            inputArrays[i] = null;  //Default to null\n                            if (list != null && !list.isEmpty()) {\n                                for (INDArray arr : list) {\n                                    if (arr != null && !arr.wasClosed()) {\n                                        inputArrays[i] = arr;\n                                        break;\n                                    }\n                                }\n                            }\n                            break;\n                        default:\n                            throw new IllegalStateException(\"Unsupported SDValue type for op \" + opName + \" input \" + argName + \": \" + argValue.getSdValueType());\n                    }\n                }\n\n                // Centralized check for null or closed arrays, performed immediately after resolving each input.\n                // This prevents nulls from ever reaching the OpContext.\n                if (inputArrays[i] != null && inputArrays[i].wasClosed()) {\n                    throw new ND4JIllegalStateException(\"Input array was closed! For argument '\" + argName + \"' at index \" + i + \" for op '\" + op.getOwnName() + \"' (\" + op.opName() + \") with id \" + inputArrays[i].getId());\n                }\n            }\n\n            for(int i = 0; i < inputArrays.length; i++) {\n                if(inputArrays[i] == null) {\n                    // This should not happen for regular ops if control flow is handled correctly by Merge nodes.\n                    // The previous implementation would silently skip the op and propagate nulls, hiding the root cause.\n                    // By throwing an exception, we pinpoint the exact location where control flow resolution failed.\n                    throw new ND4JIllegalStateException(\"Unexpected null input array at index \" + i + \" for operation '\" +\n                            op.getOwnName() + \"' (op name: \" + op.opName() + \"). Input variable name: '\" + argNames[i] + \"'. \" +\n                            \"This indicates a failure in control flow management, where a null from an inactive branch \" +\n                            \"was propagated past a Merge node. \\nDependent values for this operation:\\n\" +\n                            getDependentValuesString(variableValues, argNames[i]));\n                }\n            }\n\n            opContext.setInputArrays(inputArrays);\n        }","sourceCodeStart":764,"sourceCodeEnd":800,"githubUrl":"https://github.com/deeplearning4j/deeplearning4j/blob/4c22ac5fe4a8350d05d224e7f4499429f7f69c93/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/autodiff/samediff/internal/InferenceSession.java#L764-L800","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Detach arrays you keep beyond their workspace: arr.detach() before storing/reusing them","Create/attach the input array inside the same workspace scope as the execution, or disable problematic workspaces (Nd4j.getWorkspaceManager().setDefaultWorkspaceConfiguration(...))","Recreate the input array each execution instead of caching the same INDArray reference","Check for double-close patterns (calling close() yourself while the framework also manages the array)"],"exampleFix":"// before: array from a closed workspace reused in next iteration\nINDArray cached = firstRunOutput; // attached to now-closed workspace\nsd.output(inputs, \"out\"); // -> Input array was closed!\n// after: detach before caching\nINDArray cached = firstRunOutput.detach();\nsd.output(Map.of(\"in\", cached), \"out\");","handlingStrategy":"validation","validationCode":"for (INDArray a : inputArrays) {\n    if (a != null && a.wasClosed()) throw new IllegalStateException(\"Refusing to run: input array already closed (id=\" + a.getId() + \")\");\n}","typeGuard":"boolean usable(INDArray a) { return a != null && !a.wasClosed(); }","tryCatchPattern":"try { sd.output(inputs, outName); } catch (ND4JIllegalStateException e) { if (e.getMessage().contains(\"Input array was closed\")) { refreshInputArrays(); return sd.output(inputs, outName); } throw e; }","preventionTips":["Detach arrays cached across executions (arr.detach())","Keep input arrays within the workspace scope of the execution","Never call close() on arrays the framework/workspace also manages","Re-create inputs per iteration rather than reusing references from closed runs"],"tags":["nd4j","memory-management","workspaces","closed-array"],"backgroundTag":"internal-invariant-violation","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"}