RequestEvaluator leaks JDBC connections when a worker thread idles out #161

Closed
opened 2026-09-03 14:51:49 +00:00 by tobi · 1 comment
Owner

Summary

RequestEvaluator threads that idle out after the ten-minute wait in notifyAndWait() never release their cached JDBC connection back to the database. Over time this accumulates into hundreds of orphaned, permanently open connections per application instance.

Where

src/main/java/helma/framework/core/RequestEvaluator.java, method notifyAndWait():

try {
    // wait for request, max 10 min
    wait(1000 * 60 * 10);
} catch (InterruptedException ix) {
    // we got interrrupted, releases resources and thread
    thread = null;
    transactor = null;
}

//  if no request arrived, release ressources and thread
if ((reqtype == NONE) && (thread == localThread)) {
    thread = null;
    transactor = null;
}

Both branches null out thread/transactor without calling tx.closeConnections() first. Every other exit path in this class does call it: the finally block at the end of run(), stopTransactor(), and the “another thread already took over” branch earlier in the same method.

Why this leaks a real connection, not just an object reference

DbSource.getConnection() (objectmodel/db/DbSource.java) opens connections via plain DriverManager.getConnection(...) — there is no pooling library underneath. A connection is cached once per Transactor (itself pinned to the current thread via Transactor's own ThreadLocal) and reused for the lifetime of that thread. Transactor.closeConnections() is the only code that actually calls Connection.close(); it is invoked from abort() but not from commit(), and, per above, not from two of notifyAndWait()'s four exit paths either.

When a RequestEvaluator's worker thread idles out, its Transactor and cached Connection become unreachable through Helma's own bookkeeping, but nothing tells the JDBC driver to close the socket. It stays open until the driver's own finalizer runs (unreliable, often delayed indefinitely) or the database's own wait_timeout kills it server-side.

getEvaluator()/releaseEvaluator() in Application.java pool RequestEvaluator objects on a LIFO stack, independent of the underlying OS Thread. So even under steady, non-bursty traffic, evaluators sitting further down the stack can go unused past the ten-minute timeout, cycle their internal thread, and leak a connection — this is not limited to load spikes.

Scheduled/cron jobs go through the identical mechanism: Application.CronRunner.run() calls thisEvaluator.invokeInternal(...), which itself calls startTransactor() and blocks on wait(). The actual JS execution (and any DB access it performs) happens on that evaluator's own managed thread, subject to the same notifyAndWait() gap. So this is one root cause reachable from both HTTP requests and scheduler jobs, not two separate bugs.

Observed impact

On a production instance that had been running about five months without a restart: 245+ established, entirely idle connections to MariaDB from a JVM whose service cgroup reported only 55 live OS threads at the time — far more open sockets than could be explained by currently-alive threads. Restarting the JVM is the only way to reclaim them; the count climbs continuously over uptime and appears to scale with request-rate variance (a lower-traffic instance restarted the same day showed proportionally fewer leaked connections).

Suggested fix

Add the same cleanup call used by every other exit path, in both branches of notifyAndWait():

} catch (InterruptedException ix) {
    if (tx != null) tx.closeConnections();
    thread = null;
    transactor = null;
}

if ((reqtype == NONE) && (thread == localThread)) {
    if (tx != null) tx.closeConnections();
    thread = null;
    transactor = null;
}

(tx would need to be fetched via Transactor.getInstance() near the top of the method, as already done elsewhere in the same file.)

