deeplearning4j/deeplearning4j · critical · IllegalStateException

Variable substitution depth too large

Error message

Variable substitution depth too large: {MAX_SUBST} {expr}

What it means

This is output from the std::terminate handler installed by the backward-cpp stack-trace library embedded in libnd4j. When an uncaught C++ exception (or any other terminate condition) reaches the handler, it prints the exception's runtime type name (typeid(e).name(), a mangled name like 'St13runtime_error') to stderr before dumping a stack trace. It is a diagnostic, not an error code you fix directly; the real problem is the exception that escaped.

Solutions

  1. Read the 'Exception message:' line printed immediately after to identify the underlying exception, and fix that root cause (e.g. dtype mismatch, bad shape).
  2. Ensure the JNI layer catches C++ exceptions and converts them to Java exceptions instead of letting them escape.
  3. Decode the mangled type name with `c++filt <name>` to learn the exact exception class thrown.
  4. Re-run under a debugger with a breakpoint on std::terminate to capture the throw site.

Example fix

// before: exception escapes JNI
jlong exec(jlong* ptrs) { return execBroadcast(ptrs...); } // -> std::terminate
// after
jlong exec(jlong* ptrs) {
  try { return execBroadcast(ptrs...); }
  catch (const std::exception& e) { throwJavaRuntimeException(e.what()); return 0; }
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Java/JNI: wrap every native call
try { nativeExec(args); } catch (Throwable t) { log("native op failed", t); }

Try / catch

try { nativeCall(); } catch (const std::exception& e) { convertToJavaException(e.what()); }

Prevention

When it happens

Trigger: An uncaught C++ exception propagates out of native code without being caught at the JNI boundary, invoking the terminate handler. Also any throw of an object derived from std::exception during aborting flows that reaches std::terminate.

Common situations: A datatype_exception or runtime_error thrown by libnd4j kernels escapes through a NativeOps call with no Java-side native exception bridge installed; custom op code compiled without exception handling at the JNI boundary.

Related errors


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

Appendix: source

Thrown at datavec/datavec-api/src/main/java/org/datavec/api/conf/Configuration.java:300

            }
            String var = match.group();
            var = var.substring(2, var.length() - 1); // remove ${ .. }
            String val = null;
            try {
                val = System.getProperty(var);
            } catch (SecurityException se) {
                LOG.warn("Unexpected SecurityException in Configuration", se);
            }
            if (val == null) {
                val = getRaw(var);
            }
            if (val == null) {
                return eval; // return literal ${var}: var is unbound
            }
            // substitute
            eval = eval.substring(0, match.start()) + val + eval.substring(match.end());
        }
        throw new IllegalStateException("Variable substitution depth too large: " + MAX_SUBST + " " + expr);
    }

    /**
     * Get the value of the <code>name</code> property, <code>null</code> if
     * no such property exists.
     *
     * Values are processed for <a href="#VariableExpansion">variable expansion</a>
     * before being returned.
     *
     * @param name the property name.
     * @return the value of the <code>name</code> property,
     *         or null if no such property exists.
     */
    public String get(String name) {
        return substituteVars(getProps().getProperty(name));
    }

    /**

View on GitHub (pinned to 4c22ac5fe4)