{"record":{"id":"cf85868f46a6b049","repo":"elastic/elasticsearch","slug":"an-internal-error-occurred-attempting-to-define-th","errorCode":null,"errorMessage":"An internal error occurred attempting to define the script [{}].","messagePattern":"An internal error occurred attempting to define the script \\[(.+?)\\]\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"modules/lang-painless/src/main/java/org/elasticsearch/painless/Compiler.java","lineNumber":216,"sourceCode":"        new DefaultStringConcatenationOptimizationPhase().visitClass(classNode, null);\n        new DefaultConstantFoldingOptimizationPhase().visitClass(classNode, null);\n        new DefaultConstantListOptimizationPhase(scriptScope).visitClass(classNode, null);\n        new DefaultEqualityMethodOptimizationPhase(scriptScope).visitClass(classNode, null);\n        new DefaultStaticConstantExtractionPhase().visitClass(classNode, scriptScope);\n        new DefaultIRTreeToASMBytesPhase().visitScript(classNode);\n        byte[] bytes = classNode.getBytes();\n\n        try {\n            Class<? extends PainlessScript> clazz = loader.defineScript(CLASS_NAME, bytes);\n\n            for (Map.Entry<String, Object> staticConstant : scriptScope.getStaticConstants().entrySet()) {\n                clazz.getField(staticConstant.getKey()).set(null, staticConstant.getValue());\n            }\n\n            return scriptScope;\n        } catch (Exception exception) {\n            // Catch everything to let the user know this is something caused internally.\n            throw new IllegalStateException(\"An internal error occurred attempting to define the script [\" + name + \"].\", exception);\n        }\n    }\n\n    /**\n     * Runs the two-pass compiler to generate a Painless script.  (Used by the debugger.)\n     * @param source The source code for the script.\n     * @param settings The CompilerSettings to be used during the compilation.\n     * @return The bytes for compilation.\n     */\n    byte[] compile(String name, String source, CompilerSettings settings, Printer debugStream) {\n        String scriptName = Location.computeSourceName(name);\n        ScriptClassInfo scriptClassInfo = new ScriptClassInfo(painlessLookup, scriptClass);\n        SClass root = Walker.buildPainlessTree(scriptName, source, settings);\n        ScriptScope scriptScope = new ScriptScope(painlessLookup, settings, scriptClassInfo, scriptName, source, root.getIdentifier() + 1);\n        new PainlessSemanticHeaderPhase().visitClass(root, scriptScope);\n        new PainlessSemanticAnalysisPhase().visitClass(root, scriptScope);\n        new PainlessUserTreeToIRTreePhase().visitClass(root, scriptScope);\n        ClassNode classNode = (ClassNode) scriptScope.getDecoration(root, IRNodeDecoration.class).irNode();","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/Compiler.java#L198-L234","documentation":"Compiler.compile wraps any Exception thrown while defining the generated Painless script class (loader.defineScript) or while injecting static constants into it, and rethrows as an IllegalStateException tagged with the script name. This is explicitly a catch-all for internal failures — classloader issues, verification errors, or constant-injection type mismatches — surfaced to the user as 'internal' to signal the cause is not in the script text itself.","triggerScenarios":"Compiling a Painless script that is syntactically valid but whose generated bytecode cannot be defined: classloader constraints, a SecurityManager/entitlement blocking defineClass, a corrupted jar/module path, or a static constant whose type cannot be assigned to the generated field. Observed during first execution of a new or cached script.","commonSituations":"Custom plugin injecting scripts via a non-standard classloader. Module/entitlement changes that block dynamic class definition. JVM/version incompatibility after upgrading the JDK. Concurrent script compilation hitting a classloader race. Mismatched painless API jars on the classpath.","solutions":["Read the wrapped 'exception' cause in the stack trace — it identifies the real failure (e.g. LinkageError, ClassNotFoundException, access exception).","If the cause is an access/entitlement failure, grant the script engine the needed runtime capability or run on a standard distribution.","Restart the node to clear a potentially corrupted classloader state, then recompile.","Simplify the script to a no-op and re-add sections to find which construct produces an uncompilable bytecode shape.","Ensure consistent Elasticsearch/JVM versions across nodes; mixed versions can produce verification errors."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"//try { scriptService.compile(...); } catch (IllegalStateException e) {\n//   if (e.getMessage().startsWith(\"An internal error occurred attempting to define the script\")) {\n//     // Inspect e.getCause() for the real failure (LinkageError, access, etc.)\n//     log.error(\"Painless internal compile failure\", e.getCause());\n//     // Retry on a fresh classloader state, or surface to ops\n//   } else throw e;\n//}","preventionTips":["Always inspect the wrapped cause rather than the top-level message.","Keep all nodes on identical Elasticsearch and JVM versions.","Avoid custom classloader tricks around the script engine.","Reproduce with a minimal script to isolate bytecode-shape issues."],"tags":["painless","script","compiler","classloader","internal"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}