velocious 1.0.736 → 1.0.738

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.
@@ -68,6 +68,9 @@ export default class VelociousDatabaseDriversMysql extends Base{
68
68
  /** @type {Map<string, import("../base.js").default>} */
69
69
  _advisoryLockConnections = new Map()
70
70
 
71
+ /** @type {Map<string, Promise<import("../base.js").default>>} */
72
+ _advisoryLockConnectionSpawns = new Map()
73
+
71
74
  /** @type {string | null} */
72
75
  _desiredSessionTimeZone = "+00:00"
73
76
 
@@ -1009,13 +1012,20 @@ export default class VelociousDatabaseDriversMysql extends Base{
1009
1012
  try {
1010
1013
  const rows = await connection.query(`SELECT RELEASE_LOCK(${this.quote(name)}) AS velocious_advisory_lock_result`, {retry: false})
1011
1014
  const result = rows?.[0]?.velocious_advisory_lock_result
1015
+ const fullyReleased = Number(result) === 1
1012
1016
 
1013
1017
  // Base#releaseAdvisoryLock removes one re-entrant count after this hook.
1014
1018
  // Keep the session for another counted release, but close it after the
1015
1019
  // final release so an idle pool connection cannot retain the lock.
1016
- if ((this._heldAdvisoryLocks.get(name) || 0) <= 1) await this.closeAdvisoryLockConnection(name, connection)
1017
-
1018
- return Number(result) === 1
1020
+ //
1021
+ // The re-entrant count alone cannot decide it: two concurrent releases of
1022
+ // one name both read the count before either decrement lands, so neither
1023
+ // closes the session and the pinned session outlives every holder. The
1024
+ // session's own `RELEASE_LOCK` result is authoritative — it reports 1
1025
+ // exactly when that session no longer holds the name.
1026
+ if (fullyReleased || (this._heldAdvisoryLocks.get(name) || 0) <= 1) await this.closeAdvisoryLockConnection(name, connection)
1027
+
1028
+ return fullyReleased
1019
1029
  } catch (error) {
1020
1030
  await this.closeAdvisoryLockConnection(name, connection)
1021
1031
  throw error
@@ -1043,7 +1053,37 @@ export default class VelociousDatabaseDriversMysql extends Base{
1043
1053
  return this
1044
1054
  }
1045
1055
 
1046
- const connection = await this.configuration.getDatabasePool(this._databaseIdentifier || "default").spawnConnection()
1056
+ // `spawnConnection` is awaited, so a plain check-then-set lets two
1057
+ // concurrent acquisitions of one name on this driver each open a session,
1058
+ // after which the second `set` overwrites the first map entry. The
1059
+ // overwritten session is unreachable from the map, from every pool tracked
1060
+ // set and from the idle reaper, so nothing can close it before the process
1061
+ // exits. Sharing one in-flight spawn keeps the map's one-session-per-name
1062
+ // contract true across the await.
1063
+ let pendingSpawn = this._advisoryLockConnectionSpawns.get(name)
1064
+
1065
+ while (pendingSpawn) {
1066
+ await pendingSpawn
1067
+
1068
+ const spawnedConnection = this._advisoryLockConnections.get(name)
1069
+
1070
+ if (spawnedConnection) return spawnedConnection
1071
+
1072
+ pendingSpawn = this._advisoryLockConnectionSpawns.get(name)
1073
+ }
1074
+
1075
+ const spawn = this.configuration.getDatabasePool(this._databaseIdentifier || "default").spawnConnection()
1076
+
1077
+ this._advisoryLockConnectionSpawns.set(name, spawn)
1078
+
1079
+ /** @type {import("../base.js").default | undefined} */
1080
+ let connection
1081
+
1082
+ try {
1083
+ connection = await spawn
1084
+ } finally {
1085
+ if (this._advisoryLockConnectionSpawns.get(name) === spawn) this._advisoryLockConnectionSpawns.delete(name)
1086
+ }
1047
1087
 
1048
1088
  this._advisoryLockConnections.set(name, connection)
1049
1089
  this.configuration.registerAdvisoryLockConnection(connection)