Commit f0e4956
[PHP] Inline rotatePHPRuntime() into the PHP class (#2559)
## Motivation for the change, related issues
Moves the `rotatePHPRuntime()` logic inside the PHP class and properly
await them to prevent async rotation failures:
```
{
type: 'request.error',
error: Error: PHP.run() failed with exit code 255.
at runExecutionFunction.then.dispatchEvent.type (/Users/cloudnik/www/Automattic/core/plugins/wordpress-playground/packages/php-wasm/universal/src/lib/php.ts:1052:14),
source: 'php-wasm'
}
Trace: hotSwapPHPRuntime called on a disposed PHP instance
at PHP.hotSwapPHPRuntime (/Users/cloudnik/www/Automattic/core/plugins/wordpress-playground/packages/php-wasm/universal/src/lib/php.ts:1272:13)
at rotateRuntime (/Users/cloudnik/www/Automattic/core/plugins/wordpress-playground/packages/php-wasm/universal/src/lib/rotate-php-runtime.ts:45:14)
at async rotateRuntimeForPhpWasmError (/Users/cloudnik/www/Automattic/core/plugins/wordpress-playground/packages/php-wasm/universal/src/lib/rotate-php-runtime.ts:64:4)
```
## Implementation
The `rotatePHPRuntime()` function kills the "worn out" PHP Emscripten
runtimes and replaces them with new ones. php-fpm does the same thing.
Rotating the runtime happens in response to a few events emitted by the
PHP class. However, rotation is an async operation and event listeners
are expected to be synchronous. As a result, a PHP instance may get
killed and the rotate handler will attempt to rotate it asynchronously,
causing errors such as the one cited above.
This PR inlines that logic in the PHP class via a new
`php.enableRuntimeRotation()` method. The PHP instance now keeps track
if the rotation condition internally and optionally rotates the runtime
before every `php.run()`, `php.runStream()`, and `php.cli()` call. This
approach also has other benefits:
* Ability to call `php.cli()` and `php.run()` multiple times without
trashing the instance – the rotation is handled internally.
* It creates a way to get rid of the `primary` and `secondary` PHP
instance concept in the PHPProcessManager class. We no longer need to
worry about accidentally trashing the primary instance by calling
`.cli()` on it.
* The rotation is now lazy, not eager, and only happens on the next PHP
code execution.
### Potential follow-up work
* Expose a static `PHP.create( runtimeFactory )` method to simplify the
instance constructions. Right now you need to do `const php = new PHP(
runtimeId ); PHP.enableRuntimeRotation( runtimeFactory );`.
## Testing Instructions (or ideally a Blueprint)
CI
---------
Co-authored-by: Brandon Payton <[email protected]>1 parent 3bf8006 commit f0e4956
File tree
7 files changed
+191
-214
lines changed- packages
- php-wasm
- fs-journal/src/lib
- node/src/test
- universal/src/lib
- playground/wordpress/src
7 files changed
+191
-214
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
178 | 178 | | |
179 | 179 | | |
180 | 180 | | |
181 | | - | |
| 181 | + | |
182 | 182 | | |
183 | 183 | | |
184 | 184 | | |
185 | | - | |
| 185 | + | |
186 | 186 | | |
187 | 187 | | |
188 | 188 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2868 | 2868 | | |
2869 | 2869 | | |
2870 | 2870 | | |
| 2871 | + | |
| 2872 | + | |
| 2873 | + | |
| 2874 | + | |
| 2875 | + | |
| 2876 | + | |
| 2877 | + | |
| 2878 | + | |
| 2879 | + | |
| 2880 | + | |
| 2881 | + | |
| 2882 | + | |
| 2883 | + | |
| 2884 | + | |
| 2885 | + | |
| 2886 | + | |
2871 | 2887 | | |
2872 | 2888 | | |
2873 | 2889 | | |
| |||
Lines changed: 21 additions & 73 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
15 | 14 | | |
16 | | - | |
| 15 | + | |
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
21 | 20 | | |
22 | | - | |
23 | | - | |
| 21 | + | |
24 | 22 | | |
25 | 23 | | |
26 | 24 | | |
| |||
55 | 53 | | |
56 | 54 | | |
57 | 55 | | |
58 | | - | |
59 | | - | |
| 56 | + | |
60 | 57 | | |
61 | 58 | | |
62 | 59 | | |
| |||
89 | 86 | | |
90 | 87 | | |
91 | 88 | | |
92 | | - | |
93 | | - | |
| 89 | + | |
94 | 90 | | |
95 | 91 | | |
96 | 92 | | |
| |||
192 | 188 | | |
193 | 189 | | |
194 | 190 | | |
195 | | - | |
196 | | - | |
| 191 | + | |
197 | 192 | | |
198 | 193 | | |
199 | 194 | | |
| |||
222 | 217 | | |
223 | 218 | | |
224 | 219 | | |
225 | | - | |
226 | | - | |
| 220 | + | |
227 | 221 | | |
228 | 222 | | |
229 | 223 | | |
230 | 224 | | |
231 | 225 | | |
232 | 226 | | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | 227 | | |
247 | 228 | | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | 229 | | |
257 | 230 | | |
258 | 231 | | |
259 | 232 | | |
260 | 233 | | |
261 | | - | |
262 | | - | |
| 234 | + | |
263 | 235 | | |
264 | 236 | | |
265 | 237 | | |
266 | 238 | | |
267 | 239 | | |
268 | | - | |
| 240 | + | |
269 | 241 | | |
270 | 242 | | |
271 | 243 | | |
272 | 244 | | |
| 245 | + | |
273 | 246 | | |
274 | 247 | | |
275 | 248 | | |
276 | 249 | | |
277 | 250 | | |
278 | 251 | | |
279 | | - | |
280 | | - | |
| 252 | + | |
281 | 253 | | |
282 | 254 | | |
283 | 255 | | |
| |||
296 | 268 | | |
297 | 269 | | |
298 | 270 | | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | 271 | | |
325 | 272 | | |
326 | 273 | | |
| |||
331 | 278 | | |
332 | 279 | | |
333 | 280 | | |
334 | | - | |
335 | | - | |
| 281 | + | |
336 | 282 | | |
337 | 283 | | |
338 | 284 | | |
| |||
353 | 299 | | |
354 | 300 | | |
355 | 301 | | |
356 | | - | |
357 | | - | |
| 302 | + | |
358 | 303 | | |
359 | 304 | | |
360 | 305 | | |
| |||
371 | 316 | | |
372 | 317 | | |
373 | 318 | | |
374 | | - | |
375 | | - | |
| 319 | + | |
376 | 320 | | |
377 | 321 | | |
378 | 322 | | |
| |||
395 | 339 | | |
396 | 340 | | |
397 | 341 | | |
398 | | - | |
399 | | - | |
| 342 | + | |
400 | 343 | | |
401 | 344 | | |
402 | 345 | | |
403 | 346 | | |
404 | 347 | | |
405 | 348 | | |
406 | | - | |
| 349 | + | |
| 350 | + | |
407 | 351 | | |
408 | 352 | | |
409 | 353 | | |
| |||
415 | 359 | | |
416 | 360 | | |
417 | 361 | | |
418 | | - | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
419 | 366 | | |
420 | 367 | | |
421 | 368 | | |
| |||
429 | 376 | | |
430 | 377 | | |
431 | 378 | | |
| 379 | + | |
432 | 380 | | |
433 | 381 | | |
434 | 382 | | |
0 commit comments