flowable/flowable-engine · warning

Could not close InitialDirContext correctly!

Error message

Could not close InitialDirContext correctly!

What it means

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.

Solutions

  1. Ignore if infrequent — the connection was being closed anyway; no functional impact
  2. If frequent, set keep-alive/timeouts: com.sun.jndi.ldap.connect.timeout and com.sun.jndi.ldap.read.timeout in the LDAP properties
  3. Check network middleboxes (firewalls, LBs) dropping idle LDAP sessions and adjust idle timeouts
  4. Ensure contexts are closed in a finally block right after use to minimize stale sessions

Example fix

// before
Properties p = new Properties();
p.put(Context.PROVIDER_URL, "ldap://host:389");
// after: add timeouts so stale contexts are detected early
p.put("com.sun.jndi.ldap.connect.timeout", "5000");
p.put("com.sun.jndi.ldap.read.timeout", "30000");
Defensive patterns

Strategy: try-catch

Validate before calling

// avoid stale contexts: check connectivity before reuse
try { ctx.search("", "(objectClass=*)", new SearchControls()); } catch (NamingException e) { ctx = recreateContext(); }

Try / catch

try {
    ctx.close();
} catch (NamingException e) {
    log.warn("Could not close InitialDirContext correctly!", e);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of flowable/flowable-engine@d6d39ce1c6 (2026-09-11). Data as JSON: /api/errors/7c1f1884b4e7db2a. Report an issue: GitHub.

Appendix: source

Thrown at modules/flowable-ldap/src/main/java/org/flowable/ldap/LDAPConnectionUtil.java:70

                properties.put(customParameter, ldapConfigurator.getCustomConnectionParameters().get(customParameter));
            }
        }

        InitialDirContext context;
        try {
            context = new InitialDirContext(properties);
        } catch (NamingException e) {
            LOGGER.warn("Could not create InitialDirContext for LDAP connection: {}", e.getMessage());
            throw new FlowableException("Could not create InitialDirContext for LDAP connection: " + e.getMessage(), e);
        }
        return context;
    }

    public static void closeDirectoryContext(InitialDirContext initialDirContext) {
        try {
            initialDirContext.close();
        } catch (NamingException e) {
            LOGGER.warn("Could not close InitialDirContext correctly!", e);
        }
    }

}

View on GitHub (pinned to d6d39ce1c6)