next-issue 6.6.3 → 6.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +10 -6
  2. package/dist/lock.mjs +29 -9
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -544,9 +544,13 @@ affect another. The worker:
544
544
  repository. Only your user can open the file. It works on macOS and Linux.
545
545
  When the path of the socket file is too long for the system, about 103
546
546
  bytes on macOS and 107 on Linux, it exits with an error that says so;
547
- - holds `.next-issue/worker.lock` in the repository, with its process id, so
548
- only one worker runs per repository. A lock whose process is no longer alive
549
- is taken over;
547
+ - holds `.next-issue/worker.lock` in the repository, with its process id and
548
+ the start time of that process, so only one worker runs per repository. A
549
+ lock is stale, and is taken over, when no process has that id or the process
550
+ with that id started at another time, for example because the system gave
551
+ the id of a worker that was killed or lost in a reboot to another process.
552
+ A lock without a start time, from an older version, is stale too. A worker
553
+ that cannot read its own start time exits with an error and takes no lock;
550
554
  - refuses a `start`, `stop` or `questions` message whose `root` is not
551
555
  the exact path of its repository with an `error` message, and starts or
552
556
  stops nothing;
@@ -595,9 +599,9 @@ affect another. The worker:
595
599
 
596
600
  Stop the worker of a repository with `next-issue stop` in that repository.
597
601
  Workers of other repositories carry on. This sends `SIGTERM` to the process in
598
- the lock file, so the worker stops at once and a run that is going resumes from
599
- its saved state next time. When no worker is running, the command says so and
600
- exits with 0.
602
+ the lock file when the lock is not stale, so the worker stops at once and a
603
+ run that is going resumes from its saved state next time. When no worker is
604
+ running, the command says so and exits with 0.
601
605
 
602
606
  Versions before one worker per repository ran one worker for the machine,
603
607
  with `next-issue.sock`, `next-issue.lock` and `next-issue.roots.json` in
package/dist/lock.mjs CHANGED
@@ -1,5 +1,7 @@
1
+ import { execFile } from 'node:child_process';
1
2
  import { rm, writeFile } from 'node:fs/promises';
2
3
  import { setTimeout as delay } from 'node:timers/promises';
4
+ import { promisify } from 'node:util';
3
5
  import { hasCode } from './errors.mjs';
4
6
  import { readTextOrEmpty } from './files.mjs';
5
7
  const LOCK_POLL_MS = 100;
@@ -9,18 +11,29 @@ export function oneAtATime(root, work) {
9
11
  worktreeQueues.set(root, result.catch(() => undefined));
10
12
  return result;
11
13
  }
12
- function alive(pid) {
14
+ export async function processStart(pid) {
15
+ const stat = await readTextOrEmpty(`/proc/${pid}/stat`);
16
+ if (stat !== '') {
17
+ return stat.slice(stat.lastIndexOf(')') + 2).split(' ')[19];
18
+ }
13
19
  try {
14
- process.kill(pid, 0);
15
- return true;
20
+ const { stdout } = await promisify(execFile)('ps', ['-o', 'lstart=', '-p', String(pid)], {
21
+ env: { ...process.env, LC_ALL: 'C' },
22
+ });
23
+ return stdout.trim() || undefined;
16
24
  }
17
- catch (error) {
18
- return hasCode(error, 'EPERM');
25
+ catch {
26
+ return undefined;
19
27
  }
20
28
  }
21
29
  async function holder(lockPath) {
22
- const pid = Number((await readTextOrEmpty(lockPath)).trim());
23
- return pid > 0 && alive(pid) ? pid : undefined;
30
+ const [first = '', ...rest] = (await readTextOrEmpty(lockPath)).trim().split(' ');
31
+ const pid = Number(first);
32
+ const started = rest.join(' ');
33
+ if (!(pid > 0) || started === '') {
34
+ return undefined;
35
+ }
36
+ return (await processStart(pid)) === started ? pid : undefined;
24
37
  }
25
38
  export async function stopWorker(lockPath) {
26
39
  const pid = await holder(lockPath);
@@ -29,11 +42,18 @@ export async function stopWorker(lockPath) {
29
42
  }
30
43
  return pid;
31
44
  }
32
- export async function lock(path, waitMs) {
45
+ export async function lock(path, waitMs, readStart = processStart) {
46
+ const started = await readStart(process.pid);
47
+ if (started === undefined) {
48
+ throw new Error('next-issue: cannot read the start time of the worker process');
49
+ }
33
50
  const deadline = Date.now() + waitMs;
34
51
  for (;;) {
35
52
  try {
36
- await writeFile(path, `${process.pid}\n`, { flag: 'wx', mode: 0o600 });
53
+ await writeFile(path, `${process.pid} ${started}\n`, {
54
+ flag: 'wx',
55
+ mode: 0o600,
56
+ });
37
57
  return true;
38
58
  }
39
59
  catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-issue",
3
- "version": "6.6.3",
3
+ "version": "6.6.5",
4
4
  "description": "Autonomous issue harness driven by the pi SDK",
5
5
  "keywords": [
6
6
  "github",