@notionhq/custom-blocks-dev-shell 0.1.37 → 0.1.38

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.
@@ -17,7 +17,7 @@ export class ProcessSupervisor {
17
17
  }
18
18
  addResource(resource) {
19
19
  if (this.shuttingDown) {
20
- closeResource(resource);
20
+ void closeResource(resource);
21
21
  return;
22
22
  }
23
23
  this.resources.push(resource);
@@ -49,7 +49,11 @@ export class ProcessSupervisor {
49
49
  this.shutdown("SIGTERM");
50
50
  });
51
51
  proc.on("exit", code => {
52
- this.processes.delete(proc);
52
+ // Retain an unexpectedly failed process until cleanup. Its PID may
53
+ // still identify a detached process group with live descendants.
54
+ if (!this.shuttingDown && (code === 0 || code === null)) {
55
+ this.processes.delete(proc);
56
+ }
53
57
  if (this.shuttingDown || code === 0 || code === null) {
54
58
  return;
55
59
  }
@@ -61,6 +65,9 @@ export class ProcessSupervisor {
61
65
  }
62
66
  shutdown(signal) {
63
67
  if (this.shuttingDown) {
68
+ if (signal !== "exit") {
69
+ this.forceShutdown();
70
+ }
64
71
  return;
65
72
  }
66
73
  this.shuttingDown = true;
@@ -68,44 +75,82 @@ export class ProcessSupervisor {
68
75
  (this.processes.size > 0 || this.resources.length > 0)) {
69
76
  console.log(`\n${dim}Shutting down...${reset}`);
70
77
  }
71
- for (const resource of this.resources) {
72
- closeResource(resource);
73
- }
74
78
  for (const [proc, detached] of this.processes) {
75
79
  killProcess(proc, detached, "SIGTERM");
76
80
  }
77
81
  if (signal === "exit") {
82
+ for (const resource of this.resources) {
83
+ void closeResource(resource);
84
+ }
78
85
  return;
79
86
  }
80
- const timer = setTimeout(() => {
81
- for (const [proc, detached] of this.processes) {
82
- killProcess(proc, detached, "SIGKILL");
83
- }
84
- process.exit(process.exitCode ?? 0);
85
- }, SHUTDOWN_GRACE_MS);
86
- timer.unref();
87
+ void this.finishShutdown();
88
+ }
89
+ async finishShutdown() {
90
+ const resourceCleanup = Promise.all(this.resources.map(resource => closeResource(resource)));
91
+ const processCleanup = Promise.all([...this.processes.keys()].map(proc => waitForExit(proc)));
92
+ let finished = false;
93
+ await Promise.race([
94
+ Promise.all([resourceCleanup, processCleanup]).then(() => {
95
+ finished = true;
96
+ }),
97
+ delay(SHUTDOWN_GRACE_MS),
98
+ ]);
99
+ if (!finished) {
100
+ this.forceShutdown();
101
+ return;
102
+ }
103
+ // A process can exit before its descendants do. Re-signal every
104
+ // remaining process group even after the direct child has exited.
105
+ this.forceKillProcesses();
106
+ process.exit(process.exitCode ?? 0);
107
+ }
108
+ forceShutdown() {
109
+ this.forceKillProcesses();
110
+ process.exit(process.exitCode ?? 130);
111
+ }
112
+ forceKillProcesses() {
113
+ for (const [proc, detached] of this.processes) {
114
+ killProcess(proc, detached, "SIGKILL", true);
115
+ }
87
116
  }
88
117
  }
89
- function closeResource(resource) {
118
+ async function closeResource(resource) {
90
119
  try {
91
- resource();
120
+ await resource();
92
121
  }
93
122
  catch { }
94
123
  }
95
- function killProcess(proc, detached, signal) {
96
- if (proc.pid === undefined || proc.killed) {
124
+ function killProcess(proc, detached, signal, force = false) {
125
+ const pid = proc.pid;
126
+ if (pid === undefined) {
97
127
  return;
98
128
  }
99
129
  try {
100
130
  if (detached && process.platform !== "win32") {
101
- process.kill(-proc.pid, signal);
131
+ process.kill(-pid, signal);
132
+ return;
102
133
  }
103
- else {
134
+ if (force || isProcessRunning(proc)) {
104
135
  proc.kill(signal);
105
136
  }
106
137
  }
107
138
  catch { }
108
139
  }
140
+ function isProcessRunning(proc) {
141
+ return proc.exitCode === null && proc.signalCode === null;
142
+ }
143
+ function waitForExit(proc) {
144
+ if (proc.exitCode !== null || proc.signalCode !== null) {
145
+ return Promise.resolve();
146
+ }
147
+ return new Promise(resolve => {
148
+ proc.once("exit", () => resolve());
149
+ });
150
+ }
151
+ function delay(milliseconds) {
152
+ return new Promise(resolve => setTimeout(resolve, milliseconds));
153
+ }
109
154
  export function installProcessSignalHandlers(run) {
110
155
  for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
111
156
  process.on(signal, () => run.shutdown(signal));
@@ -51,7 +51,11 @@ const createShellLaunch = async ({ args, plan, log }) => {
51
51
  }
52
52
  return {
53
53
  processes: [],
54
- resources: [() => server.close()],
54
+ resources: [
55
+ () => new Promise(resolve => {
56
+ server.close(() => resolve());
57
+ }),
58
+ ],
55
59
  };
56
60
  };
57
61
  async function main() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notionhq/custom-blocks-dev-shell",
3
- "version": "0.1.37",
3
+ "version": "0.1.38",
4
4
  "description": "Local preview shell for Notion custom block workers.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -34,6 +34,7 @@
34
34
  "@types/react": "^19.2.14",
35
35
  "@types/react-dom": "^19.2.3",
36
36
  "@vitejs/plugin-react": "^6.0.1",
37
+ "@vitest/coverage-v8": "^4.1.5",
37
38
  "tailwindcss": "^4.2.4",
38
39
  "typescript": "^6.0.3",
39
40
  "vite": "^8.0.10",