1
0

concurrent-locking.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Issue #238 — "database is locked" on concurrent MCP tool calls.
  3. *
  4. * With node:sqlite (real WAL) as the backend, the fixes that remain relevant:
  5. * 1. busy_timeout is a bounded few-second wait (not a 2-minute hang) and WAL is
  6. * active — so a reader never blocks on a concurrent writer.
  7. * 2. The MCP ToolHandler reuses the default instance when a tool passes a
  8. * projectPath pointing at the default project, instead of opening a SECOND
  9. * connection to the same DB.
  10. */
  11. import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
  12. import * as fs from 'fs';
  13. import * as path from 'path';
  14. import * as os from 'os';
  15. import CodeGraph from '../src';
  16. import { ToolHandler } from '../src/mcp/tools';
  17. import { DatabaseConnection } from '../src/db';
  18. /** Normalize a PRAGMA read across return shapes (array | object | scalar). */
  19. function pragmaValue(raw: unknown, key: string): unknown {
  20. const row = Array.isArray(raw) ? raw[0] : raw;
  21. if (row !== null && typeof row === 'object') return (row as Record<string, unknown>)[key];
  22. return row;
  23. }
  24. describe('issue #238 — connection PRAGMAs (#1)', () => {
  25. let dir: string;
  26. let conn: DatabaseConnection;
  27. beforeAll(() => {
  28. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg238-pragma-'));
  29. conn = DatabaseConnection.initialize(path.join(dir, 'codegraph.db'));
  30. });
  31. afterAll(() => {
  32. conn.close();
  33. fs.rmSync(dir, { recursive: true, force: true });
  34. });
  35. it('uses a bounded busy_timeout, not the old 2-minute hang', () => {
  36. const ms = Number(pragmaValue(conn.getDb().pragma('busy_timeout'), 'timeout'));
  37. expect(ms).toBeGreaterThan(0);
  38. expect(ms).toBeLessThanOrEqual(30000); // far below the old 120000
  39. });
  40. it('runs in WAL mode — the mode that lets readers proceed during a write', () => {
  41. const mode = String(pragmaValue(conn.getDb().pragma('journal_mode'), 'journal_mode')).toLowerCase();
  42. expect(mode).toBe('wal');
  43. });
  44. it('getJournalMode() surfaces the effective mode for status triage', () => {
  45. expect(conn.getJournalMode()).toBe('wal');
  46. });
  47. });
  48. describe('issue #238 — WAL lets a reader proceed during a writer', () => {
  49. let dir: string;
  50. beforeAll(() => {
  51. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg238-wal-'));
  52. });
  53. afterAll(() => {
  54. fs.rmSync(dir, { recursive: true, force: true });
  55. });
  56. it('a read on a 2nd connection succeeds while a writer holds the lock', () => {
  57. const dbPath = path.join(dir, 'codegraph.db');
  58. const writer = DatabaseConnection.initialize(dbPath);
  59. // The property only holds under WAL; skip if the filesystem couldn't enable it.
  60. if (writer.getJournalMode() !== 'wal') {
  61. writer.close();
  62. return;
  63. }
  64. const reader = DatabaseConnection.open(dbPath);
  65. try {
  66. writer.getDb().prepare('BEGIN EXCLUSIVE').run(); // hard write lock, held open
  67. const t0 = Date.now();
  68. const row = reader.getDb().prepare('SELECT COUNT(*) AS c FROM nodes').get() as { c: number };
  69. const waited = Date.now() - t0;
  70. expect(row.c).toBe(0);
  71. expect(waited).toBeLessThan(1000); // proceeds immediately, no busy wait
  72. } finally {
  73. try { writer.getDb().prepare('COMMIT').run(); } catch { /* ignore */ }
  74. reader.close();
  75. writer.close();
  76. }
  77. });
  78. });
  79. describe('issue #238 — ToolHandler reuses the default instance (#2)', () => {
  80. let dir: string;
  81. let cg: CodeGraph;
  82. let root: string;
  83. let handler: ToolHandler;
  84. beforeAll(async () => {
  85. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg238-tools-'));
  86. fs.writeFileSync(path.join(dir, 'a.ts'), 'export function helper(): number { return 1; }\n');
  87. fs.writeFileSync(
  88. path.join(dir, 'b.ts'),
  89. "import { helper } from './a';\nexport function main(): number { return helper(); }\n"
  90. );
  91. cg = await CodeGraph.init(dir, { index: true });
  92. root = cg.getProjectRoot();
  93. handler = new ToolHandler(cg);
  94. });
  95. afterAll(() => {
  96. cg.close();
  97. fs.rmSync(dir, { recursive: true, force: true });
  98. });
  99. it('getCodeGraph(defaultRoot) returns the default instance, not a new connection', () => {
  100. const openSpy = vi.spyOn(CodeGraph, 'openSync');
  101. try {
  102. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  103. const resolved = (handler as any).getCodeGraph(root);
  104. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  105. const nested = (handler as any).getCodeGraph(path.join(root, 'does', 'not', 'exist'));
  106. expect(resolved).toBe(cg);
  107. expect(nested).toBe(cg); // a sub-path resolves up to the same default project
  108. expect(openSpy).not.toHaveBeenCalled(); // no second connection opened
  109. } finally {
  110. openSpy.mockRestore();
  111. }
  112. });
  113. it('concurrent read tool calls (mixed projectPath) all succeed without "database is locked"', async () => {
  114. const openSpy = vi.spyOn(CodeGraph, 'openSync');
  115. try {
  116. const calls: Promise<{ content: Array<{ text: string }>; isError?: boolean }>[] = [
  117. handler.execute('codegraph_search', { query: 'helper' }),
  118. handler.execute('codegraph_search', { query: 'helper', projectPath: root }),
  119. handler.execute('codegraph_callers', { symbol: 'helper', projectPath: root }),
  120. handler.execute('codegraph_callees', { symbol: 'main' }),
  121. handler.execute('codegraph_files', { projectPath: root }),
  122. handler.execute('codegraph_status', { projectPath: root }),
  123. ];
  124. const results = await Promise.all(calls);
  125. for (const r of results) {
  126. expect(r.isError).not.toBe(true);
  127. expect(r.content[0]?.text ?? '').not.toMatch(/database is locked/i);
  128. }
  129. // Passing the default project's own path must not open a second connection.
  130. expect(openSpy).not.toHaveBeenCalled();
  131. } finally {
  132. openSpy.mockRestore();
  133. }
  134. });
  135. });