Просмотр исходного кода

test(subprocess): accept zombie quiescence

Tianyi Cui 1 месяц назад
Родитель
Сommit
ea047cc634
1 измененных файлов с 11 добавлено и 1 удалено
  1. 11 1
      packages/subprocess/subprocess-local/tests/spawn.spec.ts

+ 11 - 1
packages/subprocess/subprocess-local/tests/spawn.spec.ts

@@ -60,7 +60,7 @@ function spec(command: string, overrides: SpecOverrides = {}) {
   }
 }
 
-/** Poll until a pid no longer exists (kill(pid, 0) throws ESRCH). */
+/** Poll until a pid no longer exists, or is only a zombie on Linux. */
 async function waitGone(pid: number, timeoutMs = 5_000): Promise<void> {
   const deadline = Date.now() + timeoutMs
   while (Date.now() < deadline) {
@@ -69,6 +69,16 @@ async function waitGone(pid: number, timeoutMs = 5_000): Promise<void> {
     } catch {
       return
     }
+    if (process.platform === 'linux') {
+      try {
+        const stat = readFileSync(`/proc/${pid}/stat`, 'utf8')
+        const state = stat.slice(stat.lastIndexOf(')') + 2, stat.lastIndexOf(')') + 3)
+        if (state === 'Z' || state === 'X') return
+      } catch (error: unknown) {
+        if ((error as NodeJS.ErrnoException).code === 'ENOENT') return
+        throw error
+      }
+    }
     await new Promise(resolve => setTimeout(resolve, 20))
   }
   throw new Error(`pid ${pid} still alive after ${timeoutMs}ms`)