elevate-adapter: catch-all exception handler in handle()

Wrap _handle_inner() in try/except so unhandled exceptions always
produce an error frame instead of silently closing the connection
(which caused exit 255 with no output on the client).
This commit is contained in:
Ollie Agent 2026-07-19 10:28:38 +02:00
parent 6bc4ab6745
commit e47bb9e2ff
1 changed files with 41 additions and 31 deletions

View File

@ -191,45 +191,55 @@ def _execute_on_superpowerd(conn, cmd, cwd, token):
def handle(conn):
with conn:
data = b""
while chunk := conn.recv(65536):
data += chunk
if b"\n" in data:
break
try:
req = json.loads(data)
except json.JSONDecodeError as e:
send_error(conn, f"elevate-adapter: bad request: {e}")
return
_handle_inner(conn)
except Exception as e:
try:
send_error(conn, f"elevate-adapter: internal error: {e}")
except OSError:
pass
cmd = req.get("cmd", "")
cwd = req.get("cwd", os.getcwd())
token = os.environ.get("SUPERPOWERD_SESSION_TOKEN", "")
def _handle_inner(conn):
data = b""
while chunk := conn.recv(65536):
data += chunk
if b"\n" in data:
break
# First attempt
sp_data, done = _execute_on_superpowerd(conn, cmd, cwd, token)
if done:
_finish_response(conn, sp_data)
return
try:
req = json.loads(data)
except json.JSONDecodeError as e:
send_error(conn, f"elevate-adapter: bad request: {e}")
return
# Auth error — refresh token and retry once
with _token_lock:
# Re-read in case another thread already refreshed
current = os.environ.get("SUPERPOWERD_SESSION_TOKEN", "")
if current == token:
new_token = refresh_token()
else:
new_token = current
cmd = req.get("cmd", "")
cwd = req.get("cwd", os.getcwd())
if not new_token:
# Refresh failed — report the original auth error
_finish_response(conn, sp_data)
return
token = os.environ.get("SUPERPOWERD_SESSION_TOKEN", "")
sp_data, _ = _execute_on_superpowerd(conn, cmd, cwd, new_token)
# First attempt
sp_data, done = _execute_on_superpowerd(conn, cmd, cwd, token)
if done:
_finish_response(conn, sp_data)
return
# Auth error — refresh token and retry once
with _token_lock:
# Re-read in case another thread already refreshed
current = os.environ.get("SUPERPOWERD_SESSION_TOKEN", "")
if current == token:
new_token = refresh_token()
else:
new_token = current
if not new_token:
# Refresh failed — report the original auth error
_finish_response(conn, sp_data)
return
sp_data, _ = _execute_on_superpowerd(conn, cmd, cwd, new_token)
_finish_response(conn, sp_data)
def _finish_response(conn, sp_data):