next-issue 6.6.6 → 6.6.7

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.
package/README.md CHANGED
@@ -556,7 +556,10 @@ affect another. The worker:
556
556
  with that id started at another time, for example because the system gave
557
557
  the id of a worker that was killed or lost in a reboot to another process.
558
558
  A lock without a start time, from an older version, is stale too. A worker
559
- that cannot read its own start time exits with an error and takes no lock;
559
+ that cannot read its own start time exits with an error and takes no lock.
560
+ When several workers start at once, one takes the lock and the others exit
561
+ with `another worker is running`. A worker that stops removes the socket
562
+ and lock files only while it still holds the lock;
560
563
  - refuses a `start`, `stop` or `questions` message whose `root` is not
561
564
  the exact path of its repository with an `error` message, and starts or
562
565
  stops nothing;
package/dist/lock.mjs CHANGED
@@ -1,9 +1,11 @@
1
1
  import { execFile } from 'node:child_process';
2
- import { rm, writeFile } from 'node:fs/promises';
2
+ import { randomUUID } from 'node:crypto';
3
+ import { readFileSync, statSync } from 'node:fs';
4
+ import { link, rm, writeFile } from 'node:fs/promises';
3
5
  import { setTimeout as delay } from 'node:timers/promises';
4
6
  import { promisify } from 'node:util';
5
7
  import { hasCode } from './errors.mjs';
6
- import { readTextOrEmpty } from './files.mjs';
8
+ import { readTextIfPresent, readTextOrEmpty } from './files.mjs';
7
9
  const LOCK_POLL_MS = 100;
8
10
  const worktreeQueues = new Map();
9
11
  export function oneAtATime(root, work) {
@@ -26,8 +28,8 @@ export async function processStart(pid) {
26
28
  return undefined;
27
29
  }
28
30
  }
