sqlite-database-integration icon indicating copy to clipboard operation
sqlite-database-integration copied to clipboard

MySQL Proxy: All client sessions share one adapter/connection, leaking transactions and session state between clients

Open 0xABCD01 opened this issue 1 month ago • 0 comments

Package

mysql-proxy (WordPress/sqlite-database-integration), against trunk (70bdaf3).

Summary

All concurrent MySQL client sessions share a single Adapter instance — and therefore a single underlying database connection — so per-connection state leaks between clients.

  • MySQL_Proxy::__construct() accepts one Adapter and stores it (packages/mysql-proxy/src/class-mysql-proxy.php).
  • For every new client, MySQL_Proxy creates the session with that same instance: new MySQL_Session( $this->adapter, $client_id, ... ).
  • The bundled SQLite_Adapter (packages/mysql-proxy/src/Adapter/class-sqlite-adapter.php) holds exactly one WP_MySQL_On_SQLite driver, i.e. one PDO/SQLite connection, created in its constructor.

Impact

State that is connection-scoped in MySQL is shared across all proxy clients:

  • Transactions: BEGIN / COMMIT / ROLLBACK from one client commits or rolls back other clients' in-flight work on the shared connection.
  • Temporary tables: a TEMPORARY TABLE created by one client is visible to (and droppable by) every other client.
  • Session state: user variables (SET @x = ...), LAST_INSERT_ID(), and the current database (USE) are whatever the most recent client set them to.
  • Disconnect handling: one client disconnecting mid-transaction leaves the shared connection in that transaction state for the next client.

This is a correctness/isolation problem for the proxy's stated use case (serving non-PHP MySQL clients), rather than a cosmetic one: two ordinary, well-behaved clients can silently corrupt each other's work.

Why this is filed as an issue, not a PR

Fixing this requires one adapter/connection per session, which changes how MySQL_Proxy is constructed and used — a public API evolution that needs a maintainer decision (per the semver guidance in AGENTS.md). Plausible shapes:

  1. Adapter factory: MySQL_Proxy accepts a factory/callable that produces a fresh Adapter per client session. The CLI (bin/wp-mysql-proxy.php) and the test server bootstrap would pass fn() => new SQLite_Adapter( $db_path ). Backwards compatibility could be kept by still accepting a plain Adapter (documented as shared/single-client) or by a major-version change.
  2. Cloneable adapter contract: an Adapter::for_session() / clone-based method the proxy calls per session; SQLite_Adapter would open a new driver connection per clone. Adapters that are inherently stateless could return $this.

Option 1 keeps the Adapter interface unchanged and makes the sharing explicit at the construction site, so it looks like the smaller, safer change — but I would like a maintainer's steer before implementing either.

Reproduction sketch

With the proxy running, open two concurrent client connections:

  1. Client A: BEGIN; INSERT INTO t VALUES (1); (no commit)
  2. Client B: SELECT * FROM t; — sees A's uncommitted row; ROLLBACK; — rolls back A's transaction.
  3. Client A: COMMIT; — nothing left to commit; the row is gone.

0xABCD01 avatar Nov 01 '26 11:11 0xABCD01