RequestEvaluator leaks JDBC connections when a worker thread idles out #161
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
RequestEvaluatorthreads that idle out after the ten-minute wait innotifyAndWait()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, methodnotifyAndWait():Both branches null out
thread/transactorwithout callingtx.closeConnections()first. Every other exit path in this class does call it: thefinallyblock at the end ofrun(),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 plainDriverManager.getConnection(...)— there is no pooling library underneath. A connection is cached once perTransactor(itself pinned to the current thread viaTransactor's ownThreadLocal) and reused for the lifetime of that thread.Transactor.closeConnections()is the only code that actually callsConnection.close(); it is invoked fromabort()but not fromcommit(), and, per above, not from two ofnotifyAndWait()'s four exit paths either.When a
RequestEvaluator's worker thread idles out, itsTransactorand cachedConnectionbecome 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 ownwait_timeoutkills it server-side.getEvaluator()/releaseEvaluator()inApplication.javapoolRequestEvaluatorobjects on a LIFO stack, independent of the underlying OSThread. 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()callsthisEvaluator.invokeInternal(...), which itself callsstartTransactor()and blocks onwait(). The actual JS execution (and any DB access it performs) happens on that evaluator's own managed thread, subject to the samenotifyAndWait()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():(
txwould need to be fetched viaTransactor.getInstance()near the top of the method, as already done elsewhere in the same file.)This was on the wrong track — closing.
notifyAndWait()'s two exit paths are covered:run()'s outerwhile (localThread == thread)loop is wrapped intry { … } 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,threadgets nulled, the outer loop condition fails, andtx.closeConnections()already runs in thefinallyblock 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 thehelma-20190209.jaractually deployed in production by well over a decade. There's no gap here to fix.The real, narrower leak is filed separately as #163.