GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pdf-parser/integrations/javascript/subprocess.mjs at main · Tom-R-Main/pdf-parser

Expand file tree

/

subprocess.mjs

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
34 lines (32 loc) · 2 KB
/

subprocess.mjs

Copy path

File metadata and controls

34 lines (32 loc) · 2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
import { setTimeout as delay } from 'node:timers/promises';
const inspect = promisify(execFile);
const kill = pid => { try { process.kill(pid, 'SIGKILL'); } catch (error) { if (error.code !== 'ESRCH') throw error; } };
/** Give each live parent a chance to reap killed children before killing it.
* A blocked parent or unavailable process inventory can leave cleanup uncertain;
* callers retain that gap on the original failure instead of claiming success. */
export async function terminateSubprocess(child, grouped) {
if (!child.pid) return;
if (!grouped) { child.kill('SIGKILL'); return; }
const deadline = performance.now() + 1000;
let gap;
try {
while (performance.now() < deadline) {
const { stdout } = await inspect('ps', ['-eo', 'pid=,ppid=,pgid='], { timeout: 200, maxBuffer: 1024 * 1024 });
const members = stdout.trim().split('\n').map(line => line.trim().split(/\s+/u).map(Number))
.filter(([pid, parent, group]) => Number.isSafeInteger(pid) && pid > 1 && Number.isSafeInteger(parent) && group === child.pid && pid !== child.pid);
if (members.length === 0) break;
const parents = new Set(members.map(([, parent]) => parent));
for (const [pid] of members) if (!parents.has(pid)) kill(pid);
// Do not promote an unreaped zombie to a terminated subtree. A fresh
// inventory must show removal before its parent becomes a leaf.
await delay(20);
}
if (performance.now() >= deadline) gap = new Error('Descendant reaping exceeded the cleanup grace period');
} catch (cause) { gap = new Error('Descendant inventory or termination failed', { cause }); }
// Also catches descendants forked after the last inventory. Escaped sessions
// and parents unable to reap are outside this bounded cleanup qualification.
try { kill(-child.pid); } catch (cause) { gap ??= new Error('Process group termination failed', { cause }); child.kill('SIGKILL'); }
if (gap) throw gap;
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.