fix(satp-hermes): destroy knex connection pools on shutdown
Summary
- Destroy orphaned knex connection pool in
createDBRepository()after migrations complete - Destroy
localRepositoryandremoteRepositoryconnection pools in bothshutdown()andkill()methods - Prevents database connection pool leaks across gateway restarts
Problem
The SATP Hermes gateway leaked database connection pools in two ways:
-
createDBRepository()created a localknex()instance for running migrations but never calleddestroy()on it. The pool stayed alive after the function returned. -
shutdown()andkill()never calleddestroy()onthis.localRepositoryorthis.remoteRepository, even thoughKnexLocalLogRepository.destroy()exists.
This caused:
SQLITE_BUSYerrors under concurrent load (two pools fighting for WAL lock)- PostgreSQL
max_connectionsexhaustion after repeated restarts - Node.js process hanging on shutdown (open pool keeps event loop alive)
Changes
File: packages/cactus-plugin-satp-hermes/src/main/typescript/plugin-satp-hermes-gateway.ts
| Method | Change |
|---|---|
createDBRepository() |
Added await database.destroy() after database.migrate.latest() |
shutdown() |
Added await this.localRepository.destroy() and await this.remoteRepository.destroy() before monitor service shutdown |
kill() |
Added same repository destroy calls before return |
Test plan
- [ ] Start SATP gateway with SQLite (default), verify no
SQLITE_BUSYerrors under concurrent sessions - [ ] Verify gateway process exits cleanly on SIGTERM without requiring SIGKILL
- [ ] Restart gateway multiple times, confirm no connection pool accumulation
- [ ] Run existing SATP integration tests to verify no regressions
Fixes #4184
Please squash the commits into single commit
@sandeepnRES I've squashed all commits into a single commit as requested. Please let me know if any further changes are needed.
@mn-ram thank you for the code. Generally looks good. I left a minor comment. Could you please either 1: run the test plan and assert the changes work or 2: add programatic tests that verify the same?
Pushed an update based on Copilot feedback:
-Moved cleanup logic into a destroyRepositories() helper with try/catch for each destroy().
-Now calling it in both early-return and normal paths, so DB pools don’t leak.
-Wrapped migration knex() in try/finally to always release the pool.
Will run tests and share results soon.
Test results: Ran unit tests — all relevant tests passed, including shutdown tests. Logs show the shutdown flow is working correctly.
No issues found.
Integration tests will run in CI.