{"record":{"id":"a8cc2e1eee7035dc","repo":"elastic/elasticsearch","slug":"an-internal-error-occurred-attempting-to-define-th-a8cc2e","errorCode":null,"errorMessage":"An internal error occurred attempting to define the factory class [{}].","messagePattern":"An internal error occurred attempting to define the factory class \\[(.+?)\\]\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"critical","filePath":"modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessScriptEngine.java","lineNumber":357,"sourceCode":"\n        GeneratorAdapter deterAdapter = new GeneratorAdapter(\n            Opcodes.ASM5,\n            isResultDeterministic,\n            writer.visitMethod(Opcodes.ACC_PUBLIC, methodName, isResultDeterministic.getDescriptor(), null, null)\n        );\n        deterAdapter.visitCode();\n        deterAdapter.push(scriptScope.isDeterministic());\n        deterAdapter.returnValue();\n        deterAdapter.endMethod();\n\n        writer.visitEnd();\n        Class<?> factory = loader.defineFactory(className.replace('/', '.'), writer.toByteArray());\n\n        try {\n            return context.factoryClazz.cast(factory.getConstructor().newInstance());\n        } catch (Exception exception) {\n            // Catch everything to let the user know this is something caused internally.\n            throw new IllegalStateException(\n                \"An internal error occurred attempting to define the factory class [\" + className + \"].\",\n                exception\n            );\n        }\n    }\n\n    private static void writeNeedsMethods(Class<?> clazz, ClassWriter writer, Set<String> extractedVariables) {\n        for (Method method : clazz.getMethods()) {\n            if (method.getName().startsWith(\"needs\")\n                && method.getReturnType().equals(boolean.class)\n                && method.getParameterTypes().length == 0) {\n                String name = method.getName();\n                name = name.substring(5);\n                name = Character.toLowerCase(name.charAt(0)) + name.substring(1);\n\n                org.objectweb.asm.commons.Method needs = new org.objectweb.asm.commons.Method(\n                    method.getName(),\n                    MethodType.methodType(boolean.class).toMethodDescriptorString()","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessScriptEngine.java#L339-L375","documentation":"PainlessScriptEngine throws this IllegalStateException when the dynamically generated script factory class cannot be instantiated via reflection. After the compiler writes the factory bytecode and defines it through the loader, it calls factory.getConstructor().newInstance(); any failure (NoSuchMethodException, SecurityException, IllegalAccessException, InstantiationException, InvocationTargetException) is wrapped in this generic error. This is an internal compiler/infrastructure failure, not a user script error.","triggerScenarios":"The factory class was generated but its no-arg constructor is missing, inaccessible, or threw during execution. This typically indicates a Painless compiler bug, a classloader/SecurityManager restriction, an ASM version mismatch, or bytecode generation corruption.","commonSituations":"Running a Painless build with incompatible ASM or JDK versions. Custom security managers blocking reflective instantiation. Bugs in the Painless compiler that emit malformed factory bytecode. Rare edge cases in script compilation.","solutions":["Check the caused-by exception (the 'exception' argument) for the root cause — it names the actual reflective failure.","If caused by SecurityManager/permissions, ensure the Elasticsearch security policy permits reflective access to generated script classes.","If no obvious cause, report a Painless bug with the script source, ES version, and JDK version; this is not expected under normal operation."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// This is an internal error; inspect the caused-by exception:\n// try { scriptEngine.compile(...); } catch (IllegalStateException e) { log.error('factory init failed', e.getCause()); }","preventionTips":["Keep Elasticsearch and the JDK on supported, compatible versions.","Review the caused-by exception for the root reflective failure.","Report reproducible cases as Painless bugs with full version info."],"tags":["painless","elasticsearch","internal-error","reflection","factory-class"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}