{"record":{"id":"7c1f1884b4e7db2a","repo":"flowable/flowable-engine","slug":"could-not-close-initialdircontext-correctly","errorCode":null,"errorMessage":"Could not close InitialDirContext correctly!","messagePattern":"Could not close InitialDirContext correctly!","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"modules/flowable-ldap/src/main/java/org/flowable/ldap/LDAPConnectionUtil.java","lineNumber":70,"sourceCode":"                properties.put(customParameter, ldapConfigurator.getCustomConnectionParameters().get(customParameter));\n            }\n        }\n\n        InitialDirContext context;\n        try {\n            context = new InitialDirContext(properties);\n        } catch (NamingException e) {\n            LOGGER.warn(\"Could not create InitialDirContext for LDAP connection: {}\", e.getMessage());\n            throw new FlowableException(\"Could not create InitialDirContext for LDAP connection: \" + e.getMessage(), e);\n        }\n        return context;\n    }\n\n    public static void closeDirectoryContext(InitialDirContext initialDirContext) {\n        try {\n            initialDirContext.close();\n        } catch (NamingException e) {\n            LOGGER.warn(\"Could not close InitialDirContext correctly!\", e);\n        }\n    }\n\n}\n","sourceCodeStart":52,"sourceCodeEnd":75,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-ldap/src/main/java/org/flowable/ldap/LDAPConnectionUtil.java#L52-L75","documentation":"WARN-only log in LDAPConnectionUtil.closeDirectoryContext: closing the InitialDirContext threw a NamingException. The context could not be released cleanly, potentially leaking a connection to the LDAP server, but no exception is propagated to the caller.","triggerScenarios":"NamingException during InitialDirContext.close() — typically because the underlying connection is already dead/timed out, or the context was created against a server that dropped the session.","commonSituations":"Long-running apps with idle LDAP connections reaped by a firewall/load balancer; LDAP server restarts while contexts are open; missing com.sun.jndi.ldap.connect.timeout/read timeout settings leading to stale contexts.","solutions":["Ignore if infrequent — the connection was being closed anyway; no functional impact","If frequent, set keep-alive/timeouts: com.sun.jndi.ldap.connect.timeout and com.sun.jndi.ldap.read.timeout in the LDAP properties","Check network middleboxes (firewalls, LBs) dropping idle LDAP sessions and adjust idle timeouts","Ensure contexts are closed in a finally block right after use to minimize stale sessions"],"exampleFix":"// before\nProperties p = new Properties();\np.put(Context.PROVIDER_URL, \"ldap://host:389\");\n// after: add timeouts so stale contexts are detected early\np.put(\"com.sun.jndi.ldap.connect.timeout\", \"5000\");\np.put(\"com.sun.jndi.ldap.read.timeout\", \"30000\");","handlingStrategy":"try-catch","validationCode":"// avoid stale contexts: check connectivity before reuse\ntry { ctx.search(\"\", \"(objectClass=*)\", new SearchControls()); } catch (NamingException e) { ctx = recreateContext(); }","typeGuard":null,"tryCatchPattern":"try {\n    ctx.close();\n} catch (NamingException e) {\n    log.warn(\"Could not close InitialDirContext correctly!\", e);\n}","preventionTips":["Close contexts in finally/try-with-resources immediately after use","Configure com.sun.jndi.ldap.connect.timeout and read.timeout","Coordinate firewall/LB idle timeouts with LDAP keep-alive settings"],"tags":["flowable","ldap","resource-cleanup","connection-leak","naming-exception"],"backgroundTag":"broken-pipe","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-18T11:17:12.947Z"}