| 73 | // /proc to wait only for live group members. Remove this when every supported |
| 74 | // Linux host guarantees a reaping init or Node exposes a live-group query. |
| 75 | function linuxProcessGroupHasLiveMembers(processGroupId) { |
| 76 | // /proc can report unknown entry types, causing Dirent enumeration to lstat |
| 77 | // a process that exits before enumeration completes. Read names only so all |
| 78 | // disappearing-process checks stay inside the per-entry ENOENT guard. |
| 79 | for (const entry of readdirSync("/proc")) { |
| 80 | if (!/^\d+$/u.test(entry)) { |
| 81 | continue; |
| 82 | } |
| 83 | try { |
| 84 | const stat = readFileSync(`/proc/${entry}/stat`, "utf8"); |
| 85 | const [state, , groupId] = stat |
| 86 | .slice(stat.lastIndexOf(")") + 1) |
| 87 | .trim() |
| 88 | .split(/\s+/u); |
| 89 | if ( |
| 90 | groupId === String(processGroupId) && |
| 91 | state !== "Z" && |
| 92 | state !== "X" |
| 93 | ) { |
| 94 | return true; |
| 95 | } |
| 96 | } catch (error) { |
| 97 | // proc_single_show can return ESRCH after open succeeds if the task |
| 98 | // exits before stat is read. Both errors prove this entry disappeared; |
| 99 | // access failures and other unknown observations must still fail closed. |
| 100 | if (error.code !== "ENOENT" && error.code !== "ESRCH") { |
| 101 | throw error; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | function waitForPosixProcessGroupExit(processGroupId) { |
| 109 | return new Promise((resolve, reject) => { |