victor HF Staff commited on
Commit
870fbcf
·
1 Parent(s): ac90842

fix: retry connect() forever instead of crashing the Space when pod is restarting

Browse files
Files changed (1) hide show
  1. server.ts +8 -2
server.ts CHANGED
@@ -106,8 +106,14 @@ function broadcast() {
106
 
107
  async function connect() {
108
  status = 'connecting';
109
- const health = await fetch(`${ENDPOINT}/health`).then(r => r.json()).catch(() => null);
110
- if (!health || health.status !== 'ok') throw new Error(`endpoint unreachable: ${ENDPOINT}`);
 
 
 
 
 
 
111
 
112
  const models = await fetch(`${ENDPOINT}/v1/models`).then(r => r.json()).catch(() => null);
113
  remoteModelId = models?.data?.[0]?.id || REMOTE_MODEL;
 
106
 
107
  async function connect() {
108
  status = 'connecting';
109
+ // Retry the health check forever — if the upstream pod is restarting, we
110
+ // want to wait it out, not crash the Space and fall into RUNTIME_ERROR.
111
+ while (true) {
112
+ const health = await fetch(`${ENDPOINT}/health`).then(r => r.json()).catch(() => null);
113
+ if (health && health.status === 'ok') break;
114
+ trace({ type: 'connect_waiting', endpoint: ENDPOINT });
115
+ await new Promise(r => setTimeout(r, 5000));
116
+ }
117
 
118
  const models = await fetch(`${ENDPOINT}/v1/models`).then(r => r.json()).catch(() => null);
119
  remoteModelId = models?.data?.[0]?.id || REMOTE_MODEL;