@calltelemetry/fmix 2.7.14 → 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 +110 -13
  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.14"
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"
@@ -5670,13 +5694,15 @@ fmix_lock_keeper_start() {
5670
5694
  fi
5671
5695
  mkdir -p "$FMIX_HOME/lock-keeper-state" || {
5672
5696
  printf '{"error":"lock_keeper_bootstrap_failed","reason":"state preparation failed"}\n' >&2
5697
+ fmix_error "Check filesystem permissions and agent sandbox write access to FMIX_HOME: $FMIX_HOME"
5673
5698
  return 70
5674
5699
  }
5675
5700
  state="$FMIX_HOME/lock-keeper-state/${lock_name}.${owner_pid}.${RANDOM}.state"
5676
5701
  # Keep the program in argv, not a background heredoc pipe that Bash must
5677
5702
  # fill before exec. A quoted literal also avoids a heredoc-backed command
5678
5703
  # substitution while retaining the single-file Bash 3.2 runner contract.
5679
- keeper_source='import fcntl
5704
+ keeper_source='import errno
5705
+ import fcntl
5680
5706
  import json
5681
5707
  import os
5682
5708
  import signal
@@ -5704,12 +5730,31 @@ def request_stop(_signum, _frame):
5704
5730
  stop_requested = True
5705
5731
 
5706
5732
 
5733
+ def io_failure(operation, target, error):
5734
+ hint = "Check available disk space and filesystem health for FMIX_HOME."
5735
+ if error.errno in (errno.EACCES, errno.EPERM, errno.EROFS):
5736
+ hint = (
5737
+ "Check filesystem permissions and agent sandbox write access to the configured "
5738
+ "FMIX_HOME. Retry only after access is authorized; do not bypass locks or "
5739
+ "relocate only lock state away from the shared cache."
5740
+ )
5741
+ sys.stderr.write(json.dumps({
5742
+ "error": "lock_keeper_io_failed", "operation": operation,
5743
+ "path": str(target), "errno": error.errno, "hint": hint,
5744
+ }) + "\n")
5745
+ sys.stderr.flush()
5746
+ raise SystemExit(70)
5747
+
5748
+
5707
5749
  def publish_state(value):
5708
5750
  temporary = state_path.with_name(
5709
5751
  state_path.name + f".tmp-{os.getpid()}"
5710
5752
  )
5711
- temporary.write_text(value + "\n", encoding="utf-8")
5712
- os.replace(temporary, state_path)
5753
+ try:
5754
+ temporary.write_text(value + "\n", encoding="utf-8")
5755
+ os.replace(temporary, state_path)
5756
+ except OSError as error:
5757
+ io_failure("publish_state", state_path, error)
5713
5758
 
5714
5759
 
5715
5760
  def owner_alive():
@@ -5745,7 +5790,10 @@ def get_holder_info(lock_name, fmix_home):
5745
5790
  signal.signal(signal.SIGTERM, request_stop)
5746
5791
  signal.signal(signal.SIGINT, request_stop)
5747
5792
  publish_state("started")
5748
- fd = os.open(path, os.O_RDWR | os.O_CREAT, 0o600)
5793
+ try:
5794
+ fd = os.open(path, os.O_RDWR | os.O_CREAT, 0o600)
5795
+ except OSError as error:
5796
+ io_failure("open_kernel_lock", path, error)
5749
5797
  os.set_inheritable(fd, False)
5750
5798
  operation = fcntl.LOCK_SH if mode == "shared" else fcntl.LOCK_EX
5751
5799
  try:
@@ -5769,6 +5817,8 @@ try:
5769
5817
  sys.stderr.flush()
5770
5818
  last_heartbeat = now
5771
5819
  time.sleep(0.05)
5820
+ except OSError as error:
5821
+ io_failure("acquire_kernel_lock", path, error)
5772
5822
  else:
5773
5823
  publish_state("stopped")
5774
5824
  raise SystemExit(1)
@@ -8930,8 +8980,19 @@ fmix_db_exists() {
8930
8980
  [[ "$result" == "1" ]]
8931
8981
  }
8932
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
+
8933
8994
  fmix_drop_db() {
8934
- local db_name="$1"
8995
+ local db_name="$1" force_active="${2:-0}"
8935
8996
  fmix_guard_db_name "$db_name" || return 1
8936
8997
  local timeout timeout_ms out rc ddl_lock=""
8937
8998
  if fmix_host_test_lock_enabled; then
@@ -8945,10 +9006,38 @@ fmix_drop_db() {
8945
9006
  PGCONNECT_TIMEOUT=5 PGOPTIONS="${PGOPTIONS:-} -c statement_timeout=${timeout_ms} -c lock_timeout=${timeout_ms}" \
8946
9007
  fmix_psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
8947
9008
  -c "ALTER DATABASE \"${db_name}\" IS_TEMPLATE false" >/dev/null 2>&1 || true
9009
+ local drop_sql="DROP DATABASE IF EXISTS \"${db_name}\""
8948
9010
  out="$(PGCONNECT_TIMEOUT=5 PGOPTIONS="${PGOPTIONS:-} -c statement_timeout=${timeout_ms} -c lock_timeout=${timeout_ms}" \
8949
9011
  fmix_psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
8950
- -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)"
8951
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
8952
9041
  if (( rc != 0 )); then
8953
9042
  printf '%s\n' "$out" | tail -8 >&2
8954
9043
  fmix_error "DROP DATABASE $db_name failed or exceeded ${timeout}s."
@@ -15052,7 +15141,7 @@ cmd_doctor() {
15052
15141
  && fmix_control_psql 5 -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -c "SELECT 1" >/dev/null 2>&1; then
15053
15142
  local s_dbs db_name shash session_found orphan_drop_rc
15054
15143
  s_dbs="$(fmix_control_psql 5 -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
15055
- -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 ' ')"
15056
15145
  for db_name in $s_dbs; do
15057
15146
  [[ -n "$db_name" ]] || continue
15058
15147
  shash="${db_name#ct_fmix_s_}"
@@ -15066,6 +15155,14 @@ cmd_doctor() {
15066
15155
  echo " [WARN] Refusing cleanup for malformed session DB name: $db_name"
15067
15156
  continue
15068
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
15069
15166
  orphan_drop_rc=0
15070
15167
  fmix_drop_orphan_session_db "$db_name" "doctor orphan DB sweep" \
15071
15168
  >/dev/null || orphan_drop_rc=$?
@@ -15763,7 +15860,7 @@ PY
15763
15860
  local db_name
15764
15861
  db_name="ct_fmix_s_${shash}"
15765
15862
  if fmix_guard_db_name "$db_name" 2>/dev/null && fmix_db_exists "$db_name" 2>/dev/null; then
15766
- if fmix_drop_db "$db_name" 2>/dev/null; then
15863
+ if fmix_drop_db "$db_name" 1 2>/dev/null; then
15767
15864
  echo " dropped db $db_name"
15768
15865
  else
15769
15866
  local drop_rc=$?
@@ -17759,7 +17856,7 @@ cmd_gc() {
17759
17856
  if [[ "$confirm" =~ ^[Yy]$ ]]; then
17760
17857
  # db came from a state file — re-validate before SQL (codex P1)
17761
17858
  fmix_guard_db_name "$db" || continue
17762
- if fmix_drop_db "$db"; then
17859
+ if fmix_drop_db "$db" 1; then
17763
17860
  echo " Dropped: $db"
17764
17861
  rm -f "$FMIX_HOME/compile-stamps/$(basename "$sf")"
17765
17862
  rm -f "$sf"
@@ -17997,7 +18094,7 @@ cmd_gc() {
17997
18094
  session_db="$(fmix_read_state "$session_root/state.json" db_name 2>/dev/null)"
17998
18095
  [[ -n "$session_db" ]] || session_db="ct_fmix_s_$session_hash"
17999
18096
  if fmix_db_exists "$session_db"; then
18000
- fmix_drop_db "$session_db" || {
18097
+ fmix_drop_db "$session_db" 1 || {
18001
18098
  local drop_rc=$?
18002
18099
  if fmix_lock_failure_is_fatal "$drop_rc"; then
18003
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.14",
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": "8ee775d32021237d69b3e1d04442f146af595d3c4538e78162389b5da76d9316",
30
- "sourceTag": "fmix-v2.7.14"
29
+ "runnerSha256": "3c4fbbb7a154db21a68c62906a7e852a98faea0956259feee4b4329fd6f60402",
30
+ "sourceTag": "fmix-v2.7.16"
31
31
  }
32
32
  }