pi-webapp 0.1.36-alpha.1 → 0.1.37

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.
package/bin/pi-webapp.mjs CHANGED
@@ -144,6 +144,8 @@ async function serve(pi, args) {
144
144
  let stopCommand;
145
145
  let stopPreflightTimer;
146
146
  let stopPreflightId;
147
+ let stopCheckRunning = false;
148
+ let stopEpoch = 0;
147
149
  const instanceId = randomUUID();
148
150
  const startedAt = performance.now();
149
151
  const lifecycle = (event, fields = {}) => {
@@ -173,13 +175,17 @@ async function serve(pi, args) {
173
175
  clearTimeout(stopPreflightTimer);
174
176
  stopPreflightId = undefined;
175
177
  lifecycle("launcher_stop_refused", { reason: stopReason });
176
- stopping = false;
177
178
  restartRequest = null;
178
- void readFile(stopPath, "utf8").then(value => value.trim() === nonce ? unlink(stopPath) : undefined).catch(() => {});
179
+ // Keep the gate closed until the rejected request is gone. Otherwise the
180
+ // interval can read that same nonce again and launch an unsolicited drain,
181
+ // racing the caller's explicit retry (or leaving an idle host draining).
182
+ void readFile(stopPath, "utf8").then(value => value.trim() === nonce ? unlink(stopPath) : undefined)
183
+ .catch(() => {}).finally(() => { stopping = false; });
179
184
  };
180
185
  const shutdown = (reason) => {
181
186
  if (stopping) return;
182
187
  stopping = true;
188
+ stopEpoch++;
183
189
  stopReason = reason;
184
190
  lifecycle("launcher_stop_requested", { reason });
185
191
  // RPC EOF can exit even when an extension's shutdown handler fails. Drain
@@ -200,18 +206,27 @@ async function serve(pi, args) {
200
206
  };
201
207
  const startupTimer = setTimeout(() => fail(`Pi 启动超时。日志:${logPath}`), 60000);
202
208
  const stopTimer = setInterval(async () => {
203
- try { if ((await readFile(stopPath, "utf8")).trim() === nonce) shutdown("stop_request"); }
204
- catch { /* No stop request. */ }
205
- if (stopping) return;
209
+ if (stopping || stopCheckRunning) return;
210
+ stopCheckRunning = true;
211
+ const epoch = stopEpoch;
206
212
  try {
207
- const request = JSON.parse(await readFile(restartPath, "utf8"));
208
- const prepared = JSON.parse(await readFile(updatePath, "utf8"));
209
- const selected = desktopPackages ? await readDesktopRelease(agentDir, packageVersion) : prepared;
210
- if ((runtimeKind !== "desktop" || desktopPackages) && request.nonce === nonce && request.launcher === selected?.launcher && existsSync(request.launcher)) {
211
- restartRequest = request;
212
- shutdown("restart_request");
213
- }
214
- } catch { /* No valid restart request. */ }
213
+ try {
214
+ const request = (await readFile(stopPath, "utf8")).trim();
215
+ if (epoch !== stopEpoch || stopping) return;
216
+ if (request === nonce) shutdown("stop_request");
217
+ } catch { /* No stop request. */ }
218
+ if (stopping || epoch !== stopEpoch) return;
219
+ try {
220
+ const request = JSON.parse(await readFile(restartPath, "utf8"));
221
+ const prepared = JSON.parse(await readFile(updatePath, "utf8"));
222
+ const selected = desktopPackages ? await readDesktopRelease(agentDir, packageVersion) : prepared;
223
+ if (stopping || epoch !== stopEpoch) return;
224
+ if ((runtimeKind !== "desktop" || desktopPackages) && request.nonce === nonce && request.launcher === selected?.launcher && existsSync(request.launcher)) {
225
+ restartRequest = request;
226
+ shutdown("restart_request");
227
+ }
228
+ } catch { /* No valid restart request. */ }
229
+ } finally { stopCheckRunning = false; }
215
230
  }, 400);
216
231
  process.on("SIGTERM", () => shutdown("signal_sigterm"));
217
232
  process.on("SIGINT", () => shutdown("signal_sigint"));