Files
ServerKit/templates/serverkit-backend.service.in
T
Juan DenisandClaude Fable 5 751dde7a7d fix(deploy): threaded gunicorn worker — gevent-websocket class broke WebSocket
The app runs flask-socketio with async_mode='threading' (the backend
is real-thread based: jobs scheduler, queue bus, agent registry), where
engineio serves WebSocket via simple-websocket on the raw socket. The
systemd unit template and Dockerfile still launched gunicorn with the
GeventWebSocketWorker, which ALSO answers the upgrade — the client got
HTTP/1.1 101 twice and read the second one as a frame ('Invalid frame
header'), so every panel silently degraded to polling. Proven by
hexdump (double 101, first 'frame' byte 0x48 'H') and fixed live:
single 101 + 0x81 text frame, socket.io connects through Cloudflare.

- unit template + Dockerfile: gunicorn -w 1 --threads 100 (single
  PROCESS stays mandatory — in-memory agent gateway)
- requirements: pin simple-websocket explicitly (was transitive-only)
- CLAUDE.md / ARCHITECTURE.md / SECURITY.md single-worker notes updated

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 00:14:49 -04:00

38 lines
1.3 KiB
Plaintext

[Unit]
Description=ServerKit Backend API
After=network.target docker.service
Wants=docker.service
[Service]
Type=simple
User=@USER@
Group=@USER@
WorkingDirectory=@SERVERKIT_DIR@/backend
Environment="PATH=@SERVERKIT_VENV_DIR@/bin:/usr/local/bin:/usr/bin:/bin"
Environment="FLASK_ENV=production"
Environment="SERVERKIT_INSTALL_DIR=@SERVERKIT_DIR@"
EnvironmentFile=@SERVERKIT_DIR@/.env
# Single PROCESS (-w 1) is mandatory: the agent gateway keeps connected-agent
# state in memory. Threaded worker (not GeventWebSocketWorker): the app runs
# async_mode='threading', where engineio serves WebSocket via simple-websocket
# on the raw socket — a gevent-websocket worker ALSO answers the upgrade,
# producing a double 101 handshake the browser reports as
# "Invalid frame header" (WS then silently degrades to polling).
ExecStart=@SERVERKIT_VENV_DIR@/bin/gunicorn -w 1 --threads 100 -b 0.0.0.0:@PORT@ --timeout 120 --access-logfile @LOG_DIR@/access.log --error-logfile @LOG_DIR@/error.log run:app
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=5
# Security settings (while still allowing system management)
NoNewPrivileges=no
ProtectSystem=no
ProtectHome=no
# Logging
StandardOutput=journal
StandardError=journal
SyslogIdentifier=serverkit
[Install]
WantedBy=multi-user.target