@calltelemetry/fmix 2.7.15 → 2.7.16

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.
Files changed (2) hide show
  1. package/bin/fmix +80 -9
  2. package/package.json +3 -3
package/bin/fmix CHANGED
@@ -25,7 +25,7 @@ set -uo pipefail
25
25
  # ---------------------------------------------------------------------------
26
26
  # Constants & config
27
27
  # ---------------------------------------------------------------------------
28
- FMIX_VERSION="2.7.15"
28
+ FMIX_VERSION="2.7.16"
29
29
  FMIX_HOME="${FMIX_HOME:-${HOME}/.ct-fmix}"
30
30
  FMIX_OS="$(uname -s)"
31
31
  FMIX_ARCH="$(uname -m)"
@@ -2338,6 +2338,22 @@ fmix_session_metadata_references_hash() {
2338
2338
  return 1
2339
2339
  }
2340
2340
 
2341
+ fmix_session_hash_has_local_root() {
2342
+ # A scratch DB name carries only the session hash, and the same hash can
2343
+ # legitimately exist under another FMIX_HOME on this host (the DB server is
2344
+ # shared). Only a session root in THIS home proves fmix created the DB here.
2345
+ # Without it, the database may belong to another agent's live home: a second
2346
+ # FMIX_HOME once classified two live ~/.ct-fmix scratch DBs as orphans and
2347
+ # only the leaked backends stopped the DROP. Never destructive-clean a DB
2348
+ # this home cannot prove it owns.
2349
+ local session_hash="$1" session_dir
2350
+ [[ "$session_hash" =~ ^[0-9a-f]{16}$ ]] || return 1
2351
+ for session_dir in "$FMIX_HOME"/sessions/v1/*/"$session_hash"; do
2352
+ [[ -d "$session_dir" && ! -L "$session_dir" ]] && return 0
2353
+ done
2354
+ return 1
2355
+ }
2356
+
2341
2357
  fmix_drop_orphan_session_db() {
2342
2358
  # Destructive orphan cleanup must serialize with first-session setup. A
2343
2359
  # snapshot taken before a session publishes meta.json is advisory only;
@@ -2350,6 +2366,13 @@ fmix_drop_orphan_session_db() {
2350
2366
  return 2
2351
2367
  fi
2352
2368
  session_hash="${BASH_REMATCH[1]}"
2369
+ # Enforced here, not only in doctor: gc calls this helper directly and the
2370
+ # Postgres server is shared across FMIX_HOMEs. A hash with no root in this
2371
+ # home may be a live session in another home.
2372
+ if ! fmix_session_hash_has_local_root "$session_hash"; then
2373
+ fmix_warn "Refusing orphan cleanup for $db_name: no session root in this FMIX_HOME."
2374
+ return 2
2375
+ fi
2353
2376
  session_lock="session-$session_hash"
2354
2377
  if fmix_lock_acquire "$session_lock" 0 "$diagnostic_name" >/dev/null 2>&1; then
2355
2378
  :
@@ -2378,7 +2401,8 @@ fmix_drop_orphan_session_db() {
2378
2401
  fi
2379
2402
  fmix_guard_db_name "$db_name" || drop_rc=1
2380
2403
  if (( drop_rc == 0 )); then
2381
- fmix_drop_db "$db_name" || drop_rc=$?
2404
+ # Lease + local-root proof above: this is this home's expired orphan.
2405
+ fmix_drop_db "$db_name" 1 || drop_rc=$?
2382
2406
  fi
2383
2407
  fmix_lock_release_optional "$session_lock" || {
2384
2408
  local release_rc=$?
@@ -2473,7 +2497,7 @@ fmix_purge_expired_sessions() {
2473
2497
  fi
2474
2498
  session_db="ct_fmix_s_$session_hash"
2475
2499
  if fmix_db_exists "$session_db"; then
2476
- fmix_drop_db "$session_db" >/dev/null 2>&1 || {
2500
+ fmix_drop_db "$session_db" 1 >/dev/null 2>&1 || {
2477
2501
  local drop_rc=$?
2478
2502
  if fmix_lock_failure_is_fatal "$drop_rc"; then
2479
2503
  fmix_command_lock_finish "$session_lock" "$drop_rc"
@@ -8956,8 +8980,19 @@ fmix_db_exists() {
8956
8980
  [[ "$result" == "1" ]]
8957
8981
  }
8958
8982
 
8983
+ fmix_drop_error_is_active_sessions() {
8984
+ # PostgreSQL reports a refused drop in two places: the primary error
8985
+ # ("database ... is being accessed by other users") and its DETAIL line
8986
+ # ("There are N other sessions using the database"). Match either, and
8987
+ # lowercase first so the check does not depend on message casing.
8988
+ local message="$1" lowered
8989
+ lowered="$(printf '%s' "$message" | tr '[:upper:]' '[:lower:]')"
8990
+ [[ "$lowered" == *"is being accessed by other users"* \
8991
+ || "$lowered" == *"other sessions using the database"* ]]
8992
+ }
8993
+
8959
8994
  fmix_drop_db() {
8960
- local db_name="$1"
8995
+ local db_name="$1" force_active="${2:-0}"
8961
8996
  fmix_guard_db_name "$db_name" || return 1
8962
8997
  local timeout timeout_ms out rc ddl_lock=""
8963
8998
  if fmix_host_test_lock_enabled; then
@@ -8971,10 +9006,38 @@ fmix_drop_db() {
8971
9006
  PGCONNECT_TIMEOUT=5 PGOPTIONS="${PGOPTIONS:-} -c statement_timeout=${timeout_ms} -c lock_timeout=${timeout_ms}" \
8972
9007
  fmix_psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
8973
9008
  -c "ALTER DATABASE \"${db_name}\" IS_TEMPLATE false" >/dev/null 2>&1 || true
9009
+ local drop_sql="DROP DATABASE IF EXISTS \"${db_name}\""
8974
9010
  out="$(PGCONNECT_TIMEOUT=5 PGOPTIONS="${PGOPTIONS:-} -c statement_timeout=${timeout_ms} -c lock_timeout=${timeout_ms}" \
8975
9011
  fmix_psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
8976
- -v ON_ERROR_STOP=1 -c "DROP DATABASE IF EXISTS \"${db_name}\"" 2>&1)"
9012
+ -v ON_ERROR_STOP=1 -c "$drop_sql" 2>&1)"
8977
9013
  rc=$?
9014
+ # A scratch DB can outlive the session that owns it while a leaked
9015
+ # connection pool still holds idle backends. PostgreSQL then refuses the
9016
+ # DROP ("database ... is being accessed by other users") and the database
9017
+ # leaks forever — observed as two orphaned scratch DBs holding 142 idle
9018
+ # backends. Only callers that have proven this name is an orphan/expired
9019
+ # under this FMIX_HOME pass force_active=1, and they get one bounded FORCE
9020
+ # retry. PG13+ has WITH (FORCE); older servers get one explicit
9021
+ # backend-termination pass first. Callers without that proof keep the plain
9022
+ # drop so a live pool is never terminated by surprise.
9023
+ if (( rc != 0 )) && [[ "$force_active" == "1" ]] && fmix_drop_error_is_active_sessions "$out"; then
9024
+ local server_version
9025
+ server_version="$(fmix_server_version_num)"
9026
+ if [[ "$server_version" =~ ^[0-9]+$ ]] && (( server_version >= 130000 )); then
9027
+ out="$(PGCONNECT_TIMEOUT=5 PGOPTIONS="${PGOPTIONS:-} -c statement_timeout=${timeout_ms} -c lock_timeout=${timeout_ms}" \
9028
+ fmix_psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
9029
+ -v ON_ERROR_STOP=1 -c "${drop_sql} WITH (FORCE)" 2>&1)"
9030
+ rc=$?
9031
+ else
9032
+ fmix_control_psql "$timeout" -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
9033
+ -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '${db_name}' AND pid <> pg_backend_pid()" \
9034
+ >/dev/null 2>&1 || true
9035
+ out="$(PGCONNECT_TIMEOUT=5 PGOPTIONS="${PGOPTIONS:-} -c statement_timeout=${timeout_ms} -c lock_timeout=${timeout_ms}" \
9036
+ fmix_psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
9037
+ -v ON_ERROR_STOP=1 -c "$drop_sql" 2>&1)"
9038
+ rc=$?
9039
+ fi
9040
+ fi
8978
9041
  if (( rc != 0 )); then
8979
9042
  printf '%s\n' "$out" | tail -8 >&2
8980
9043
  fmix_error "DROP DATABASE $db_name failed or exceeded ${timeout}s."
@@ -15078,7 +15141,7 @@ cmd_doctor() {
15078
15141
  && fmix_control_psql 5 -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -c "SELECT 1" >/dev/null 2>&1; then
15079
15142
  local s_dbs db_name shash session_found orphan_drop_rc
15080
15143
  s_dbs="$(fmix_control_psql 5 -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
15081
- -t -c "SELECT datname FROM pg_database WHERE datname LIKE 'ct_fmix_s_%'" 2>/dev/null | tr -d ' ')"
15144
+ -t -c "SELECT datname FROM pg_database WHERE datname ~ '^ct_fmix_s_[0-9a-f]{16}\$'" 2>/dev/null | tr -d ' ')"
15082
15145
  for db_name in $s_dbs; do
15083
15146
  [[ -n "$db_name" ]] || continue
15084
15147
  shash="${db_name#ct_fmix_s_}"
@@ -15092,6 +15155,14 @@ cmd_doctor() {
15092
15155
  echo " [WARN] Refusing cleanup for malformed session DB name: $db_name"
15093
15156
  continue
15094
15157
  fi
15158
+ # The Postgres server is host-global but session roots are
15159
+ # per-FMIX_HOME. A DB with no root here may be live under a
15160
+ # different FMIX_HOME; only this home's own orphans are safe to
15161
+ # destroy. See fmix_session_hash_has_local_root.
15162
+ if ! fmix_session_hash_has_local_root "$shash"; then
15163
+ echo " [INFO] Skipping scratch DB with no session root in this FMIX_HOME: $db_name"
15164
+ continue
15165
+ fi
15095
15166
  orphan_drop_rc=0
15096
15167
  fmix_drop_orphan_session_db "$db_name" "doctor orphan DB sweep" \
15097
15168
  >/dev/null || orphan_drop_rc=$?
@@ -15789,7 +15860,7 @@ PY
15789
15860
  local db_name
15790
15861
  db_name="ct_fmix_s_${shash}"
15791
15862
  if fmix_guard_db_name "$db_name" 2>/dev/null && fmix_db_exists "$db_name" 2>/dev/null; then
15792
- if fmix_drop_db "$db_name" 2>/dev/null; then
15863
+ if fmix_drop_db "$db_name" 1 2>/dev/null; then
15793
15864
  echo " dropped db $db_name"
15794
15865
  else
15795
15866
  local drop_rc=$?
@@ -17785,7 +17856,7 @@ cmd_gc() {
17785
17856
  if [[ "$confirm" =~ ^[Yy]$ ]]; then
17786
17857
  # db came from a state file — re-validate before SQL (codex P1)
17787
17858
  fmix_guard_db_name "$db" || continue
17788
- if fmix_drop_db "$db"; then
17859
+ if fmix_drop_db "$db" 1; then
17789
17860
  echo " Dropped: $db"
17790
17861
  rm -f "$FMIX_HOME/compile-stamps/$(basename "$sf")"
17791
17862
  rm -f "$sf"
@@ -18023,7 +18094,7 @@ cmd_gc() {
18023
18094
  session_db="$(fmix_read_state "$session_root/state.json" db_name 2>/dev/null)"
18024
18095
  [[ -n "$session_db" ]] || session_db="ct_fmix_s_$session_hash"
18025
18096
  if fmix_db_exists "$session_db"; then
18026
- fmix_drop_db "$session_db" || {
18097
+ fmix_drop_db "$session_db" 1 || {
18027
18098
  local drop_rc=$?
18028
18099
  if fmix_lock_failure_is_fatal "$drop_rc"; then
18029
18100
  fmix_command_lock_finish "$session_lock" "$drop_rc"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@calltelemetry/fmix",
3
- "version": "2.7.15",
3
+ "version": "2.7.16",
4
4
  "description": "Verified native FMix runner for CallTelemetry workspaces",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -26,7 +26,7 @@
26
26
  "directory": "packages/npm"
27
27
  },
28
28
  "fmix": {
29
- "runnerSha256": "35b7561e56fc748e13e4eaf3e3df25f0cb060352f6cf8fb6d0d3edce34eba733",
30
- "sourceTag": "fmix-v2.7.15"
29
+ "runnerSha256": "3c4fbbb7a154db21a68c62906a7e852a98faea0956259feee4b4329fd6f60402",
30
+ "sourceTag": "fmix-v2.7.16"
31
31
  }
32
32
  }