@mastra/pg 1.20.1-alpha.2 → 1.20.1-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -20750,6 +20750,38 @@ var PgFactoryStorageOps = class {
20750
20750
  * Supports serializable app-table transactions for cross-replica invariant
20751
20751
  * enforcement and `authDatabase()` exposing the shared pool.
20752
20752
  */
20753
+ /**
20754
+ * A pool-level `error` listener only covers *idle* clients: pg hands ownership
20755
+ * of a client to the borrower for the duration of a checkout and takes its own
20756
+ * listener off. So a backend restart or network blip that lands on a client
20757
+ * mid-transaction reaches an emitter with no listener, and Node escalates that
20758
+ * to an uncaughtException that kills the process — the pool handler logs the
20759
+ * idle siblings and the borrowed one still brings everything down.
20760
+ *
20761
+ * Attach a listener once per physical connection instead. `connect` fires when
20762
+ * the pool establishes a client, so the client stays covered for its whole life
20763
+ * and there is nothing to remove on release.
20764
+ *
20765
+ * That listener outlives the checkout, so it also sees the failures the pool is
20766
+ * already reporting through its own idle listener. Follow `acquire`/`release`
20767
+ * to tell the two apart and stay quiet while the client is idle — otherwise one
20768
+ * dropped connection is announced twice, the second time as the wrong thing.
20769
+ *
20770
+ * The pool still discards the failed connection; this only keeps the failure
20771
+ * reportable instead of fatal.
20772
+ */
20773
+ function guardCheckedOutClientErrors(pool, warn = console.warn) {
20774
+ const borrowed = /* @__PURE__ */ new WeakSet();
20775
+ pool.on("connect", (client) => {
20776
+ borrowed.add(client);
20777
+ client.on("error", (err) => {
20778
+ if (!borrowed.has(client)) return;
20779
+ warn(`PgFactoryStorage: client error while checked out (the pool discards this connection): ${err instanceof Error ? err.message : String(err)}`);
20780
+ });
20781
+ });
20782
+ pool.on("acquire", (client) => borrowed.add(client));
20783
+ pool.on("release", (_err, client) => borrowed.delete(client));
20784
+ }
20753
20785
  var PgFactoryStorage = class extends FactoryStorage {
20754
20786
  ops;
20755
20787
  #pool;
@@ -20770,6 +20802,7 @@ var PgFactoryStorage = class extends FactoryStorage {
20770
20802
  this.#pool.on("error", (err) => {
20771
20803
  console.warn(`PgFactoryStorage: idle pool client error (pool discards the client and reconnects on next checkout): ${err instanceof Error ? err.message : String(err)}`);
20772
20804
  });
20805
+ guardCheckedOutClientErrors(this.#pool);
20773
20806
  }
20774
20807
  this.ops = new PgFactoryStorageOps(this.#pool, this.#schemas);
20775
20808
  }