fix(media): relays release their live-channel claims on shutdown

LiveRelay.stop() fired the claim releases and forgot them, and nothing
closed the shared backend's Postgres pool: on a redeploy the process could
exit first, leaving the other instances to follow a dead relay until the
claim went stale (30 s). stop() is now async and resolves once the
releases are written; MediaService.stop() passes it through; the
container awaits it and closes the backend on SIGTERM, and `media serve`
gained SIGINT/SIGTERM handling that does the same and closes open sockets
so publishers and viewers reconnect elsewhere.

Verified: a test that a stopped relay's claim is gone at once; a real
`media serve --s3 --pg --advertise` with a live publisher: SIGTERM →
exit in ~0.4 s, its shade_live_publishers row 1 → 0, the publisher
reconnecting. Suites green (media 101, storage-postgres 20, server 55,
cli 15).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-18 22:58:44 +02:00
parent 0bbeb5682b
commit 5f7c6ff4b4
6 changed files with 43 additions and 6 deletions

View File

@@ -316,6 +316,7 @@ if (vaultDisabled) {
// Several instances share one library with SHADE_MEDIA_S3_URL (sealed bytes
// in a bucket) + SHADE_MEDIA_PG_URL (records and live pins in Postgres).
let mediaService: import('@shade/media/server').MediaService | null = null;
let mediaBackend: import('@shade/storage-postgres').SharedMediaBackend | null = null;
const mediaS3 = process.env.SHADE_MEDIA_S3_URL;
const mediaPg = process.env.SHADE_MEDIA_PG_URL ?? process.env.SHADE_PREKEY_PG_URL;
if (process.env.SHADE_DISABLE_MEDIA === '1') {
@@ -353,6 +354,7 @@ if (process.env.SHADE_DISABLE_MEDIA === '1') {
});
}
}
mediaBackend = backend;
if (backend !== null) {
// Live channels across instances: each says where its publishers are, so
// the others can follow. Without its own address an instance cannot be
@@ -461,7 +463,8 @@ async function shutdown(signal: string) {
try {
cleanupTask.stop();
inboxPrune.stop();
mediaService?.stop();
await mediaService?.stop();
await mediaBackend?.close();
server.stop();
if ('close' in store && typeof store.close === 'function') {
await store.close();