29
- async function holder(lockPath) {
30
- const [first = '', ...rest] = (await readTextOrEmpty(lockPath)).trim().split(' ');
31
+ async function holderOf(text) {
32
+ const [first = '', ...rest] = text.trim().split(' ');
31
33
  const pid = Number(first);
32
34
  const started = rest.join(' ');
33
35
  if (!(pid > 0) || started === '') {
@@ -35,6 +37,13 @@ async function holder(lockPath) {
35
37
  }
36
38
  return (await processStart(pid)) === started ? pid : undefined;
37
39
  }
40
+ async function holder(lockPath) {
41
+ return holderOf(await readTextOrEmpty(lockPath));
42
+ }
43
+ async function isStale(lockPath) {
44
+ const text = await readTextIfPresent(lockPath);
45
+ return text !== undefined && (await holderOf(text)) === undefined;
46
+ }
38
47
  export async function stopWorker(lockPath) {
39
48
  const pid = await holder(lockPath);
40
49
  if (pid !== undefined) {
@@ -42,33 +51,73 @@ export async function stopWorker(lockPath) {
42
51
  }
43
52
  return pid;
44
53
  }
54
+ function inode(path) {
55
+ return statSync(path, { throwIfNoEntry: false })?.ino;
56
+ }
57
+ function textOf(path) {
58
+ try {
59
+ return readFileSync(path, 'utf8');
60
+ }
61
+ catch {
62
+ return undefined;
63
+ }
64
+ }
65
+ async function linked(source, target) {
66
+ try {
67
+ await link(source, target);
68
+ return true;
69
+ }
70
+ catch (error) {
71
+ if (hasCode(error, 'EEXIST')) {
72
+ return false;
73
+ }
74
+ throw error;
75
+ }
76
+ }
77
+ async function removeStale(path, filled) {
78
+ const breakPath = `${path}.break`;
79
+ if (!(await linked(filled, breakPath))) {
80
+ if (await isStale(breakPath)) {
81
+ await rm(breakPath, { force: true });
82
+ }
83
+ return;
84
+ }
85
+ try {
86
+ if (await isStale(path)) {
87
+ await rm(path, { force: true });
88
+ }
89
+ }
90
+ finally {
91
+ await rm(breakPath, { force: true });
92
+ }
93
+ }
45
94
  export async function lock(path, waitMs, readStart = processStart) {
46
95
  const started = await readStart(process.pid);
47
96
  if (started === undefined) {
48
97
  throw new Error('next-issue: cannot read the start time of the worker process');
49
98
  }
50
- const deadline = Date.now() + waitMs;
51
- for (;;) {
52
- try {
53
- await writeFile(path, `${process.pid} ${started}\n`, {
54
- flag: 'wx',
55
- mode: 0o600,
56
- });
57
- return true;
58
- }
59
- catch (error) {
60
- if (!hasCode(error, 'EEXIST')) {
61
- throw error;
99
+ const filled = `${path}.${randomUUID()}`;
100
+ const text = `${process.pid} ${started}\n`;
101
+ await writeFile(filled, text, { flag: 'wx', mode: 0o600 });
102
+ try {
103
+ const deadline = Date.now() + waitMs;
104
+ for (;;) {
105
+ if (await linked(filled, path)) {
106
+ const held = inode(path);
107
+ return {
108
+ owns: () => held !== undefined && inode(path) === held && textOf(path) === text,
109
+ };
110
+ }
111
+ if ((await holder(path)) === undefined) {
112
+ await removeStale(path, filled);
113
+ }
114
+ else if (Date.now() >= deadline) {
115
+ return undefined;
62
116
  }
63
- }
64
- if ((await holder(path)) === undefined) {
65
- await rm(path, { force: true });
66
- }
67
- else if (Date.now() >= deadline) {
68
- return false;
69
- }
70
- else {
71
117
  await delay(LOCK_POLL_MS);
72
118
  }
73
119
  }
120
+ finally {
121
+ await rm(filled, { force: true });
122
+ }
74
123
  }
package/dist/serve.mjs CHANGED
@@ -1,11 +1,12 @@
1
1
  import { rmSync } from 'node:fs';
2
- import { mkdir, rm } from 'node:fs/promises';
2
+ import { mkdir, rename, rm } from 'node:fs/promises';
3
3
  import { createServer } from 'node:http';
4
- import { dirname } from 'node:path';
4
+ import { dirname, join } from 'node:path';
5
5
  import { WebSocketServer } from 'ws';
6
6
  import { parseStartArgs } from './args.mjs';
7
7
  import { stopChildren, withRunChildren } from './children.mjs';
8
8
  import { message } from './errors.mjs';
9
+ import { listOrEmpty } from './files.mjs';
9
10
  import { lock } from './lock.mjs';
10
11
  import { startProgress } from './progress.mjs';
11
12
  import { isNewer } from './protocol.mjs';
@@ -20,10 +21,17 @@ const IDLE_MS = 30_000;
20
21
  export async function startWorker(options) {
21
22
  checkSocketPath(options.socketPath);
22
23
  await mkdir(dirname(options.lockPath), { recursive: true });
23
- if (!(await lock(options.lockPath, options.lockWaitMs ?? LOCK_WAIT_MS))) {
24
+ const held = await lock(options.lockPath, options.lockWaitMs ?? LOCK_WAIT_MS);
25
+ if (held === undefined) {
24
26
  return undefined;
25
27
  }
26
- await rm(options.socketPath, { force: true });
28
+ const stateDir = dirname(options.socketPath);
29
+ const bindPath = join(stateDir, `s${process.pid}`);
30
+ for (const name of await listOrEmpty(stateDir)) {
31
+ if (/^s\d+$/.test(name)) {
32
+ await rm(join(stateDir, name), { force: true });
33
+ }
34
+ }
27
35
  const runs = new Map();
28
36
  let restart = false;
29
37
  const server = createServer();
@@ -33,7 +41,7 @@ export async function startWorker(options) {
33
41
  let idleTimer;
34
42
  const { closed, close } = closer(async () => {
35
43
  clearTimeout(idleTimer);
36
- await shutdown(server, sockets, options);
44
+ await shutdown(server, sockets, options, held);
37
45
  });
38
46
  const closeWhenIdle = () => {
39
47
  if ((restart || sockets.clients.size === 0) && !busy()) {
@@ -120,9 +128,10 @@ export async function startWorker(options) {
120
128
  }
121
129
  });
122
130
  });
123
- await listen(server, options.socketPath);
131
+ await listen(server, bindPath);
132
+ await rename(bindPath, options.socketPath);
124
133
  idleTimer = setTimeout(closeWhenIdle, options.idleMs ?? IDLE_MS);
125
- return { closed, close };
134
+ return { closed, close, lock: held };
126
135
  }
127
136
  export async function serve(root) {
128
137
  const paths = workerPaths(root);
@@ -140,8 +149,10 @@ export async function serve(root) {
140
149
  const end = (signal) => {
141
150
  process.off('SIGTERM', onTerminate);
142
151
  process.off('SIGINT', onInterrupt);
143
- rmSync(paths.socketPath, { force: true });
144
- rmSync(paths.lockPath, { force: true });
152
+ if (worker.lock.owns()) {
153
+ rmSync(paths.socketPath, { force: true });
154
+ rmSync(paths.lockPath, { force: true });
155
+ }
145
156
  stopChildren(signal);
146
157
  };
147
158
  const onTerminate = () => end('SIGTERM');
@@ -56,14 +56,16 @@ export function listen(server, path) {
56
56
  function whenClosed(close) {
57
57
  return new Promise((resolve) => close(() => resolve()));
58
58
  }
59
- export async function shutdown(server, sockets, paths) {
59
+ export async function shutdown(server, sockets, paths, held) {
60
60
  for (const socket of sockets.clients) {
61
61
  socket.terminate();
62
62
  }
63
63
  await whenClosed((done) => sockets.close(done));
64
64
  await whenClosed((done) => server.close(done));
65
- await rm(paths.socketPath, { force: true });
66
- await rm(paths.lockPath, { force: true });
65
+ if (held.owns()) {
66
+ await rm(paths.socketPath, { force: true });
67
+ await rm(paths.lockPath, { force: true });
68
+ }
67
69
  }
68
70
  export function closer(stop) {
69
71
  let finish = () => undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-issue",
3
- "version": "6.6.6",
3
+ "version": "6.6.7",
4
4
  "description": "Autonomous issue harness driven by the pi SDK",
5
5
  "keywords": [
6
6
  "github",