{"record":{"id":"c16aeafe8c0dfcb1","repo":"apache/seatunnel","slug":"mdccontext-is-not-activated","errorCode":null,"errorMessage":"MDCContext is not activated","messagePattern":"MDCContext is not activated","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"seatunnel-api/src/main/java/org/apache/seatunnel/api/tracing/MDCContext.java","lineNumber":103,"sourceCode":"                MDC.put(PIPELINE_ID, String.valueOf(pipelineId));\n            }\n            if (taskId != null) {\n                MDC.put(TASK_ID, String.valueOf(taskId));\n            }\n        } catch (Throwable e) {\n            log.error(\"Failed to put MDC context\", e);\n            throw e;\n        }\n        return this;\n    }\n\n    public synchronized MDCContext deactivate() {\n        if (this == EMPTY) {\n            return this;\n        }\n\n        if (this.toRestore == null) {\n            throw new IllegalStateException(\"MDCContext is not activated\");\n        }\n\n        try {\n            MDC.remove(JOB_ID);\n            MDC.remove(PIPELINE_ID);\n            MDC.remove(TASK_ID);\n        } catch (Throwable e) {\n            log.error(\"Failed to clear MDC context\", e);\n            throw e;\n        }\n\n        if (this.toRestore != null) {\n            this.toRestore.activate();\n        }\n\n        return this;\n    }\n","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/apache/seatunnel/blob/cf67b549a7a6c35fa0beb12d83c62892427ea919/seatunnel-api/src/main/java/org/apache/seatunnel/api/tracing/MDCContext.java#L85-L121","documentation":"MDCContext tracks whether it was activated (toRestore set to the prior MDC map). deactivate() throws IllegalStateException when called on a context that was never activated via activate(), or that was already deactivated (toRestore nulled afterwards). This guards against unbalanced activate/deactivate pairs corrupting thread-local MDC state.","triggerScenarios":"Calling deactivate() on MDCContext.EMPTY (returns this, no throw) is safe, but calling deactivate() on a fresh MDCContext.of(jobId) without a prior activate(), or a second deactivate() after the first already restored and cleared toRestore.","commonSituations":"Double-closing an MDCContext (close() calls deactivate, so closing twice or closing a context that was never activated); manual lifecycle code where activate() failed or was skipped; passing a non-activated context into try-with-resources.","solutions":["Ensure activate() is called before deactivate(), e.g. use try-with-resources: context.activate() then try-with-resources on the returned context","Remove the duplicate deactivate()/close() call on the same MDCContext instance","Use MDCContext.of(jobId) only with MDCTracer.tracing(...) wrappers instead of manual activate/deactivate"],"exampleFix":"// before\nMDCContext ctx = MDCContext.of(jobId);\nctx.deactivate(); // throws: never activated\n// after\ntry (MDCContext ctx = MDCContext.of(jobId).activate()) {\n    // job id in MDC\n} // close() deactivates exactly once","handlingStrategy":"try-catch","validationCode":"// before deactivating\nif (ctx != MDCContext.EMPTY && isActivated(ctx)) {\n    ctx.deactivate();\n}","typeGuard":"boolean isActivated(MDCContext ctx) {\n    return ctx != null && ctx != MDCContext.EMPTY;\n}","tryCatchPattern":"try {\n    ctx.deactivate();\n} catch (IllegalStateException e) {\n    log.warn(\"MDCContext deactivate called without matching activate\", e);\n}","preventionTips":["Always use try-with-resources around MDCContext.activate() so close() balances activation","Never call deactivate() twice on the same context","Prefer MDCTracer.tracing(...) wrappers over manual activate/deactivate","Treat MDCContext as opaque; do not copy or reuse instances across threads"],"tags":["java","mdc","tracing","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"cf67b549a7a6c35fa0beb12d83c62892427ea919","analyzedAt":"2026-09-10T21:44:55.265Z","contentChangedAt":"2026-09-10T21:44:55.265Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}