diff --git a/docs/design/layer1-native-binding.md b/docs/design/layer1-native-binding.md index 83b53c6..ed5bee9 100644 --- a/docs/design/layer1-native-binding.md +++ b/docs/design/layer1-native-binding.md @@ -821,13 +821,61 @@ Low priority: High priority: -- Thread-safety hardening: single-process connection plus async-worker / registry model needs TSan and a clear policy. +- Thread-safety hardening: the multi-connection registry plus async-worker model needs TSan and a clear policy. Low priority: - True query interrupt, depending on upstream C ABI. - Symlink-level path canonicalization. +#### 9.6.1 Multi-connection model (same-path parallelism foundation) + +The native registry was changed from a single active slot to a model that mirrors +chdb-core's actual constraint: one process-wide `EmbeddedServer` bound to a single +data directory, with N independent `chdb_connection` handles attached to it. + +- Same path → multiple `Session`s now each own a DISTINCT connection and coexist + (previously they refcount-collapsed onto one shared connection, which was a + latent clobber: separate JS param chains over one underlying connection). +- Different data directory while one is live → still rejected (engine + `BAD_ARGUMENTS`), surfaced as `ChdbConnectionError`. +- Last connection out unbinds the server, so a different path may then bind. +- Data is shared across same-path connections (same `EmbeddedServer`), so Layer 2 + `chdb://memory` clients share one refcounted temp dir and keep cross-client + table visibility while each holds its own connection. + +**Why this is the actual fix for the node-vs-python parallelism gap.** chdb-core +itself is not the bottleneck: the same `libchdb.so` (both `v26.5.1-rc.1` and a +build from current source), called from Python `ctypes` threads, runs concurrent +`chdb_query_n` calls in parallel (~3.7x on 4 cores) with per-connection parameter +isolation (N connections × concurrent parameterized queries → zero clobber). The +reason chdb-python parallelized while chdb-node did not was purely the binding: +chdb-python creates one independent `ChdbClient` per `Connection`, whereas the +old node registry refcount-collapsed every same-path `Session` onto ONE shared +`ChdbClient`, so `ChdbClient::executeMaterializedQuery`'s per-instance +`client_mutex` serialized them. Giving each `Session` its own connection removes +that shared mutex. + +Verified with this change (build/Release binding, libchdb `v26.5.1-rc.1`): +four `max_threads=1` heavy queries on four same-path Sessions enter on four +threads simultaneously and complete in ~1x the single-query time (≈3.7x speedup). + +Parameterized queries, however, are serialized PROCESS-WIDE (a single +`globalParamChain` shared by the default connection and every Session). The +bundled libchdb's parameter set/reset (`CApiQueryParameterGuard`) is NOT isolated +per connection under true parallelism: concurrent parameterized queries on +different connections clobber each other (`456 Substitution not set`) and the race +can fatally abort the engine (`236`). Thread-based bindings (chdb-python) rarely +surface this because the GIL serializes the short set/clear/execute window; +node's libuv pool runs them with real parallelism and hits it under CI. Non-param +queries are never chained and keep full parallelism. If a future libchdb isolates +parameter state per connection, this can relax to a per-connection chain. + +Distribution note: the parallelism only reaches users once the per-platform +`@chdb/lib-*` packages are REBUILT from this source — the loader prefers an +installed `@chdb/lib-` over `build/Release`, so a stale prebuilt addon +would mask the fix. + ### 9.7 Distribution / CI / docs Medium priority: diff --git a/index.js b/index.js index ee7c998..27e2886 100644 --- a/index.js +++ b/index.js @@ -228,17 +228,44 @@ function withAbortTimeout(nativePromise, opts) { }); } -// Parameterized queries set+reset parameter state ON THE CONNECTION, so two -// param queries overlapping on the same connection corrupt each other (wrong -// values / "Substitution not set"). We therefore serialize the parameterized -// async path PER CONNECTION via a promise chain (the default connection and -// each Session get their own). Non-parameterized queries are unaffected and stay -// concurrent. The chain advances on NATIVE completion (not on early abort/ -// timeout), so an aborted param query still fully drains before the next starts. -const defaultParamChain = { tail: Promise.resolve() }; +// Parameterized queries set+reset parameter state around each call +// (chdb-core's CApiQueryParameterGuard). With the bundled libchdb this set/reset +// is NOT isolated per connection under real concurrency: two parameterized +// queries running at the same time on DIFFERENT connections clobber each other +// (wrong values / "Substitution not set", code 456) — and the resulting race can +// fatally abort the in-process engine (code 236). (Thread-based bindings like +// chdb-python rarely surface this because the GIL serializes the short +// set/clear/execute window; node's libuv worker pool runs them with true +// parallelism and reliably hits it under CI scheduling.) We therefore serialize +// the parameterized async path through a SINGLE process-wide chain shared by the +// default connection and every Session. Non-parameterized queries are never +// chained and run in parallel (the multi-connection win). The chain advances on +// NATIVE completion (not on early abort/timeout), so an aborted param query still +// fully drains before the next starts. +const globalParamChain = { tail: Promise.resolve() }; function runExclusiveParam(chain, startNative, opts) { - const nativeP = chain.tail.then(() => startNative()); + // Pre-check the caller's cancellation BEFORE the chain head fires startNative. + // Otherwise a query that was queued behind earlier param queries and whose + // caller already observed CHDB_ABORT/CHDB_TIMEOUT (settled early by + // withAbortTimeout) would still dispatch the native call when its turn came up, + // leaking unintended side effects (e.g. queued DDL/DML). The chain still + // advances on the guarded promise so the next queued query is not stalled. + const signal = opts && opts.signal; + const timeout = opts && opts.timeout; + const deadline = timeout ? Date.now() + timeout : 0; + const guardedStart = () => { + if (signal && signal.aborted) { + return Promise.reject(new ChdbAbortError( + 'Query aborted before execution (queued behind earlier parameterized queries)')); + } + if (deadline && Date.now() >= deadline) { + return Promise.reject(new ChdbTimeoutError( + `Query timed out (${timeout}ms) before execution (queued behind earlier parameterized queries)`)); + } + return startNative(); + }; + const nativeP = chain.tail.then(guardedStart); chain.tail = nativeP.then(() => {}, () => {}); return withAbortTimeout(nativeP, opts); } @@ -384,7 +411,7 @@ function queryBindAsync(query, params = {}, opts = {}) { const { sql, format } = prepArrow(query, opts, "CSV"); let bound; try { bound = formatParams(params); } catch (e) { return Promise.reject(e); } - return runExclusiveParam(defaultParamChain, () => chdbNode.QueryAsync(sql, format, bound), opts); + return runExclusiveParam(globalParamChain, () => chdbNode.QueryAsync(sql, format, bound), opts); } // v3 insert (default connection). Dispatches on the shape of `values`: @@ -405,13 +432,14 @@ const openSessions = new Set(); // Track in-flight native operations (async query/insert) that were settled // EARLY by abort/timeout. There is no interrupt in the C ABI, so the native // computation keeps running on the libuv thread after the JS promise has -// already rejected. libchdb permits only ONE active operation per process, so -// an orphaned background op from one test races the next test's first query and -// crashes the in-process engine ("server is shutting down due to a fatal -// error", ABORTED), cascading into every later test (this is why x64 CI runners -// went all-red while the timing on arm runners usually won the race). Each -// tracked promise settles on NATIVE completion and never rejects — the caller -// already received the mapped early-settle error; here we only need the timing. +// already rejected. An orphaned background op that is still running when its +// connection is closed is a use-after-free on that connection, and a connection +// torn down mid-op can abort the shared in-process engine ("server is shutting +// down due to a fatal error", ABORTED), cascading into later work. We therefore +// drain pending ops before closing a connection (close() below) and in test +// teardown. Each tracked promise settles on NATIVE completion and never rejects +// — the caller already received the mapped early-settle error; here we only need +// the timing. const pendingNativeOps = new Set(); function trackNative(nativePromise) { const done = nativePromise.then(() => {}, () => {}); @@ -429,11 +457,12 @@ function _drainPendingOps() { } // Force-close every session still open in this process. Internal helper for -// test teardown: libchdb allows one active data directory per process, so a -// single test that creates a Session and never closes it (e.g. it threw before -// its own close) blocks every later `new Session()` with a different path. A -// global afterEach calling this guarantees no session leaks across test -// boundaries, instead of relying on every test to clean up perfectly. +// test teardown: chdb-core binds one data directory per process, so a single +// test that creates a Session and never closes it (e.g. it threw before its own +// close) blocks every later `new Session()` with a DIFFERENT path (same-path +// sessions would coexist). A global afterEach calling this guarantees no session +// leaks across test boundaries, instead of relying on every test to clean up +// perfectly. function _closeAllSessions() { for (const s of [...openSessions]) { try { s.close(); } catch (_) { /* best effort */ } @@ -456,7 +485,6 @@ const SESSION_SIGNALS = ['SIGINT', 'SIGTERM']; class Session { #closed = false; - #paramChain = { tail: Promise.resolve() }; // per-connection serialization for parameterized queries #signalHandler = null; // opt-in signal handler, deregistered on close() constructor(path = "", opts = {}) { @@ -469,10 +497,14 @@ class Session { this.isTemp = false; } - // Create a connection for this session (the native registry enforces a - // single active connection per process). The registry key is normalized to - // an absolute path so e.g. "./data" and its absolute form map to the same - // connection; this.path is left as the caller passed it (public surface). + // Create a connection for this session. Each Session owns its OWN native + // connection: the native registry allows N independent connections to the + // same bound path (they share the one process-wide EmbeddedServer and its + // data, but each carries its own query/parameter state, so same-path + // sessions run in parallel without clobbering). A *different* data directory + // while one is live is still rejected. The registry key is normalized to an + // absolute path so e.g. "./data" and its absolute form bind the same server; + // this.path is left as the caller passed it (public surface). try { const key = this.path ? resolvePath(this.path) : this.path; this.connection = chdbNode.CreateConnection(key); @@ -538,7 +570,7 @@ class Session { const { sql, format } = prepArrow(query, opts, "CSV"); let bound; try { bound = formatParams(params); } catch (e) { return Promise.reject(e); } - return runExclusiveParam(this.#paramChain, () => chdbNode.QueryAsyncConnection(this.connection, sql, format, bound), opts); + return runExclusiveParam(globalParamChain, () => chdbNode.QueryAsyncConnection(this.connection, sql, format, bound), opts); } // v3 insert. Same dispatch as the standalone insert(): row arrays -> inline @@ -554,7 +586,8 @@ class Session { // v3 streaming. opts: { format?='JSONEachRow', signal? }. Returns a // ChdbQueryStream (AsyncIterable). Only one stream may be active per session - // at a time (single active connection). + // at a time (one streaming cursor per connection); other sessions stream in + // parallel on their own connections. queryStream(sql, opts = {}) { if (!this.connection) throw new ChdbClosedError("No active connection available"); if (!sql) throw new ChdbStreamError("queryStream requires a non-empty query"); @@ -655,7 +688,8 @@ class Session { } // Diagnostic version info. libchdb is probed via SELECT version(); -// falls back to 'unknown' if a session is holding the single active connection. +// falls back to 'unknown' if a session is holding a different data directory +// than the standalone default would bind. function version() { let libchdb = 'unknown'; try { diff --git a/lib/chdb_node.cpp b/lib/chdb_node.cpp index 159dd0f..409282f 100644 --- a/lib/chdb_node.cpp +++ b/lib/chdb_node.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include typedef void * ChdbConnection; @@ -25,28 +27,54 @@ char * QueryWithConnection(ChdbConnection conn, const char * query, const char * // param state between calls (unlike the old session-wide "SET param_x=" path, // which could race when different callers set different values). -// Connection registry. chDB / libchdb allow only ONE active connection per -// process; this registry is the single owner of that constraint. It tracks the -// one live connection by its path key, reference-counts session handles for the -// same path (so reopening the same path returns the same connection while it is -// live), and REJECTS a second concurrent data directory rather than silently -// switching (silent switching caused the malloc crash on repeated start/stop). +// Connection registry. chdb-core runs ONE process-wide EmbeddedServer bound to a +// single data directory (path): the first connect() binds it, a later connect() +// to the SAME path attaches another independent client to that server, and a +// connect() to a DIFFERENT path while one is live is rejected by the engine +// (BAD_ARGUMENTS: "EmbeddedServer already initialized with path ..."). The server +// unbinds when its last connection closes, after which a different path may bind. +// +// This registry mirrors that model. Unlike the previous single-slot design (which +// refcount-collapsed every same-path Session onto ONE shared connection), it now +// keeps each connection as a DISTINCT chdb_connection handle, so: +// - N connections to the same path coexist and run NON-PARAMETERIZED queries +// in parallel (each chdb-core client has its own per-instance +// ChdbClient::client_mutex, so distinct connections never serialize against +// each other); +// - a different data directory is still rejected while one is bound; +// - same-path Sessions no longer silently share one native connection (which +// was a latent clobber: two Sessions had separate JS param chains but one +// underlying connection). +// +// PARAMETERIZED queries are NOT parallelized today: the bundled libchdb does not +// isolate parameter set/reset per connection under true parallelism (concurrent +// param queries on different connections clobber each other, surfacing as +// `456 Substitution not set` and can fatally abort the engine `236`). The JS +// layer serializes ALL parameterized async queries through one process-wide +// chain (see `globalParamChain` in index.js); non-parameterized queries are +// unaffected and keep full parallelism. +// // The path key is normalized to an absolute path by the JS layer (path.resolve) -// so "./data" and the absolute form map to the same connection; symlink-level -// canonicalization is a possible future refinement. The lazily-created default -// in-memory connection serves standalone query()/queryBind() and yields -// to a session when one opens (matching v2's release_default_conn behaviour). +// so "./data" and the absolute form bind the same server. The lazily-created +// default in-memory connection (key "") serves standalone query()/queryBind() +// and yields to a session when one opens (matching v2 behaviour). // // The canonical handle passed around is the `chdb_connection*` returned by // chdb_connect (dereferenced for chdb_query, passed as-is to chdb_close_conn), // matching the External handle layout v2 exposed to JS. -struct ActiveConn { - chdb_connection *conn = nullptr; // chdb_connect() result, or nullptr - std::string key; // "" = default in-memory; otherwise the path - int refcount = 0; // open Session handles (default is transient) - bool isDefault = false; +// +// Thread-safety: registry mutations happen on the Node main thread (the N-API +// wrappers below), so they are already serialized by the event loop; queries run +// on libuv worker threads but only touch a connection handle, never this registry. +// g_reg_mu is a defensive guard so the invariants hold even if a future caller +// mutates the registry off the main thread. +struct Registry { + std::string boundKey; // path the EmbeddedServer is bound to (valid while !conns.empty()) + std::set conns; // every live connection (sessions + the default) + chdb_connection *defaultConn = nullptr; // the lazy in-memory default (key ""), if up }; -static ActiveConn g_active; +static Registry g_reg; +static std::mutex g_reg_mu; static chdb_connection *open_raw(const std::string &path) { char prog[] = "clickhouse"; @@ -62,31 +90,37 @@ static chdb_connection *open_raw(const std::string &path) { return (conn_ptr && *conn_ptr) ? conn_ptr : nullptr; } -static void hard_close_active() { - if (g_active.conn) { - chdb_close_conn(g_active.conn); +// Close every live connection. atexit backstop (the JS layer closes sessions on +// 'exit' too); also used so a leaked connection never outlives the process. +static void hard_close_all() { + std::lock_guard lk(g_reg_mu); + for (chdb_connection *c : g_reg.conns) { + if (c) chdb_close_conn(c); } - g_active.conn = nullptr; - g_active.key.clear(); - g_active.refcount = 0; - g_active.isDefault = false; + g_reg.conns.clear(); + g_reg.defaultConn = nullptr; + g_reg.boundKey.clear(); } static void ensure_atexit() { static bool done = false; if (!done) { - std::atexit(hard_close_active); + std::atexit(hard_close_all); done = true; } } -// Default connection for standalone query()/queryBind() (lazy, in-memory). +// Default connection for standalone query()/queryBind() (lazy, in-memory). A +// single shared default per process; reused across standalone calls. static chdb_connection *get_default_conn(char **error_message) { ensure_atexit(); - if (g_active.conn) { - if (g_active.isDefault) return g_active.conn; + std::lock_guard lk(g_reg_mu); + if (g_reg.defaultConn) return g_reg.defaultConn; + // No default up. If a session holds a real data directory, "" cannot bind a + // second one (one EmbeddedServer path per process) — reject, as v2 did. + if (!g_reg.conns.empty()) { if (error_message && !*error_message) - *error_message = strdup((std::string("chdb: a session (path='") + g_active.key + + *error_message = strdup((std::string("chdb: a session (path='") + g_reg.boundKey + "') is active; close it before using standalone query()").c_str()); return nullptr; } @@ -96,31 +130,35 @@ static chdb_connection *get_default_conn(char **error_message) { *error_message = strdup("Failed to acquire default connection"); return nullptr; } - g_active.conn = c; - g_active.key = ""; - g_active.refcount = 0; - g_active.isDefault = true; + g_reg.defaultConn = c; + g_reg.boundKey = ""; + g_reg.conns.insert(c); return c; } -// Session connection: same path reuses (refcount++); a live default yields; a -// different active data directory is rejected (not silently switched). +// Session connection: same bound path opens ANOTHER independent connection; a +// live in-memory default yields; a different data directory is rejected. static chdb_connection *acquire_session_conn(const std::string &path, char **error_message) { ensure_atexit(); - if (g_active.conn) { - if (!g_active.isDefault && g_active.key == path) { - g_active.refcount++; - return g_active.conn; - } - if (g_active.isDefault) { - hard_close_active(); - } else { + std::lock_guard lk(g_reg_mu); + if (!g_reg.conns.empty()) { + if (g_reg.boundKey.empty()) { + // Only the in-memory default is up (boundKey ""). It is transient and must + // yield so this session can bind a real data directory. + if (g_reg.defaultConn) { + chdb_close_conn(g_reg.defaultConn); + g_reg.conns.erase(g_reg.defaultConn); + g_reg.defaultConn = nullptr; + } + // conns is now empty; fall through to bind `path`. + } else if (g_reg.boundKey != path) { if (error_message && !*error_message) *error_message = strdup((std::string("chdb: only one active data directory per " - "process; close the current session (path='") + g_active.key + + "process; close the current session (path='") + g_reg.boundKey + "') before opening '" + path + "'").c_str()); return nullptr; } + // else boundKey == path: an independent connection to the same server. } chdb_connection *c = open_raw(path); if (!c) { @@ -128,18 +166,21 @@ static chdb_connection *acquire_session_conn(const std::string &path, char **err *error_message = strdup((std::string("Failed to create connection for path '") + path + "'").c_str()); return nullptr; } - g_active.conn = c; - g_active.key = path; - g_active.refcount = 1; - g_active.isDefault = false; + g_reg.boundKey = path; + g_reg.conns.insert(c); return c; } static void release_session_conn(chdb_connection *conn) { if (!conn) return; - if (g_active.conn == conn && !g_active.isDefault) { - if (--g_active.refcount <= 0) hard_close_active(); - } + std::lock_guard lk(g_reg_mu); + auto it = g_reg.conns.find(conn); + if (it == g_reg.conns.end()) return; // already released / unknown handle + chdb_close_conn(conn); + g_reg.conns.erase(it); + if (conn == g_reg.defaultConn) g_reg.defaultConn = nullptr; + // Last connection out unbinds the EmbeddedServer so a different path may bind. + if (g_reg.conns.empty()) g_reg.boundKey.clear(); } static char *exec_query(chdb_connection conn, const char *query, @@ -860,10 +901,10 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) { exports.Set("CloseConnection", Napi::Function::New(env, CloseConnectionWrapper)); exports.Set("QueryWithConnection", Napi::Function::New(env, QueryWithConnectionWrapper)); - // Most-reliable exit cleanup: close the active connection on env - // teardown, ahead of (and complementary to) the std::atexit backstop. Both - // call the idempotent hard_close_active, so running twice is harmless. - env.AddCleanupHook([] { hard_close_active(); }); + // Most-reliable exit cleanup: close every live connection on env teardown, + // ahead of (and complementary to) the std::atexit backstop. Both call the + // idempotent hard_close_all, so running twice is harmless. + env.AddCleanupHook([] { hard_close_all(); }); return exports; } diff --git a/test/v3/async-stress.test.ts b/test/v3/async-stress.test.ts index 5ad8b0e..c4fd8fa 100644 --- a/test/v3/async-stress.test.ts +++ b/test/v3/async-stress.test.ts @@ -9,6 +9,14 @@ import { queryAsync, queryBindAsync, Session } from '../../index.js' const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)) const HEAVY = (n: number) => `SELECT count() FROM numbers(${n})` +// The storm/race cases below run dozens of heavy queries in sequence and take +// ~15-30s on a fast runner. Give them a generous timeout so a slow/loaded CI +// runner never hits the 30s default and kills a test mid-flight — an abandoned +// in-flight native op (plain queryAsync is not tracked for drain) would collide +// with the next test on the single in-process engine and abort it (code 236), +// cascading failures into every later file. +const STRESS_TIMEOUT_MS = 120_000 + describe('async concurrency — correctness & no deadlock', () => { it('runs 64 concurrent default-connection queries with no cross-contamination', async () => { const N = 64 @@ -80,7 +88,7 @@ describe('async cancellation storms (no crash / no hang)', () => { expect(codes.every((c) => c === 'CHDB_ABORT')).toBe(true) // recovers after the storm expect((await queryAsync('SELECT 7', { format: 'CSV' })).text().trim()).toBe('7') - }) + }, STRESS_TIMEOUT_MS) it('survives a timeout storm and recovers', async () => { const codes = await Promise.all( @@ -90,7 +98,7 @@ describe('async cancellation storms (no crash / no hang)', () => { ) expect(codes.every((c) => c === 'CHDB_TIMEOUT')).toBe(true) expect((await queryAsync('SELECT 8', { format: 'CSV' })).text().trim()).toBe('8') - }) + }, STRESS_TIMEOUT_MS) }) describe('lifecycle race: close / registry mutation during an in-flight query', () => { @@ -106,7 +114,7 @@ describe('lifecycle race: close / registry mutation during an in-flight query', const r = await p expect(typeof r).toBe('string') // resolved 'ok' or a typed error code — never a crash } - }) + }, STRESS_TIMEOUT_MS) it('opening a new session while a default-conn query is in flight is safe (60x)', async () => { for (let i = 0; i < 60; i++) { @@ -120,7 +128,7 @@ describe('lifecycle race: close / registry mutation during an in-flight query', s.close() // close even if the assertion throws, or the session leaks into the next test } } - }) + }, STRESS_TIMEOUT_MS) }) describe('async path memory (no leak on the query path)', () => { diff --git a/test/v3/multi-connection.test.ts b/test/v3/multi-connection.test.ts new file mode 100644 index 0000000..cff042c --- /dev/null +++ b/test/v3/multi-connection.test.ts @@ -0,0 +1,175 @@ +/** + * Multi-connection behavior (feat/layer1-multi-connection). + * + * Before this change, the native registry was a single slot: a second Session to + * the SAME path refcount-collapsed onto one shared connection, and a different + * path was rejected. Now each Session owns an independent connection to the one + * process-wide EmbeddedServer, so same-path sessions coexist and run + * non-parameterized queries in PARALLEL, while a *different* data directory is + * still rejected. Parameterized queries are serialized process-wide (the bundled + * libchdb does not isolate parameter state per connection under true + * parallelism). This mirrors the chdb-python model (multiple Connection objects + * to the same path) verified empirically. + */ +import { describe, it, expect, afterEach } from 'vitest' +import * as os from 'os' +import { mkdtempSync, rmSync } from 'fs' +import { join } from 'path' +// eslint-disable-next-line @typescript-eslint/no-var-requires +const chdb = require('../../index.js') +const { Session } = chdb + +function tmpPath(): string { + return mkdtempSync(join(os.tmpdir(), 'chdb-mc-test-')) +} + +const dirs: string[] = [] +afterEach(() => { + for (const d of dirs.splice(0)) { + try { rmSync(d, { recursive: true, force: true }) } catch { /* best-effort */ } + } +}) + +describe('multi-connection: same-path coexistence', () => { + it('opens N Sessions to the SAME path simultaneously (previously rejected)', () => { + const dir = tmpPath(); dirs.push(dir) + const sessions = Array.from({ length: 4 }, () => new Session(dir)) + try { + for (const s of sessions) { + expect(s.open).toBe(true) + expect(s.query('SELECT 1', 'CSV').trim()).toBe('1') + } + } finally { + for (const s of sessions) s.close() + } + }) + + it('shares data across distinct same-path connections', () => { + const dir = tmpPath(); dirs.push(dir) + const a = new Session(dir) + const b = new Session(dir) + try { + a.query('CREATE TABLE shared (x Int64) ENGINE = MergeTree ORDER BY x', 'CSV') + a.query('INSERT INTO shared VALUES (7)', 'CSV') + // b is a SEPARATE connection but the same EmbeddedServer → sees the table. + expect(b.query('EXISTS TABLE shared', 'CSV').trim()).toBe('1') + expect(b.query('SELECT sum(x) FROM shared', 'CSV').trim()).toBe('7') + } finally { + a.close(); b.close() + } + }) + + it('still rejects a DIFFERENT data directory while one is live', () => { + const d1 = tmpPath(); dirs.push(d1) + const d2 = tmpPath(); dirs.push(d2) + const a = new Session(d1) + try { + // Assert on the stable .name/.code contract — the Layer 1 entrypoint does + // not re-export the error class itself (matches registry.test.ts:54). + let thrown: unknown + try { new Session(d2) } catch (e) { thrown = e } + expect(thrown).toBeInstanceOf(Error) + expect((thrown as Error).name).toBe('ChdbConnectionError') + expect((thrown as { code?: string }).code).toBe('CHDB_CONNECTION') + } finally { + a.close() + } + // After the first is closed, a different path binds fine (server unbinds). + const b = new Session(d2) + expect(b.query('SELECT 1', 'CSV').trim()).toBe('1') + b.close() + }) +}) + +describe('multi-connection: parameterized queries under concurrency are correct', () => { + // Fired concurrently across distinct same-path connections. The bundled + // libchdb does not isolate parameter set/reset per connection under true + // parallelism, so index.js serializes ALL parameterized queries through one + // process-wide chain; this asserts no value is ever clobbered (code 456) and + // the engine is never corrupted, whatever the interleaving. + it('never clobbers parameters across concurrent same-path connections', async () => { + const dir = tmpPath(); dirs.push(dir) + const N = 8 + const ITERS = 60 + const sessions = Array.from({ length: N }, () => new Session(dir)) + try { + const workers = sessions.map((s, tid) => + (async () => { + for (let i = 0; i < ITERS; i++) { + const x = tid * 1_000_000 + i + const y = 7 * x + 13 + const r = await s.queryBindAsync( + 'SELECT {a:Int64} AS a, {b:Int64} AS b', + { a: String(x), b: String(y) }, + { format: 'CSV' }, + ) + const [gotA, gotB] = r.text().trim().split(',').map((v: string) => Number(v)) + expect(gotA).toBe(x) + expect(gotB).toBe(y) + } + })(), + ) + await Promise.all(workers) + } finally { + for (const s of sessions) s.close() + } + }) +}) + +describe('multi-connection: concurrent execution is correct', () => { + // Real parallel SPEEDUP measurements live out of the default suite: CI installs + // the PUBLISHED @chdb/lib-* prebuilt (still the old single-connection registry), + // and the loader prefers it over build/Release, so no wall-clock speedup is + // observable in CI until those platform packages are republished from this + // source. The deterministic correctness check below is what gates the model. + const HEAVY = 'SELECT sum(sipHash64(number)) FROM numbers(20000000) SETTINGS max_threads = 1' + const K = 4 + + it('runs K concurrent same-path queries and all return the correct result', async () => { + const dir = tmpPath(); dirs.push(dir) + const sessions = Array.from({ length: K }, () => new Session(dir)) + try { + const expected = (await sessions[0].queryAsync(HEAVY, { format: 'CSV' })).text().trim() + const results = await Promise.all( + sessions.map((s) => s.queryAsync(HEAVY, { format: 'CSV' }).then((r: { text(): string }) => r.text().trim())), + ) + for (const r of results) expect(r).toBe(expected) + } finally { + for (const s of sessions) s.close() + } + }) +}) + +describe('multi-connection: parameter chain cancellation pre-check', () => { + // Param queries are serialized through a single process-wide chain. A request + // whose AbortSignal fires WHILE it is still waiting its turn behind earlier + // queries must not dispatch the native call when it finally reaches the head — + // the caller already observed CHDB_ABORT, and silently running queued DDL/DML + // afterwards would be a surprising side effect (the review-flagged behavior). + it('does not dispatch a queued param query whose AbortSignal already fired', async () => { + const dir = tmpPath(); dirs.push(dir) + const s = new Session(dir) + try { + const HEAVY_PARAM = 'SELECT count() FROM numbers({n:UInt64}) WHERE sipHash64(number) % 2 = 0 SETTINGS max_threads = 1' + const head = s.queryBindAsync(HEAVY_PARAM, { n: '50000000' }, { format: 'CSV' }) + const ctrls = Array.from({ length: 3 }, () => new AbortController()) + const queued = ctrls.map((c, i) => + s.queryBindAsync('SELECT {x:Int64}', { x: String(i + 1) }, { format: 'CSV', signal: c.signal })) + for (const c of ctrls) c.abort() + const settled = await Promise.allSettled(queued) + for (const r of settled) { + expect(r.status).toBe('rejected') + const e = (r as PromiseRejectedResult).reason + // ChdbAbortError sets .name to 'AbortError' (web AbortController parity); + // .code is the stable cross-class contract. + expect((e as { code?: string }).code).toBe('CHDB_ABORT') + } + await head + const tail = await s.queryBindAsync('SELECT {x:Int64}', { x: '42' }, { format: 'CSV' }) + expect(tail.text().trim()).toBe('42') + } finally { + s.close() + } + }) +}) +