Commit 5528680
authored
fix: prevent infinite app spawn loop in production builds (#28)
* fix: prevent infinite app spawn loop in production builds
The server launcher used `spawn(process.execPath, ...)` to start the
standalone server process in production. Since process.execPath is the
Electron binary, this launched another full Electron app instance instead
of a Node.js process, which in turn tried to launch another server,
creating an infinite spawn loop.
Fix: use Electron's `utilityProcess.fork()` for production builds, which
runs the server script as a proper Node.js child process without creating
another Electron window. Dev mode continues using `npx tsx` via
child_process.spawn.
Also adds `app.requestSingleInstanceLock()` as a safety net to prevent
multiple app instances from ever running simultaneously.
* fix: resolve native module loading in utilityProcess
The utilityProcess can't access modules inside the asar archive and
doesn't have Electron's ASAR require() patching. This caused three
failures in the production build:
1. Server entry detected as index.js but tsup outputs index.cjs
(package.json has "type": "module") — add .cjs to isDirectRun check
2. JS dependencies (fastify, pino, etc.) externalized by default —
add tsup config with noExternal regex to bundle all JS deps while
keeping only native modules (node-pty, better-sqlite3) external
3. Native module resolution broken because Electron's utility_init wraps
Module._resolveFilename — patch Module._load instead via tsup banner
to intercept require() calls and redirect native modules to their
absolute paths in app.asar.unpacked/node_modules/
4. better-sqlite3 uses require('bindings') which is trapped in the asar —
shim the bindings package in the Module._load patch to resolve
.node addons directly from known unpacked paths
* fix: address PR review feedback
- Update comment to say index.cjs instead of index.js
- Simplify exit listener — both ChildProcess and UtilityProcess
support .on('exit'), no branching needed
- Handle bindings object form { bindings: 'name.node' }
- Log errors in native module patch instead of silently swallowing1 parent d033c12 commit 5528680
File tree
5 files changed
+195
-53
lines changed- packages/server
- src
- src/main
- server
5 files changed
+195
-53
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
108 | 111 | | |
109 | 112 | | |
110 | 113 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
13 | 21 | | |
14 | 22 | | |
15 | 23 | | |
| |||
244 | 252 | | |
245 | 253 | | |
246 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
247 | 264 | | |
248 | 265 | | |
249 | 266 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
16 | 24 | | |
17 | 25 | | |
18 | 26 | | |
19 | 27 | | |
20 | 28 | | |
21 | 29 | | |
22 | | - | |
23 | 30 | | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | 31 | | |
47 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
48 | 91 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
54 | 95 | | |
| 96 | + | |
| 97 | + | |
55 | 98 | | |
56 | 99 | | |
57 | | - | |
58 | | - | |
59 | 100 | | |
60 | 101 | | |
61 | 102 | | |
| |||
71 | 112 | | |
72 | 113 | | |
73 | 114 | | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | 115 | | |
81 | 116 | | |
82 | 117 | | |
| |||
95 | 130 | | |
96 | 131 | | |
97 | 132 | | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
104 | 144 | | |
105 | | - | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
106 | 149 | | |
107 | 150 | | |
108 | 151 | | |
109 | 152 | | |
110 | 153 | | |
111 | 154 | | |
112 | | - | |
| 155 | + | |
113 | 156 | | |
114 | 157 | | |
115 | 158 | | |
116 | 159 | | |
117 | | - | |
| 160 | + | |
118 | 161 | | |
119 | 162 | | |
120 | | - | |
| 163 | + | |
121 | 164 | | |
122 | 165 | | |
123 | 166 | | |
| |||
142 | 185 | | |
143 | 186 | | |
144 | 187 | | |
145 | | - | |
| 188 | + | |
| 189 | + | |
146 | 190 | | |
147 | 191 | | |
148 | | - | |
| 192 | + | |
| 193 | + | |
149 | 194 | | |
150 | 195 | | |
0 commit comments