{"record":{"id":"0380cb572512dd68","repo":"apache/flink","slug":"the-runtime-context-has-not-been-initialized","errorCode":null,"errorMessage":"The runtime context has not been initialized.","messagePattern":"The runtime context has not been initialized\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/functions/AbstractRichFunction.java","lineNumber":51,"sourceCode":"    private static final long serialVersionUID = 1L;\n\n    // --------------------------------------------------------------------------------------------\n    //  Runtime context access\n    // --------------------------------------------------------------------------------------------\n\n    private transient RuntimeContext runtimeContext;\n\n    @Override\n    public void setRuntimeContext(RuntimeContext t) {\n        this.runtimeContext = t;\n    }\n\n    @Override\n    public RuntimeContext getRuntimeContext() {\n        if (this.runtimeContext != null) {\n            return this.runtimeContext;\n        } else {\n            throw new IllegalStateException(\"The runtime context has not been initialized.\");\n        }\n    }\n\n    @Override\n    public IterationRuntimeContext getIterationRuntimeContext() {\n        if (this.runtimeContext == null) {\n            throw new IllegalStateException(\"The runtime context has not been initialized.\");\n        } else if (this.runtimeContext instanceof IterationRuntimeContext) {\n            return (IterationRuntimeContext) this.runtimeContext;\n        } else {\n            throw new IllegalStateException(\"This stub is not part of an iteration step function.\");\n        }\n    }\n\n    // --------------------------------------------------------------------------------------------\n    //  Default life cycle methods\n    // --------------------------------------------------------------------------------------------\n","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/functions/AbstractRichFunction.java#L33-L69","documentation":"Thrown by AbstractRichFunction.getRuntimeContext() when the Flink runtime has not yet injected the RuntimeContext via setRuntimeContext(). The runtime normally calls setRuntimeContext() before open(); calling getRuntimeContext() outside the Flink execution lifecycle (e.g. in a unit test, in a constructor, or in a static initializer) will trigger this.","triggerScenarios":"A RichFunction subclass calls getRuntimeContext() before the Flink runtime has called setRuntimeContext(). This happens when: the function is instantiated and tested outside a Flink pipeline, getRuntimeContext() is called in the constructor or a field initializer, or the function is used in a way that bypasses the normal open() lifecycle.","commonSituations":"Unit tests that instantiate a RichFunction directly without wrapping it in a RuntimeUDFContext. Calling getRuntimeContext() in a constructor or instance initializer block. Using a RichFunction inside a custom source/sink that does not go through the standard operator lifecycle.","solutions":["Move all getRuntimeContext() calls inside the open() method or later lifecycle methods (map(), flatMap(), etc.), never in constructors or field initializers.","In unit tests, wrap the RichFunction with a RuntimeUDFContext or use Flink's test utilities (e.g. KeyedOneInputStreamOperatorTestHarness or AbstractStreamOperatorTestHarness) that properly set the context.","If you need runtime info during construction, defer it to open() and store it in a field there."],"exampleFix":"// before\npublic class MyMapper extends RichMapFunction<String, String> {\n    private final Counter counter = getRuntimeContext().getMetricGroup().counter(\"myCounter\");\n}\n\n// after — initialize in open()\npublic class MyMapper extends RichMapFunction<String, String> {\n    private transient Counter counter;\n\n    @Override\n    public void open(OpenContext ctx) throws Exception {\n        counter = getRuntimeContext().getMetricGroup().counter(\"myCounter\");\n    }\n}","handlingStrategy":"validation","validationCode":"// Ensure getRuntimeContext() is only called from open() or processing methods\n// In tests, wrap the function properly:\nRichMapFunction<String, String> fn = new MyMapper();\nfn.setRuntimeContext(new RuntimeUDFContext(...)); // use test utility\n// Then it is safe to call fn.getRuntimeContext()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call getRuntimeContext() in constructors or field initializers — only in open() or later lifecycle methods.","In unit tests, use StreamingRuntimeContext or RuntimeUDFContext test helpers to set the context before calling methods that use it.","Mark fields that depend on the runtime context as transient and initialize them in open()."],"tags":["rich-function","runtime-context","lifecycle","user-error"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}