## Summary `RequestEvaluator` threads that idle out after the ten-minute wait in `notifyAndWait()` never release their cached JDBC connection back to the database. Over time this accumulates into hundreds of orphaned, permanently open connections per application instance. ## Where `src/main/java/helma/framework/core/RequestEvaluator.java`, method `notifyAndWait()`: ```java try { // wait for request, max 10 min wait(1000 * 60 * 10); } catch (InterruptedException ix) { // we got interrrupted, releases resources and thread thread = null; transactor = null; } // if no request arrived, release ressources and thread if ((reqtype == NONE) && (thread == localThread)) { thread = null; transactor = null; } ``` Both branches null out `thread`/`transactor` without calling `tx.closeConnections()` first. Every other exit path in this class does call it: the `finally` block at the end of `run()`, `stopTransactor()`, and the “another thread already took over” branch earlier in the same method. ## Why this leaks a real connection, not just an object reference `DbSource.getConnection()` (`objectmodel/db/DbSource.java`) opens connections via plain `DriverManager.getConnection(...)` — there is no pooling library underneath. A connection is cached once per `Transactor` (itself pinned to the current thread via `Transactor`'s own `ThreadLocal`) and reused for the lifetime of that thread. `Transactor.closeConnections()` is the only code that actually calls `Connection.close()`; it is invoked from `abort()` but not from `commit()`, and, per above, not from two of `notifyAndWait()`'s four exit paths either. When a `RequestEvaluator`'s worker thread idles out, its `Transactor` and cached `Connection` become unreachable through Helma's own bookkeeping, but nothing tells the JDBC driver to close the socket. It stays open until the driver's own finalizer runs (unreliable, often delayed indefinitely) or the database's own `wait_timeout` kills it server-side. `getEvaluator()`/`releaseEvaluator()` in `Application.java` pool `RequestEvaluator` *objects* on a LIFO stack, independent of the underlying OS `Thread`. So even under steady, non-bursty traffic, evaluators sitting further down the stack can go unused past the ten-minute timeout, cycle their internal thread, and leak a connection — this is not limited to load spikes. Scheduled/cron jobs go through the identical mechanism: `Application.CronRunner.run()` calls `thisEvaluator.invokeInternal(...)`, which itself calls `startTransactor()` and blocks on `wait()`. The actual JS execution (and any DB access it performs) happens on that evaluator's own managed thread, subject to the same `notifyAndWait()` gap. So this is one root cause reachable from both HTTP requests and scheduler jobs, not two separate bugs. ## Observed impact On a production instance that had been running about five months without a restart: 245+ established, entirely idle connections to MariaDB from a JVM whose service cgroup reported only 55 live OS threads at the time — far more open sockets than could be explained by currently-alive threads. Restarting the JVM is the only way to reclaim them; the count climbs continuously over uptime and appears to scale with request-rate variance (a lower-traffic instance restarted the same day showed proportionally fewer leaked connections). ## Suggested fix Add the same cleanup call used by every other exit path, in both branches of `notifyAndWait()`: ```java } catch (InterruptedException ix) { if (tx != null) tx.closeConnections(); thread = null; transactor = null; } if ((reqtype == NONE) && (thread == localThread)) { if (tx != null) tx.closeConnections(); thread = null; transactor = null; } ``` (`tx` would need to be fetched via `Transactor.getInstance()` near the top of the method, as already done elsewhere in the same file.)
tobi self-assigned this 2026-09-03 14:51:49 +00:00
Author
Owner

This was on the wrong track — closing.

notifyAndWait()'s two exit paths are covered: run()'s outer while (localThread == thread) loop is wrapped in try { … } finally { Transactor tx = Transactor.getInstance(); if (tx != null) { tx.closeConnections(); } }, with an explicit comment ("spans whole execution loop") saying so. When a worker thread idles out, thread gets nulled, the outer loop condition fails, and tx.closeConnections() already runs in the finally block before the thread dies. A live test on antville-dev (burst 24 concurrent requests, then wait past the 10-minute idle timeout) confirmed connections drop back to baseline correctly.

I also checked how far back this safety net goes: git log -S"spans whole execution loop" shows it was added in 2005 and has been there ever since, so it predates even the helma-20190209.jar actually deployed in production by well over a decade. There's no gap here to fix.

The real, narrower leak is filed separately as #163.

This was on the wrong track — closing. `notifyAndWait()`'s two exit paths *are* covered: `run()`'s outer `while (localThread == thread)` loop is wrapped in `try { … } finally { Transactor tx = Transactor.getInstance(); if (tx != null) { tx.closeConnections(); } }`, with an explicit comment ("spans whole execution loop") saying so. When a worker thread idles out, `thread` gets nulled, the outer loop condition fails, and `tx.closeConnections()` already runs in the `finally` block before the thread dies. A live test on antville-dev (burst 24 concurrent requests, then wait past the 10-minute idle timeout) confirmed connections drop back to baseline correctly. I also checked how far back this safety net goes: `git log -S"spans whole execution loop"` shows it was added in 2005 and has been there ever since, so it predates even the `helma-20190209.jar` actually deployed in production by well over a decade. There's no gap here to fix. The real, narrower leak is filed separately as #163.
tobi closed this issue 2026-09-06 07:55:37 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
antville/helma#161
No description provided.