mirror of
https://github.com/czlonkowski/n8n-mcp.git
synced 2026-01-30 06:22:04 +00:00
* fix: critical memory leak from per-session database connections (#542) Each MCP session was creating its own database connection (~900MB), causing OOM kills every ~20 minutes with 3-4 concurrent sessions. Changes: - Add SharedDatabase singleton pattern - all sessions share ONE connection - Reduce session timeout from 30 min to 5 min (configurable) - Add eager cleanup for reconnecting instances - Fix telemetry event listener leak Memory impact: ~900MB/session → ~68MB shared + ~5MB/session overhead Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> Conceived by Romuald Czlonkowski - https://www.aiadvisors.pl/en * fix: resolve test failures from shared database race conditions - Fix `shutdown()` to respect shared database pattern (was directly closing) - Add `await this.initialized` in both `close()` and `shutdown()` to prevent race condition where cleanup runs while initialization is in progress - Add double-shutdown protection with `isShutdown` flag - Export `SharedDatabaseState` type for proper typing - Include error details in debug logs - Add MCP server close to `shutdown()` for consistency with `close()` - Null out `earlyLogger` in `shutdown()` for consistency The CI test failure "The database connection is not open" was caused by: 1. `shutdown()` directly calling `this.db.close()` which closed the SHARED database connection, breaking subsequent tests 2. Race condition where `shutdown()` ran before initialization completed Conceived by Romuald Członkowski - www.aiadvisors.pl/en Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test: add unit tests for shared-database module Add comprehensive unit tests covering: - getSharedDatabase: initialization, reuse, different path error, concurrent requests - releaseSharedDatabase: refCount decrement, double-release guard - closeSharedDatabase: state clearing, error handling, re-initialization - Helper functions: isSharedDatabaseInitialized, getSharedDatabaseRefCount 21 tests covering the singleton database connection pattern used to prevent ~900MB memory leaks per session. Conceived by Romuald Członkowski - www.aiadvisors.pl/en Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Database Layer Unit Tests
This directory contains comprehensive unit tests for the database layer components of n8n-mcp.
Test Coverage
node-repository.ts - 100% Coverage ✅
saveNodemethod with JSON serializationgetNodemethod with JSON deserializationgetAIToolsmethodsafeJsonParseprivate method- Edge cases: large JSON, boolean conversion, invalid JSON handling
template-repository.ts - 80.31% Coverage ✅
- FTS5 initialization and fallback
saveTemplatewith sanitizationgetTemplateandgetTemplatesByNodessearchTemplateswith FTS5 and LIKE fallbackgetTemplatesForTaskwith task mapping- Template statistics and maintenance operations
- Uncovered: Some error paths in FTS5 operations
database-adapter.ts - Tested via Mocks
- Interface compliance tests
- PreparedStatement implementation
- Transaction support
- FTS5 detection logic
- Error handling patterns
Test Strategy
The tests use a mock-based approach to:
- Isolate database operations from actual database dependencies
- Test business logic without requiring real SQLite/sql.js
- Ensure consistent test execution across environments
- Focus on behavior rather than implementation details
Key Test Files
node-repository-core.test.ts- Core NodeRepository functionalitytemplate-repository-core.test.ts- Core TemplateRepository functionalitydatabase-adapter-unit.test.ts- DatabaseAdapter interface and patterns
Running Tests
# Run all database tests
npm test -- tests/unit/database/
# Run with coverage
npm run test:coverage -- tests/unit/database/
# Run specific test file
npm test -- tests/unit/database/node-repository-core.test.ts
Mock Infrastructure
The tests use custom mock implementations:
MockDatabaseAdapter- Simulates database operationsMockPreparedStatement- Simulates SQL statement execution- Mock logger and template sanitizer for external dependencies
This approach ensures tests are fast, reliable, and maintainable.