Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

1.9 KB
Raw Blame
#!/usr/bin/env bash
# Owner-only lifecycle lock and signal-safe pending-child cleanup.

gateway_lock_acquire() {
  gateway_private_file "$LOCK_FILE"
  exec {GATEWAY_LOCK_FD}>>"$LOCK_FILE" || gateway_fail "cannot open gateway lifecycle lock"
  if ! flock -x -w 12 "$GATEWAY_LOCK_FD"; then
    exec {GATEWAY_LOCK_FD}>&- || true
    GATEWAY_LOCK_FD=''
    gateway_fail "another gateway lifecycle operation is still running"
  fi
  LOCK_HELD=1
}

gateway_lock_release() {
  [ "${LOCK_HELD:-0}" = 1 ] || return 0
  if [ -n "${GATEWAY_LOCK_FD:-}" ]; then
    flock -u "$GATEWAY_LOCK_FD" 2>/dev/null || true
    exec {GATEWAY_LOCK_FD}>&- || true
    GATEWAY_LOCK_FD=''
  fi
  LOCK_HELD=0
}

GATEWAY_PENDING_CLEANUP=0

gateway_operation_cleanup() {
  [ "${GATEWAY_PENDING_CLEANUP:-0}" = 1 ] || return 0
  if declare -F gateway_cleanup_failed_start >/dev/null 2>&1; then
    # A loaded managed-provider adapter owns provider + runtime rollback.
    gateway_cleanup_failed_start || true
  elif declare -F gateway_stop_pending_runtime >/dev/null 2>&1; then
    # Local-only fallback must remain signal-safe even when no provider adapter
    # has been loaded. Once the runtime has a captured identity, stop that exact
    # owned process; otherwise roll back only the provisional PID/start-ticks.
    if [ "${RUNTIME_STARTED_NOW:-false}" = true ] && [ "${RUNTIME_IDENTITY:-null}" != null ]; then
      gateway_stop_identity "$RUNTIME_IDENTITY" || true
    else
      gateway_stop_pending_runtime || true
    fi
  fi
  GATEWAY_PENDING_CLEANUP=0
}

gateway_with_lock() {
  gateway_lock_acquire
  # HUP/INT/TERM become ordinary non-zero exits. EXIT then rolls back any
  # pre-persistence child processes before releasing the lifecycle lock.
  trap 'gateway_operation_cleanup; gateway_lock_release' EXIT
  trap 'exit 129' HUP
  trap 'exit 130' INT
  trap 'exit 143' TERM
  "$@"
  gateway_operation_cleanup
  gateway_lock_release
  trap - EXIT HUP INT TERM
}