@infernetprotocol/cli 0.1.59 → 0.1.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/commands/start.js +33 -8
  2. package/package.json +7 -7
package/commands/start.js CHANGED
@@ -83,6 +83,16 @@ const DEFAULT_HEARTBEAT_MS = 30_000;
83
83
  const DEFAULT_POLL_MS = 15_000;
84
84
  const UPDATE_CHECK_MS = 5 * 60 * 1000; // 5 minutes
85
85
  const UPDATE_DRAIN_MS = 10 * 60 * 1000; // wait for in-flight jobs before restarting
86
+ const SHUTDOWN_DEADLINE_MS = 5_000; // hard exit: a half-shut daemon must never outlive its pidfile
87
+ const OFFLINE_HEARTBEAT_TIMEOUT_MS = 3_000;
88
+
89
+ function withTimeout(promise, ms, what) {
90
+ let timer;
91
+ const timeout = new Promise((_, reject) => {
92
+ timer = setTimeout(() => reject(new Error(`${what} timed out after ${ms} ms`)), ms);
93
+ });
94
+ return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
95
+ }
86
96
 
87
97
  async function writePidFile(pid) {
88
98
  const p = getDaemonPidPath();
@@ -98,6 +108,18 @@ function removeSocketFile() {
98
108
  try { unlinkSync(getDaemonSocketPath()); } catch { /* ignore */ }
99
109
  }
100
110
 
111
+ // Exit paths clean up only what is still ours: when another daemon has since started, the pidfile
112
+ // holds its pid and the socket is its listener. Deleting them made that daemon unreachable.
113
+ function ownsPidFile() {
114
+ try { return readFileSync(getDaemonPidPath(), 'utf8').trim() === String(process.pid); } catch { return false; }
115
+ }
116
+
117
+ async function removeOwnPidAndSocket() {
118
+ if (!ownsPidFile()) return;
119
+ removeSocketFile();
120
+ await removePidFile();
121
+ }
122
+
101
123
  // ---------------------------------------------------------------------------
102
124
  // Entry point — branch between "spawn detached" and "run foreground loop".
103
125
  // ---------------------------------------------------------------------------
@@ -1349,10 +1371,16 @@ async function runDaemon(args, ctx) {
1349
1371
  if (shuttingDown) return;
1350
1372
  shuttingDown = true;
1351
1373
  process.stdout.write(`\nReceived ${signal}; shutting down...\n`);
1374
+ // Exit no matter what hangs below. A daemon that has released its socket and pidfile but
1375
+ // is still alive is invisible to `infernet start`, so every restart stacked another one.
1376
+ setTimeout(() => {
1377
+ process.stderr.write(`shutdown did not finish in ${SHUTDOWN_DEADLINE_MS} ms; exiting anyway\n`);
1378
+ process.exit(0);
1379
+ }, SHUTDOWN_DEADLINE_MS);
1352
1380
  if (heartbeatTimer) clearInterval(heartbeatTimer);
1353
1381
  if (pollTimer) clearInterval(pollTimer);
1354
1382
  try {
1355
- await client.heartbeat({ status: 'offline' });
1383
+ await withTimeout(client.heartbeat({ status: 'offline' }), OFFLINE_HEARTBEAT_TIMEOUT_MS, 'offline heartbeat');
1356
1384
  } catch (err) {
1357
1385
  process.stderr.write(`offline heartbeat failed: ${err?.message ?? err}\n`);
1358
1386
  }
@@ -1360,8 +1388,7 @@ async function runDaemon(args, ctx) {
1360
1388
  if (p2pServer) { try { p2pServer.close(); } catch {} }
1361
1389
  if (healthServer) { try { healthServer.close(); } catch {} }
1362
1390
  try { await shutdownEngine(); } catch {}
1363
- removeSocketFile();
1364
- await removePidFile();
1391
+ await removeOwnPidAndSocket();
1365
1392
  process.stdout.write('bye\n');
1366
1393
  process.exit(0);
1367
1394
  };
@@ -1503,8 +1530,7 @@ async function runDaemon(args, ctx) {
1503
1530
  if (ipcServer) { try { ipcServer.close(); } catch {} }
1504
1531
  if (p2pServer) { try { p2pServer.close(); } catch {} }
1505
1532
  if (healthServer) { try { healthServer.close(); } catch {} }
1506
- removeSocketFile();
1507
- await removePidFile();
1533
+ await removeOwnPidAndSocket();
1508
1534
  return 0;
1509
1535
  }
1510
1536
 
@@ -1602,9 +1628,8 @@ async function runDaemon(args, ctx) {
1602
1628
  if (ipcServer) try { ipcServer.close(); } catch {}
1603
1629
  if (p2pServer) try { p2pServer.close(); } catch {}
1604
1630
  if (healthServer) try { healthServer.close(); } catch {}
1605
- try { await shutdownEngine(); } catch {}
1606
- removeSocketFile();
1607
- await removePidFile();
1631
+ try { await withTimeout(shutdownEngine(), SHUTDOWN_DEADLINE_MS, 'engine shutdown'); } catch {}
1632
+ await removeOwnPidAndSocket();
1608
1633
 
1609
1634
  // Small grace period so OS reclaims the ports before the new process binds.
1610
1635
  await new Promise((r) => setTimeout(r, 500));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infernetprotocol/cli",
3
- "version": "0.1.59",
3
+ "version": "0.1.61",
4
4
  "description": "GPU-node CLI / daemon for the Infernet Protocol — register a GPU server with a Supabase control plane and start earning.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -30,12 +30,12 @@
30
30
  "ora": "^9.4.0",
31
31
  "react": "^17.0.2",
32
32
  "react-blessed": "^0.7.2",
33
- "@infernetprotocol/auth": "0.1.59",
34
- "@infernetprotocol/deploy-providers": "0.1.59",
35
- "@infernetprotocol/engine": "0.1.59",
36
- "@infernetprotocol/gpu": "0.1.59",
37
- "@infernetprotocol/rpc-adapter": "0.1.59",
38
- "@infernetprotocol/config": "0.1.59"
33
+ "@infernetprotocol/config": "0.1.61",
34
+ "@infernetprotocol/deploy-providers": "0.1.61",
35
+ "@infernetprotocol/engine": "0.1.61",
36
+ "@infernetprotocol/gpu": "0.1.61",
37
+ "@infernetprotocol/auth": "0.1.61",
38
+ "@infernetprotocol/rpc-adapter": "0.1.61"
39
39
  },
40
40
  "repository": {
41
41
  "type": "git",