@peerbit/program 6.0.35 → 6.0.37
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/dist/src/handler.d.ts +146 -1
- package/dist/src/handler.d.ts.map +1 -1
- package/dist/src/handler.js +1726 -78
- package/dist/src/handler.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/program.d.ts +40 -1
- package/dist/src/program.d.ts.map +1 -1
- package/dist/src/program.js +521 -50
- package/dist/src/program.js.map +1 -1
- package/package.json +13 -13
- package/src/handler.ts +2481 -82
- package/src/index.ts +1 -0
- package/src/program.ts +697 -59
package/dist/src/handler.js
CHANGED
|
@@ -2,7 +2,36 @@ import {} from "@peerbit/blocks-interface";
|
|
|
2
2
|
import { logger as loggerFn } from "@peerbit/logger";
|
|
3
3
|
import PQueue from "p-queue";
|
|
4
4
|
export const logger = loggerFn("peerbit:program:handler");
|
|
5
|
+
export const TERMINAL_BASE_CHECKPOINT = Symbol.for("@peerbit/program/terminal-base-checkpoint");
|
|
6
|
+
export const TERMINAL_BASE_COMMIT = Symbol.for("@peerbit/program/terminal-base-commit");
|
|
7
|
+
export const TERMINAL_BASE_RETRY = Symbol.for("@peerbit/program/terminal-base-retry");
|
|
8
|
+
export const TERMINAL_OUTER_CLEANUP_RETAIN = Symbol.for("@peerbit/program/terminal-outer-cleanup-retain");
|
|
9
|
+
export const TERMINAL_OUTER_CLEANUP_RELEASE = Symbol.for("@peerbit/program/terminal-outer-cleanup-release");
|
|
10
|
+
const TERMINAL_OPERATION_NOT_STARTED = Symbol.for("@peerbit/program/terminal-operation-not-started-error");
|
|
11
|
+
/**
|
|
12
|
+
* A terminal-operation precondition rejected the call before any lifecycle or
|
|
13
|
+
* resource mutation began. Handler leaves the program retryable instead of
|
|
14
|
+
* retaining this as partially completed cleanup.
|
|
15
|
+
*/
|
|
16
|
+
export class TerminalOperationNotStartedError extends Error {
|
|
17
|
+
[TERMINAL_OPERATION_NOT_STARTED] = true;
|
|
18
|
+
constructor(message) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = "TerminalOperationNotStartedError";
|
|
21
|
+
}
|
|
22
|
+
static [Symbol.hasInstance](instance) {
|
|
23
|
+
return Boolean(instance &&
|
|
24
|
+
typeof instance === "object" &&
|
|
25
|
+
instance[TERMINAL_OPERATION_NOT_STARTED] === true);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const assertCanAttachParent = (child) => {
|
|
29
|
+
if (child.acceptsParentAttachments === false) {
|
|
30
|
+
throw new Error("Program is terminating and cannot accept another parent");
|
|
31
|
+
}
|
|
32
|
+
};
|
|
5
33
|
export const addParent = (child, parent) => {
|
|
34
|
+
assertCanAttachParent(child);
|
|
6
35
|
if (child.parents && child.parents.includes(parent) && parent == null) {
|
|
7
36
|
return; // prevent root parents to exist multiple times. This will allow use to close a program onces even if it is reused multiple times
|
|
8
37
|
}
|
|
@@ -11,22 +40,840 @@ export const addParent = (child, parent) => {
|
|
|
11
40
|
(parent.children || (parent.children = [])).push(child);
|
|
12
41
|
}
|
|
13
42
|
};
|
|
43
|
+
/**
|
|
44
|
+
* Owns lifecycle state for one client. Handler instances do not share address
|
|
45
|
+
* reservations, so a client must use a single Handler for a program graph.
|
|
46
|
+
*/
|
|
14
47
|
export class Handler {
|
|
15
48
|
properties;
|
|
16
49
|
items;
|
|
17
50
|
_openQueue;
|
|
18
51
|
_openingPromises;
|
|
52
|
+
_openingReservations;
|
|
53
|
+
_openingReservationsByPromise;
|
|
54
|
+
_openingReservationsByAddress;
|
|
55
|
+
_parentAttachmentReservations;
|
|
56
|
+
_openAdmissions;
|
|
57
|
+
_openLifecycleCallbacks;
|
|
58
|
+
_terminalLifecycleCallbacks;
|
|
59
|
+
_lifecycleMethodInvocations;
|
|
60
|
+
_acceptingOpens;
|
|
61
|
+
_stopPromise;
|
|
62
|
+
_lifecycleState;
|
|
63
|
+
_failedInitializations;
|
|
64
|
+
_cleanupResiduals;
|
|
65
|
+
_terminalOperationsByProgram;
|
|
66
|
+
_terminalOperationsByAddress;
|
|
67
|
+
_initializationRollbacks;
|
|
68
|
+
_replacementClosures;
|
|
19
69
|
constructor(properties) {
|
|
20
70
|
this.properties = properties;
|
|
21
71
|
this._openQueue = new Map();
|
|
22
72
|
this._openingPromises = new Map();
|
|
73
|
+
this._openingReservations = new Set();
|
|
74
|
+
this._openingReservationsByPromise = new WeakMap();
|
|
75
|
+
this._openingReservationsByAddress = new Map();
|
|
76
|
+
this._parentAttachmentReservations = new Map();
|
|
77
|
+
this._openAdmissions = new Set();
|
|
78
|
+
this._openLifecycleCallbacks = 0;
|
|
79
|
+
this._terminalLifecycleCallbacks = 0;
|
|
80
|
+
this._lifecycleMethodInvocations = 0;
|
|
81
|
+
this._acceptingOpens = true;
|
|
82
|
+
this._lifecycleState = "running";
|
|
83
|
+
this._failedInitializations = new Map();
|
|
84
|
+
this._cleanupResiduals = new Map();
|
|
85
|
+
this._terminalOperationsByProgram = new WeakMap();
|
|
86
|
+
this._terminalOperationsByAddress = new Map();
|
|
87
|
+
this._initializationRollbacks = new WeakSet();
|
|
88
|
+
this._replacementClosures = new WeakSet();
|
|
23
89
|
this.items = new Map();
|
|
24
90
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Re-open admissions after the owning client has finished starting all of the
|
|
93
|
+
* services programs depend on. This is an internal client lifecycle hook, not
|
|
94
|
+
* a substitute for starting the client itself.
|
|
95
|
+
*
|
|
96
|
+
* This is called by the owning client after its services have started.
|
|
97
|
+
*/
|
|
98
|
+
start() {
|
|
99
|
+
if (this._lifecycleState === "running") {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
this.assertCanStart();
|
|
103
|
+
this._lifecycleState = "running";
|
|
104
|
+
this._acceptingOpens = true;
|
|
105
|
+
}
|
|
106
|
+
/** Validate restart eligibility without opening admissions. */
|
|
107
|
+
assertCanStart() {
|
|
108
|
+
if (this._lifecycleState === "running") {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (this._lifecycleState !== "stopped" ||
|
|
112
|
+
this._stopPromise ||
|
|
113
|
+
this.items.size > 0 ||
|
|
114
|
+
this._openingPromises.size > 0 ||
|
|
115
|
+
this._openingReservations.size > 0 ||
|
|
116
|
+
this._openingReservationsByAddress.size > 0 ||
|
|
117
|
+
this._parentAttachmentReservations.size > 0 ||
|
|
118
|
+
this._openAdmissions.size > 0 ||
|
|
119
|
+
this._failedInitializations.size > 0 ||
|
|
120
|
+
this._cleanupResiduals.size > 0 ||
|
|
121
|
+
this._terminalOperationsByAddress.size > 0) {
|
|
122
|
+
throw new Error("Program handler cannot start before a successful stop has fully drained");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Stop the handler after admitted lifecycle work drains.
|
|
127
|
+
*
|
|
128
|
+
* Lifecycle overrides and callbacks must not await their own handler/client
|
|
129
|
+
* stop. Immediate reentry is rejected. After user code has yielded, portable
|
|
130
|
+
* JavaScript runtimes provide no reliable async-call provenance with which to
|
|
131
|
+
* distinguish self-reentry from an unrelated external stop; schedule stop from
|
|
132
|
+
* the lifecycle owner instead. A conservatively rejected external stop remains
|
|
133
|
+
* retryable after the active callback finishes.
|
|
134
|
+
*/
|
|
135
|
+
stop() {
|
|
136
|
+
if (this._openLifecycleCallbacks > 0 ||
|
|
137
|
+
this._terminalLifecycleCallbacks > 0 ||
|
|
138
|
+
this._lifecycleMethodInvocations > 0 ||
|
|
139
|
+
[...this.items.values()].some((program) => program.terminalLifecycleCallbackRunning === true)) {
|
|
140
|
+
return Promise.reject(new TerminalOperationNotStartedError("Program lifecycle callbacks cannot wait for their own handler to stop"));
|
|
141
|
+
}
|
|
142
|
+
if (this._stopPromise) {
|
|
143
|
+
return this._stopPromise;
|
|
144
|
+
}
|
|
145
|
+
if (this._lifecycleState === "stopped") {
|
|
146
|
+
return Promise.resolve();
|
|
147
|
+
}
|
|
148
|
+
// Close admissions synchronously. Every open that passed this fence has a
|
|
149
|
+
// completion promise in `_openAdmissions`, so stop has a stable set to drain.
|
|
150
|
+
this._acceptingOpens = false;
|
|
151
|
+
this._lifecycleState = "stopping";
|
|
152
|
+
this._stopPromise = this.stopOnce().then(() => {
|
|
153
|
+
// The client still has storage, index, and transport teardown to perform.
|
|
154
|
+
// Only its completed start() transition may re-open admissions.
|
|
155
|
+
this._lifecycleState = "stopped";
|
|
156
|
+
this._stopPromise = undefined;
|
|
157
|
+
}, (error) => {
|
|
158
|
+
// Keep admissions closed after a partial stop. Clearing only the promise
|
|
159
|
+
// makes stop() retryable without exposing the live residual items to opens.
|
|
160
|
+
this._lifecycleState = "failed";
|
|
161
|
+
this._stopPromise = undefined;
|
|
162
|
+
throw error;
|
|
163
|
+
});
|
|
164
|
+
return this._stopPromise;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Async methods execute synchronously until their first await. Fence that
|
|
168
|
+
* invocation window so a lifecycle override cannot make stop wait on the very
|
|
169
|
+
* operation that is awaiting stop. Awaited user callbacks have their own wider
|
|
170
|
+
* callback fences below.
|
|
171
|
+
*/
|
|
172
|
+
invokeLifecycleMethod(operation) {
|
|
173
|
+
this._lifecycleMethodInvocations += 1;
|
|
174
|
+
try {
|
|
175
|
+
return Promise.resolve(operation());
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
this._lifecycleMethodInvocations -= 1;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Program.end() can only observe the base implementation's lifetime. A
|
|
183
|
+
* subclass is still free to do asynchronous cleanup after `super.close()` or
|
|
184
|
+
* `super.drop()` has returned. Wrap the public, dynamically-dispatched method
|
|
185
|
+
* on every managed instance so Handler state covers that full outer promise.
|
|
186
|
+
*/
|
|
187
|
+
monitorTerminalOperations(address, program) {
|
|
188
|
+
let state = this._terminalOperationsByProgram.get(program);
|
|
189
|
+
const newLifecycle = !state || this._terminalOperationsByAddress.get(address) !== state;
|
|
190
|
+
if (!state) {
|
|
191
|
+
state = {
|
|
192
|
+
program,
|
|
193
|
+
address,
|
|
194
|
+
activeCalls: 0,
|
|
195
|
+
pending: new Set(),
|
|
196
|
+
failed: false,
|
|
197
|
+
terminalCompleted: false,
|
|
198
|
+
outerTail: Promise.resolve(),
|
|
199
|
+
outerCalls: new Set(),
|
|
200
|
+
};
|
|
201
|
+
this._terminalOperationsByProgram.set(program, state);
|
|
202
|
+
}
|
|
203
|
+
state.address = address;
|
|
204
|
+
if (newLifecycle) {
|
|
205
|
+
state.failed = false;
|
|
206
|
+
state.failedOperation = undefined;
|
|
207
|
+
state.failedCall = undefined;
|
|
208
|
+
state.terminalCompleted = false;
|
|
209
|
+
}
|
|
210
|
+
this._terminalOperationsByAddress.set(address, state);
|
|
211
|
+
this.refreshTerminalOperationWrappers(state);
|
|
212
|
+
return { state, newLifecycle };
|
|
213
|
+
}
|
|
214
|
+
refreshTerminalOperationWrappers(state) {
|
|
215
|
+
const program = state.program;
|
|
216
|
+
if (program.close !== state.closeWrapper) {
|
|
217
|
+
const close = program.close;
|
|
218
|
+
state.closeOperation = close;
|
|
219
|
+
const closeWrapper = (...args) => this.runTerminalOperation(state, "close", close, args, closeWrapper);
|
|
220
|
+
state.closeWrapper = closeWrapper;
|
|
221
|
+
program.close = closeWrapper;
|
|
222
|
+
}
|
|
223
|
+
const droppable = program;
|
|
224
|
+
if (typeof droppable.drop === "function" &&
|
|
225
|
+
droppable.drop !== state.dropWrapper) {
|
|
226
|
+
const drop = droppable.drop;
|
|
227
|
+
state.dropOperation = drop;
|
|
228
|
+
const dropWrapper = (...args) => this.runTerminalOperation(state, "drop", drop, args, dropWrapper);
|
|
229
|
+
state.dropWrapper = dropWrapper;
|
|
230
|
+
droppable.drop = dropWrapper;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
runTerminalOperation(state, type, operation, args, invokedWrapper) {
|
|
234
|
+
const from = args[0];
|
|
235
|
+
const synchronousInvocation = state.synchronouslyInvokingWrapper;
|
|
236
|
+
if (synchronousInvocation) {
|
|
237
|
+
if (type !== synchronousInvocation.type) {
|
|
238
|
+
return Promise.reject(new TerminalOperationNotStartedError("Program terminal methods cannot synchronously invoke another terminal operation"));
|
|
239
|
+
}
|
|
240
|
+
if (!this.sameTerminalArgs(args, synchronousInvocation.args)) {
|
|
241
|
+
return Promise.reject(new TerminalOperationNotStartedError("A captured terminal wrapper cannot change the active operation owner"));
|
|
242
|
+
}
|
|
243
|
+
if (invokedWrapper !== synchronousInvocation.wrapper) {
|
|
244
|
+
// A post-open replacement may delegate to the Handler wrapper it
|
|
245
|
+
// captured before being re-wrapped. Peel that stale wrapper back to its
|
|
246
|
+
// raw operation; the current outer wrapper still monitors the full promise.
|
|
247
|
+
try {
|
|
248
|
+
return Promise.resolve(operation.apply(state.program, args));
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
return Promise.reject(error);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return Promise.reject(new TerminalOperationNotStartedError("Program terminal methods cannot wait for their own active operation"));
|
|
255
|
+
}
|
|
256
|
+
if (state.program.terminalLifecycleCallbackRunning ||
|
|
257
|
+
(this._terminalLifecycleCallbacks > 0 && state.activeCalls > 0)) {
|
|
258
|
+
return Promise.reject(new TerminalOperationNotStartedError("Program lifecycle callbacks cannot wait for their own terminal operation"));
|
|
259
|
+
}
|
|
260
|
+
const parentIndex = state.program.parents?.findIndex((parent) => parent === from) ?? -1;
|
|
261
|
+
const terminal = state.program.closed ||
|
|
262
|
+
parentIndex === -1 ||
|
|
263
|
+
(state.program.parents?.length ?? 0) === 1;
|
|
264
|
+
const currentWrapper = type === "close" ? state.closeWrapper : state.dropWrapper;
|
|
265
|
+
if (currentWrapper &&
|
|
266
|
+
invokedWrapper !== currentWrapper &&
|
|
267
|
+
[...state.outerCalls].some((call) => call.type === type && call.invokedWrapper === currentWrapper)) {
|
|
268
|
+
// Once replacement code has yielded there is no portable async-call
|
|
269
|
+
// context that can distinguish its captured stale wrapper from an
|
|
270
|
+
// unrelated external stale call. Never return the active outer promise to
|
|
271
|
+
// code that may itself be awaiting this call: reject before base progress
|
|
272
|
+
// instead of forming a self-cycle that wedges Handler.stop().
|
|
273
|
+
return Promise.reject(new TerminalOperationNotStartedError("A stale terminal wrapper cannot join its active replacement after yielding"));
|
|
274
|
+
}
|
|
275
|
+
const matching = [...state.outerCalls].find((call) => call.terminal &&
|
|
276
|
+
call.from === from &&
|
|
277
|
+
(call.type === type || (type === "close" && call.type === "drop")));
|
|
278
|
+
if (matching) {
|
|
279
|
+
return matching.promise;
|
|
280
|
+
}
|
|
281
|
+
try {
|
|
282
|
+
this.assertTerminalGraphCanStart(state.program);
|
|
283
|
+
}
|
|
284
|
+
catch (error) {
|
|
285
|
+
return Promise.reject(error);
|
|
286
|
+
}
|
|
287
|
+
// The outer wrapper serializes subclass methods before Program sees them. A
|
|
288
|
+
// call admitted while another owner is still queued can therefore look
|
|
289
|
+
// non-terminal now even though it will be the final release when it runs.
|
|
290
|
+
// Count already-admitted compatible calls against the owner's current
|
|
291
|
+
// references so a duplicate of that future terminal call shares its exact
|
|
292
|
+
// promise. Keep one distinct call per real duplicate ownership reference.
|
|
293
|
+
const compatibleCalls = [...state.outerCalls].filter((call) => call.from === from &&
|
|
294
|
+
(call.type === type || (type === "close" && call.type === "drop")));
|
|
295
|
+
const ownerReferences = state.program.parents?.filter((parent) => parent === from).length ?? 0;
|
|
296
|
+
const reservedOwnerReferences = compatibleCalls.filter((call) => call.claimedOwnerReference).length;
|
|
297
|
+
const synchronouslyReleasedOwnerReferences = compatibleCalls.reduce((released, call) => {
|
|
298
|
+
const before = call.ownerReferencesBeforeInvoke;
|
|
299
|
+
const after = call.ownerReferencesAfterInvoke;
|
|
300
|
+
return (released +
|
|
301
|
+
(before == null || after == null
|
|
302
|
+
? 0
|
|
303
|
+
: Math.min(1, Math.max(0, before - after))));
|
|
304
|
+
}, 0);
|
|
305
|
+
const unclaimedOwnerReferences = ownerReferences -
|
|
306
|
+
(reservedOwnerReferences - synchronouslyReleasedOwnerReferences);
|
|
307
|
+
if (compatibleCalls.length > 0 && unclaimedOwnerReferences <= 0) {
|
|
308
|
+
return compatibleCalls.at(-1).promise;
|
|
309
|
+
}
|
|
310
|
+
let resolve;
|
|
311
|
+
let reject;
|
|
312
|
+
const tracked = new Promise((promiseResolve, promiseReject) => {
|
|
313
|
+
resolve = promiseResolve;
|
|
314
|
+
reject = promiseReject;
|
|
315
|
+
});
|
|
316
|
+
const outerCall = {
|
|
317
|
+
type,
|
|
318
|
+
from,
|
|
319
|
+
terminal,
|
|
320
|
+
claimedOwnerReference: ownerReferences > 0,
|
|
321
|
+
invokedWrapper,
|
|
322
|
+
promise: tracked,
|
|
323
|
+
};
|
|
324
|
+
const hasPredecessor = state.outerCalls.size > 0;
|
|
325
|
+
state.outerCalls.add(outerCall);
|
|
326
|
+
state.activeCalls += 1;
|
|
327
|
+
state.terminalCompleted = false;
|
|
328
|
+
this._terminalOperationsByAddress.set(state.address, state);
|
|
329
|
+
state.pending.add(tracked);
|
|
330
|
+
const predecessor = state.outerTail;
|
|
331
|
+
const execute = async () => {
|
|
332
|
+
const recovering = state.failed;
|
|
333
|
+
const failedCall = state.failedCall;
|
|
334
|
+
if (recovering &&
|
|
335
|
+
(failedCall?.type !== type ||
|
|
336
|
+
!this.sameTerminalArgs(failedCall.args, args))) {
|
|
337
|
+
throw new Error(`Program at ${state.address} has failed terminal cleanup that must be retried first`);
|
|
338
|
+
}
|
|
339
|
+
const startedClosed = state.program.closed;
|
|
340
|
+
const startedAcceptingParents = state.program.acceptsParentAttachments;
|
|
341
|
+
const startedParents = [...(state.program.parents ?? [])];
|
|
342
|
+
const checkpoint = this.terminalCheckpoint(state.program);
|
|
343
|
+
const existingCleanupLease = state.cleanupLease;
|
|
344
|
+
if (!state.cleanupLease) {
|
|
345
|
+
state.cleanupLease = state.program[TERMINAL_OUTER_CLEANUP_RETAIN]?.();
|
|
346
|
+
}
|
|
347
|
+
const releaseProvisionalCleanupLease = () => {
|
|
348
|
+
if (!existingCleanupLease && state.cleanupLease) {
|
|
349
|
+
state.program[TERMINAL_OUTER_CLEANUP_RELEASE]?.(state.cleanupLease);
|
|
350
|
+
state.cleanupLease = undefined;
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
try {
|
|
354
|
+
const invoke = () => {
|
|
355
|
+
outerCall.ownerReferencesBeforeInvoke =
|
|
356
|
+
state.program.parents?.filter((parent) => parent === from).length ??
|
|
357
|
+
0;
|
|
358
|
+
let operationResult;
|
|
359
|
+
const previousSynchronouslyInvokingWrapper = state.synchronouslyInvokingWrapper;
|
|
360
|
+
state.synchronouslyInvokingWrapper = {
|
|
361
|
+
type,
|
|
362
|
+
wrapper: invokedWrapper,
|
|
363
|
+
args,
|
|
364
|
+
};
|
|
365
|
+
try {
|
|
366
|
+
operationResult = this.invokeLifecycleMethod(() => operation.apply(state.program, args));
|
|
367
|
+
}
|
|
368
|
+
finally {
|
|
369
|
+
state.synchronouslyInvokingWrapper =
|
|
370
|
+
previousSynchronouslyInvokingWrapper;
|
|
371
|
+
outerCall.ownerReferencesAfterInvoke =
|
|
372
|
+
state.program.parents?.filter((parent) => parent === from)
|
|
373
|
+
.length ?? 0;
|
|
374
|
+
}
|
|
375
|
+
return operationResult;
|
|
376
|
+
};
|
|
377
|
+
const result = recovering && failedCall?.commit
|
|
378
|
+
? await this.retryCommittedTerminalCall(state.program, failedCall.commit, invoke)
|
|
379
|
+
: await invoke();
|
|
380
|
+
if (recovering) {
|
|
381
|
+
const recoveryOwner = failedCall?.args[0];
|
|
382
|
+
const releasedDuringRetry = Math.max(0, startedParents.filter((parent) => parent === recoveryOwner).length -
|
|
383
|
+
(state.program.parents?.filter((parent) => parent === recoveryOwner).length ?? 0));
|
|
384
|
+
this.removeReleasedParentChildReferences(state.program, recoveryOwner, Math.max(failedCall?.commit?.releasedParentReferences ??
|
|
385
|
+
failedCall?.releasedParentReferences ??
|
|
386
|
+
0, releasedDuringRetry));
|
|
387
|
+
state.failed = false;
|
|
388
|
+
state.failedOperation = undefined;
|
|
389
|
+
state.failedCall = undefined;
|
|
390
|
+
this.releaseRetainedTerminalIdentity(state);
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
const commit = this.terminalCommit(state.program, checkpoint, type, from);
|
|
394
|
+
this.removeReleasedParentChildReferences(state.program, from, commit?.releasedParentReferences ?? 0);
|
|
395
|
+
}
|
|
396
|
+
if (result && state.program.closed && !state.failed) {
|
|
397
|
+
state.terminalCompleted = true;
|
|
398
|
+
}
|
|
399
|
+
if (state.cleanupLease) {
|
|
400
|
+
state.program[TERMINAL_OUTER_CLEANUP_RELEASE]?.(state.cleanupLease);
|
|
401
|
+
state.cleanupLease = undefined;
|
|
402
|
+
}
|
|
403
|
+
return result;
|
|
404
|
+
}
|
|
405
|
+
catch (error) {
|
|
406
|
+
const observedCommit = failedCall?.commit ||
|
|
407
|
+
this.terminalCommit(state.program, checkpoint, type, from);
|
|
408
|
+
if (!recovering &&
|
|
409
|
+
error instanceof TerminalOperationNotStartedError &&
|
|
410
|
+
!observedCommit) {
|
|
411
|
+
// Exclude this call's provisional lease while validating that the
|
|
412
|
+
// explicit precondition error made no lifecycle mutation.
|
|
413
|
+
releaseProvisionalCleanupLease();
|
|
414
|
+
if (state.program.closed === startedClosed &&
|
|
415
|
+
state.program.acceptsParentAttachments ===
|
|
416
|
+
startedAcceptingParents &&
|
|
417
|
+
this.sameParents(state.program.parents ?? [], startedParents)) {
|
|
418
|
+
state.terminalCompleted = startedClosed;
|
|
419
|
+
throw error;
|
|
420
|
+
}
|
|
421
|
+
state.cleanupLease = state.program[TERMINAL_OUTER_CLEANUP_RETAIN]?.();
|
|
422
|
+
}
|
|
423
|
+
if (startedClosed && !recovering) {
|
|
424
|
+
// A fresh drop of an already-cleanly-closed program, or a drop queued
|
|
425
|
+
// behind a winning close, is an API rejection rather than failed cleanup.
|
|
426
|
+
state.terminalCompleted = true;
|
|
427
|
+
releaseProvisionalCleanupLease();
|
|
428
|
+
throw error;
|
|
429
|
+
}
|
|
430
|
+
const commit = observedCommit;
|
|
431
|
+
const baseProgressed = commit != null ||
|
|
432
|
+
state.program.closed !== startedClosed ||
|
|
433
|
+
!this.sameParents(state.program.parents ?? [], startedParents);
|
|
434
|
+
const recoveryType = type === "drop" && !baseProgressed
|
|
435
|
+
? "drop"
|
|
436
|
+
: commit || state.program.closed
|
|
437
|
+
? type
|
|
438
|
+
: "close";
|
|
439
|
+
const releasedParentReferences = Math.max(failedCall?.releasedParentReferences ?? 0, startedParents.filter((parent) => parent === from).length -
|
|
440
|
+
(state.program.parents?.filter((parent) => parent === from)
|
|
441
|
+
.length ?? 0));
|
|
442
|
+
state.failed = true;
|
|
443
|
+
state.failedOperation = recoveryType;
|
|
444
|
+
state.failedCall = {
|
|
445
|
+
type: recoveryType,
|
|
446
|
+
args: [...args],
|
|
447
|
+
commit: recoveryType === type ? commit : undefined,
|
|
448
|
+
releasedParentReferences,
|
|
449
|
+
};
|
|
450
|
+
state.terminalCompleted = false;
|
|
451
|
+
const monitored = this.items.get(state.address);
|
|
452
|
+
if (!monitored || monitored === state.program) {
|
|
453
|
+
this.items.set(state.address, state.program);
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
// `items` is address-keyed, but embedded opening graphs may contain a
|
|
457
|
+
// distinct instance with the same address as a monitored root. Never
|
|
458
|
+
// orphan that root by replacing it with this failed cleanup identity.
|
|
459
|
+
const residual = this.reserveCleanupResidual(state.program);
|
|
460
|
+
residual.terminalState = state;
|
|
461
|
+
}
|
|
462
|
+
this._terminalOperationsByAddress.set(state.address, state);
|
|
463
|
+
throw error;
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
// Invoke the first outer method immediately. Program.end() and subclasses
|
|
467
|
+
// establish their mutation/attachment fences synchronously before their
|
|
468
|
+
// first await; deferring through Promise.then would admit same-turn writes
|
|
469
|
+
// after close/drop had already been requested. Only true followers use the
|
|
470
|
+
// serialized promise tail.
|
|
471
|
+
const execution = hasPredecessor
|
|
472
|
+
? predecessor.then(execute, execute)
|
|
473
|
+
: execute();
|
|
474
|
+
state.outerTail = tracked.then(() => undefined, () => undefined);
|
|
475
|
+
void execution.then(resolve, reject);
|
|
476
|
+
void tracked.then(() => this.finishOuterTerminalCall(state, outerCall, tracked), () => this.finishOuterTerminalCall(state, outerCall, tracked));
|
|
477
|
+
logger.trace(`Tracking outer ${type} operation for program at ${state.address}`);
|
|
478
|
+
return tracked;
|
|
479
|
+
}
|
|
480
|
+
finishOuterTerminalCall(state, call, promise) {
|
|
481
|
+
state.activeCalls -= 1;
|
|
482
|
+
state.outerCalls.delete(call);
|
|
483
|
+
state.pending.delete(promise);
|
|
484
|
+
this.finishTerminalOperation(state);
|
|
485
|
+
}
|
|
486
|
+
sameTerminalArgs(left, right) {
|
|
487
|
+
return (left.length === right.length &&
|
|
488
|
+
left.every((value, index) => value === right[index]));
|
|
489
|
+
}
|
|
490
|
+
sameParents(left, right) {
|
|
491
|
+
return (left.length === right.length &&
|
|
492
|
+
left.every((parent, index) => parent === right[index]));
|
|
493
|
+
}
|
|
494
|
+
terminalCheckpoint(program) {
|
|
495
|
+
return program[TERMINAL_BASE_CHECKPOINT]?.() ?? 0;
|
|
496
|
+
}
|
|
497
|
+
terminalCommit(program, checkpoint, type, from) {
|
|
498
|
+
return program[TERMINAL_BASE_COMMIT]?.(checkpoint, type, from);
|
|
499
|
+
}
|
|
500
|
+
retryCommittedTerminalCall(program, commit, operation) {
|
|
501
|
+
return program[TERMINAL_BASE_RETRY]
|
|
502
|
+
? program[TERMINAL_BASE_RETRY](commit, operation)
|
|
503
|
+
: operation();
|
|
504
|
+
}
|
|
505
|
+
addCleanupResidual(program, failure, cleanupLease) {
|
|
506
|
+
const residual = this.reserveCleanupResidual(program, cleanupLease);
|
|
507
|
+
if (cleanupLease &&
|
|
508
|
+
residual.cleanupLease &&
|
|
509
|
+
cleanupLease !== residual.cleanupLease) {
|
|
510
|
+
program[TERMINAL_OUTER_CLEANUP_RELEASE]?.(cleanupLease);
|
|
511
|
+
}
|
|
512
|
+
residual.failures.push(failure);
|
|
513
|
+
}
|
|
514
|
+
reserveCleanupResidual(program, cleanupLease) {
|
|
515
|
+
const existing = this._cleanupResiduals.get(program);
|
|
516
|
+
if (existing) {
|
|
517
|
+
return existing;
|
|
518
|
+
}
|
|
519
|
+
const retainedLease = cleanupLease ?? program[TERMINAL_OUTER_CLEANUP_RETAIN]?.();
|
|
520
|
+
const residual = {
|
|
521
|
+
program,
|
|
522
|
+
failures: [],
|
|
523
|
+
cleanupLease: retainedLease,
|
|
524
|
+
activeReservations: 0,
|
|
525
|
+
};
|
|
526
|
+
this._cleanupResiduals.set(program, residual);
|
|
527
|
+
return residual;
|
|
528
|
+
}
|
|
529
|
+
acquireCleanupReservation(program) {
|
|
530
|
+
const residual = this.reserveCleanupResidual(program);
|
|
531
|
+
residual.activeReservations += 1;
|
|
532
|
+
return residual;
|
|
533
|
+
}
|
|
534
|
+
releaseCleanupReservation(residual) {
|
|
535
|
+
residual.activeReservations -= 1;
|
|
536
|
+
if (residual.activeReservations < 0) {
|
|
537
|
+
throw new Error("Cleanup residual reservation underflow");
|
|
538
|
+
}
|
|
539
|
+
if (residual.activeReservations === 0 &&
|
|
540
|
+
residual.failures.length === 0 &&
|
|
541
|
+
residual.terminalState == null) {
|
|
542
|
+
this.releaseCleanupResidual(residual);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
releaseCleanupResidual(residual) {
|
|
546
|
+
if (residual.activeReservations > 0) {
|
|
547
|
+
throw new Error("Cleanup residual cannot be released while rollback cleanup is active");
|
|
548
|
+
}
|
|
549
|
+
if (residual.terminalState != null) {
|
|
550
|
+
throw new Error("Cleanup residual cannot be released while terminal identity is retained");
|
|
551
|
+
}
|
|
552
|
+
if (residual.cleanupLease) {
|
|
553
|
+
residual.program[TERMINAL_OUTER_CLEANUP_RELEASE]?.(residual.cleanupLease);
|
|
554
|
+
residual.cleanupLease = undefined;
|
|
555
|
+
}
|
|
556
|
+
this._cleanupResiduals.delete(residual.program);
|
|
557
|
+
}
|
|
558
|
+
releaseRetainedTerminalIdentity(state) {
|
|
559
|
+
const residual = this._cleanupResiduals.get(state.program);
|
|
560
|
+
if (residual?.terminalState !== state) {
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
residual.terminalState = undefined;
|
|
564
|
+
if (residual.activeReservations === 0 && residual.failures.length === 0) {
|
|
565
|
+
this.releaseCleanupResidual(residual);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
async retryCleanupResidual(residual, failure) {
|
|
569
|
+
const operation = residual.program[failure.type];
|
|
570
|
+
if (!operation) {
|
|
571
|
+
throw new Error(`Program at ${residual.program.address} cannot retry its failed ${failure.type} cleanup`);
|
|
572
|
+
}
|
|
573
|
+
const checkpoint = this.terminalCheckpoint(residual.program);
|
|
574
|
+
const invoke = () => this.invokeLifecycleMethod(() => operation.apply(residual.program, failure.args));
|
|
575
|
+
try {
|
|
576
|
+
if (failure.commit) {
|
|
577
|
+
await this.retryCommittedTerminalCall(residual.program, failure.commit, invoke);
|
|
578
|
+
}
|
|
579
|
+
else {
|
|
580
|
+
await invoke();
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
catch (error) {
|
|
584
|
+
failure.commit ??= this.terminalCommit(residual.program, checkpoint, failure.type, failure.args[0]);
|
|
585
|
+
throw error;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
finishTerminalOperation(state) {
|
|
589
|
+
if (state.activeCalls > 0 ||
|
|
590
|
+
state.failed ||
|
|
591
|
+
!state.terminalCompleted ||
|
|
592
|
+
!state.program.closed) {
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
if (this.items.get(state.address) === state.program) {
|
|
596
|
+
this.items.delete(state.address);
|
|
597
|
+
}
|
|
598
|
+
this.restoreTerminalOperations(state);
|
|
599
|
+
if (this._failedInitializations.get(state.address) === state.program) {
|
|
600
|
+
this._failedInitializations.delete(state.address);
|
|
601
|
+
}
|
|
602
|
+
if (this._terminalOperationsByAddress.get(state.address) === state) {
|
|
603
|
+
this._terminalOperationsByAddress.delete(state.address);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
restoreTerminalOperations(state) {
|
|
607
|
+
if (state.closeWrapper &&
|
|
608
|
+
state.closeOperation &&
|
|
609
|
+
state.program.close === state.closeWrapper) {
|
|
610
|
+
state.program.close = state.closeOperation;
|
|
611
|
+
}
|
|
612
|
+
const droppable = state.program;
|
|
613
|
+
if (state.dropWrapper &&
|
|
614
|
+
state.dropOperation &&
|
|
615
|
+
droppable.drop === state.dropWrapper) {
|
|
616
|
+
droppable.drop = state.dropOperation;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
findMonitoredPrograms(address) {
|
|
620
|
+
const targetAddress = address.toString();
|
|
621
|
+
const pending = [...this.items.values()];
|
|
622
|
+
const visited = new Set();
|
|
623
|
+
const matches = [];
|
|
624
|
+
while (pending.length > 0) {
|
|
625
|
+
const candidate = pending.pop();
|
|
626
|
+
if (visited.has(candidate))
|
|
627
|
+
continue;
|
|
628
|
+
visited.add(candidate);
|
|
629
|
+
if (candidate.address.toString() === targetAddress) {
|
|
630
|
+
matches.push(candidate);
|
|
631
|
+
}
|
|
632
|
+
for (const child of candidate.children ?? []) {
|
|
633
|
+
pending.push(child);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return matches;
|
|
637
|
+
}
|
|
638
|
+
findMonitoredProgram(address, liveOnly = false, preferred) {
|
|
639
|
+
const matches = this.findMonitoredPrograms(address);
|
|
640
|
+
if (preferred &&
|
|
641
|
+
matches.includes(preferred) &&
|
|
642
|
+
(!liveOnly || !preferred.closed)) {
|
|
643
|
+
return preferred;
|
|
644
|
+
}
|
|
645
|
+
return liveOnly
|
|
646
|
+
? matches.find((candidate) => !candidate.closed)
|
|
647
|
+
: (matches.find((candidate) => !candidate.closed) ?? matches[0]);
|
|
648
|
+
}
|
|
649
|
+
attachExistingProgram(address, program, parent) {
|
|
650
|
+
if (parent)
|
|
651
|
+
this.assertParentCanOwnOpen(parent);
|
|
652
|
+
addParent(program, parent);
|
|
653
|
+
if (parent == null && this.items.get(address) !== program) {
|
|
654
|
+
const direct = this.items.get(address);
|
|
655
|
+
if (direct && !direct.closed && direct !== program) {
|
|
656
|
+
throw new Error(`Program at ${address} is already monitored as another live instance`);
|
|
657
|
+
}
|
|
658
|
+
this.monitorTerminalOperations(address, program);
|
|
659
|
+
this.items.set(address, program);
|
|
660
|
+
}
|
|
661
|
+
return program;
|
|
662
|
+
}
|
|
663
|
+
assertNoTerminalOperation(address, requestedProgram, allowSettledAncestorRepair = requestedProgram != null) {
|
|
664
|
+
const residual = [...this._cleanupResiduals.values()].find((candidate) => candidate.program.address.toString() === address.toString());
|
|
665
|
+
if (residual) {
|
|
666
|
+
throw new Error(`Program at ${address} has pending cleanup residuals and cannot be reopened before cleanup is retried`);
|
|
667
|
+
}
|
|
668
|
+
const state = this._terminalOperationsByAddress.get(address);
|
|
669
|
+
if (state?.activeCalls) {
|
|
670
|
+
throw new Error(`Program is terminating and cannot accept another parent while its close or drop operation is finishing (${address})`);
|
|
671
|
+
}
|
|
672
|
+
if (state?.failed) {
|
|
673
|
+
throw new Error(`Program at ${address} failed terminal cleanup and cannot be reopened before cleanup is retried`);
|
|
674
|
+
}
|
|
675
|
+
const targetAddress = address.toString();
|
|
676
|
+
const pending = [...this.items.values()].map((program) => ({
|
|
677
|
+
program,
|
|
678
|
+
ancestorActive: false,
|
|
679
|
+
ancestorSettled: false,
|
|
680
|
+
ancestorResidual: false,
|
|
29
681
|
}));
|
|
682
|
+
for (const cleanupResidual of this._cleanupResiduals.values()) {
|
|
683
|
+
pending.push({
|
|
684
|
+
program: cleanupResidual.program,
|
|
685
|
+
ancestorActive: false,
|
|
686
|
+
ancestorSettled: false,
|
|
687
|
+
ancestorResidual: true,
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
const visited = new Map();
|
|
691
|
+
while (pending.length > 0) {
|
|
692
|
+
const { program: candidate, ancestorActive, ancestorSettled, ancestorResidual, } = pending.pop();
|
|
693
|
+
const candidateState = this._terminalOperationsByProgram.get(candidate);
|
|
694
|
+
const ownActive = (candidateState?.activeCalls ?? 0) > 0;
|
|
695
|
+
const ownFailed = candidateState?.failed === true;
|
|
696
|
+
const ownAttachmentFence = candidate.acceptsParentAttachments === false &&
|
|
697
|
+
(!candidate.closed ||
|
|
698
|
+
candidate.pendingTerminalOperation != null ||
|
|
699
|
+
ownActive ||
|
|
700
|
+
ownFailed);
|
|
701
|
+
const stateMask = (ancestorActive ? 1 : 0) |
|
|
702
|
+
(ancestorSettled ? 2 : 0) |
|
|
703
|
+
(ancestorResidual ? 4 : 0);
|
|
704
|
+
const previousMask = visited.get(candidate) ?? -1;
|
|
705
|
+
if (previousMask !== -1 && (previousMask | stateMask) === previousMask) {
|
|
706
|
+
continue;
|
|
707
|
+
}
|
|
708
|
+
visited.set(candidate, previousMask === -1 ? stateMask : previousMask | stateMask);
|
|
709
|
+
if (candidate.address.toString() === targetAddress) {
|
|
710
|
+
const exactRepair = allowSettledAncestorRepair && requestedProgram === candidate;
|
|
711
|
+
if (ownActive ||
|
|
712
|
+
ownFailed ||
|
|
713
|
+
ownAttachmentFence ||
|
|
714
|
+
ancestorActive ||
|
|
715
|
+
ancestorResidual ||
|
|
716
|
+
(ancestorSettled && !exactRepair)) {
|
|
717
|
+
throw new Error(`Program is terminating and cannot accept another parent while its cleanup is finishing (${address})`);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
const descendantActive = ancestorActive || ownActive;
|
|
721
|
+
const descendantSettled = ancestorSettled || (!ownActive && (ownFailed || ownAttachmentFence));
|
|
722
|
+
for (const child of candidate.children ?? []) {
|
|
723
|
+
pending.push({
|
|
724
|
+
program: child,
|
|
725
|
+
ancestorActive: descendantActive,
|
|
726
|
+
ancestorSettled: descendantSettled,
|
|
727
|
+
ancestorResidual,
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
assertTerminalGraphCanStart(program) {
|
|
733
|
+
if (this._initializationRollbacks.has(program)) {
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
const replacing = this._replacementClosures.has(program);
|
|
737
|
+
const pending = [program];
|
|
738
|
+
const visited = new Set();
|
|
739
|
+
while (pending.length > 0) {
|
|
740
|
+
const candidate = pending.pop();
|
|
741
|
+
if (visited.has(candidate))
|
|
742
|
+
continue;
|
|
743
|
+
visited.add(candidate);
|
|
744
|
+
if ((this._parentAttachmentReservations.get(candidate) ?? 0) > 0) {
|
|
745
|
+
throw new TerminalOperationNotStartedError(`Program at ${candidate.address} has an opening child attachment that must finish first`);
|
|
746
|
+
}
|
|
747
|
+
const address = candidate.address.toString();
|
|
748
|
+
const rootReplacementOpen = replacing && candidate === program;
|
|
749
|
+
if ((this._openingPromises.has(address) && !rootReplacementOpen) ||
|
|
750
|
+
(this._openingReservationsByAddress.get(address)?.size ?? 0) > 0) {
|
|
751
|
+
throw new TerminalOperationNotStartedError(`Program at ${address} cannot terminate while an open generation owns its address`);
|
|
752
|
+
}
|
|
753
|
+
for (const child of candidate.children ?? []) {
|
|
754
|
+
pending.push(child);
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
async waitForTerminalOperations(program) {
|
|
759
|
+
const state = this._terminalOperationsByProgram.get(program);
|
|
760
|
+
while (state && state.pending.size > 0) {
|
|
761
|
+
// A rejected direct close is not itself the result of stop(). Once it has
|
|
762
|
+
// settled, closeCompletely() retries the retained instance below.
|
|
763
|
+
await Promise.allSettled([...state.pending]);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
async closeCompletely(program) {
|
|
767
|
+
await this.waitForTerminalOperations(program);
|
|
768
|
+
let terminalState = this._terminalOperationsByProgram.get(program);
|
|
769
|
+
while (!program.closed || terminalState?.failed) {
|
|
770
|
+
if (terminalState) {
|
|
771
|
+
// Applications/tests may replace a failed wrapped method before stop.
|
|
772
|
+
// Re-wrap any replacement in the existing identity-specific state before
|
|
773
|
+
// invoking it, so both a fresh failure and exact recovery remain monitored.
|
|
774
|
+
this.refreshTerminalOperationWrappers(terminalState);
|
|
775
|
+
}
|
|
776
|
+
const wasRecovering = terminalState?.failed === true;
|
|
777
|
+
const failedCall = terminalState?.failedCall;
|
|
778
|
+
const ownersBefore = [...(program.parents ?? [])];
|
|
779
|
+
const owner = wasRecovering
|
|
780
|
+
? failedCall?.args[0]
|
|
781
|
+
: program.closed
|
|
782
|
+
? undefined
|
|
783
|
+
: ownersBefore[0];
|
|
784
|
+
const ownerReferencesBefore = ownersBefore.filter((candidate) => candidate === owner).length;
|
|
785
|
+
const operationType = failedCall?.type ??
|
|
786
|
+
(terminalState?.failedOperation === "drop" ? "drop" : "close");
|
|
787
|
+
const operation = program[operationType];
|
|
788
|
+
if (!operation) {
|
|
789
|
+
throw new Error(`Program at ${program.address} cannot retry its failed ${operationType} cleanup`);
|
|
790
|
+
}
|
|
791
|
+
const operationArgs = wasRecovering ? (failedCall?.args ?? []) : [owner];
|
|
792
|
+
const closed = await operation.apply(program, operationArgs);
|
|
793
|
+
await this.waitForTerminalOperations(program);
|
|
794
|
+
terminalState = this._terminalOperationsByProgram.get(program);
|
|
795
|
+
if (terminalState?.failed &&
|
|
796
|
+
terminalState.failedCall === failedCall &&
|
|
797
|
+
terminalState.failedOperation === operationType &&
|
|
798
|
+
((closed && program.closed) ||
|
|
799
|
+
(failedCall?.commit != null && closed === failedCall.commit.result))) {
|
|
800
|
+
// This also supports a test/application replacing the wrapped method
|
|
801
|
+
// after open: closeCompletely awaited the full replacement call, so it
|
|
802
|
+
// is safe to mark the previously retained cleanup as recovered. A
|
|
803
|
+
// committed non-terminal release must reproduce its recorded `false`;
|
|
804
|
+
// the fresh loop below then drains the owners that remain.
|
|
805
|
+
terminalState.failed = false;
|
|
806
|
+
terminalState.failedOperation = undefined;
|
|
807
|
+
terminalState.failedCall = undefined;
|
|
808
|
+
terminalState.terminalCompleted = closed && program.closed;
|
|
809
|
+
this.releaseRetainedTerminalIdentity(terminalState);
|
|
810
|
+
if (terminalState.cleanupLease) {
|
|
811
|
+
program[TERMINAL_OUTER_CLEANUP_RELEASE]?.(terminalState.cleanupLease);
|
|
812
|
+
terminalState.cleanupLease = undefined;
|
|
813
|
+
}
|
|
814
|
+
this.finishTerminalOperation(terminalState);
|
|
815
|
+
}
|
|
816
|
+
const ownersAfter = program.parents ?? [];
|
|
817
|
+
const ownerReferencesAfter = ownersAfter.filter((candidate) => candidate === owner).length;
|
|
818
|
+
if (owner) {
|
|
819
|
+
const inverseEdges = owner.children?.filter((candidate) => candidate === program).length ??
|
|
820
|
+
0;
|
|
821
|
+
this.removeReleasedParentChildReferences(program, owner, Math.max(0, inverseEdges - ownerReferencesAfter));
|
|
822
|
+
}
|
|
823
|
+
if (program.closed && !terminalState?.failed) {
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
if (wasRecovering && !terminalState?.failed) {
|
|
827
|
+
// The failed outer call has now completed. Its base ownership transition
|
|
828
|
+
// may already have committed, so evaluate remaining owners on a fresh loop.
|
|
829
|
+
continue;
|
|
830
|
+
}
|
|
831
|
+
// A non-terminal Program.close(from) is valid only when it releases the
|
|
832
|
+
// requested owner and strictly reduces the total owner count. The latter is
|
|
833
|
+
// a monotonic measure that bounds this loop.
|
|
834
|
+
if (closed ||
|
|
835
|
+
ownersAfter.length >= ownersBefore.length ||
|
|
836
|
+
ownerReferencesAfter >= ownerReferencesBefore) {
|
|
837
|
+
throw new Error(`Program at ${program.address} did not make ownership progress while stopping (${ownersBefore.length} -> ${ownersAfter.length})`);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
removeReleasedParentChildReferences(program, parent, releasedReferences) {
|
|
842
|
+
if (!parent) {
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
while (releasedReferences > 0) {
|
|
846
|
+
const childIndex = parent.children?.indexOf(program) ?? -1;
|
|
847
|
+
if (childIndex === -1) {
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
parent.children.splice(childIndex, 1);
|
|
851
|
+
releasedReferences -= 1;
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
assertNoFailedInitialization(address) {
|
|
855
|
+
const failed = this._failedInitializations.get(address);
|
|
856
|
+
if (!failed) {
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
if (failed.closed) {
|
|
860
|
+
if (this._terminalOperationsByAddress.get(address)?.failed) {
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
this._failedInitializations.delete(address);
|
|
864
|
+
if (this.items.get(address) === failed) {
|
|
865
|
+
this.items.delete(address);
|
|
866
|
+
}
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
throw new Error(`Program at ${address} failed initialization cleanup and cannot be reopened before it is stopped`);
|
|
870
|
+
}
|
|
871
|
+
async stopOnce() {
|
|
872
|
+
// Do not PQueue.clear(): p-queue drops queued callbacks without settling the
|
|
873
|
+
// promises returned to callers. Admitted opens must run to completion (or
|
|
874
|
+
// rejection) before their programs can be closed below.
|
|
875
|
+
await Promise.all([...this._openAdmissions]);
|
|
876
|
+
await Promise.all([...this._openQueue.values()].map((queue) => queue.onIdle()));
|
|
30
877
|
this._openQueue.clear();
|
|
31
878
|
// Wait for any in-progress opens to complete before closing
|
|
32
879
|
// This prevents race conditions where a program is being opened while we close
|
|
@@ -34,17 +881,138 @@ export class Handler {
|
|
|
34
881
|
await Promise.allSettled([...this._openingPromises.values()]);
|
|
35
882
|
}
|
|
36
883
|
this._openingPromises.clear();
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
//
|
|
884
|
+
this._openingReservations.clear();
|
|
885
|
+
this._openingReservationsByAddress.clear();
|
|
886
|
+
// A failed child can have its wrapped terminal method replaced before stop.
|
|
887
|
+
// Refresh every retained failed identity before closing any parent root, so
|
|
888
|
+
// parent traversal consumes the recorded commit through the new wrapper
|
|
889
|
+
// instead of calling the replacement outside Handler monitoring.
|
|
890
|
+
const pendingTerminalGraphs = [
|
|
891
|
+
...this.items.values(),
|
|
892
|
+
...this._cleanupResiduals.keys(),
|
|
893
|
+
];
|
|
894
|
+
const visitedTerminalGraphs = new Set();
|
|
895
|
+
while (pendingTerminalGraphs.length > 0) {
|
|
896
|
+
const candidate = pendingTerminalGraphs.pop();
|
|
897
|
+
if (visitedTerminalGraphs.has(candidate))
|
|
898
|
+
continue;
|
|
899
|
+
visitedTerminalGraphs.add(candidate);
|
|
900
|
+
const terminalState = this._terminalOperationsByProgram.get(candidate);
|
|
901
|
+
if (terminalState?.failed) {
|
|
902
|
+
this.refreshTerminalOperationWrappers(terminalState);
|
|
903
|
+
}
|
|
904
|
+
for (const child of candidate.children ?? []) {
|
|
905
|
+
pendingTerminalGraphs.push(child);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
// A close can legitimately return false when it only releases one of several
|
|
909
|
+
// owners. Drain every ownership reference so stop cannot discard a still-live
|
|
910
|
+
// program from Handler state.
|
|
911
|
+
const closeErrors = [];
|
|
912
|
+
for (const [address, program] of [...this.items]) {
|
|
913
|
+
try {
|
|
914
|
+
await this.closeCompletely(program);
|
|
915
|
+
}
|
|
916
|
+
catch (error) {
|
|
917
|
+
closeErrors.push(error);
|
|
918
|
+
}
|
|
919
|
+
const terminalState = this._terminalOperationsByProgram.get(program);
|
|
920
|
+
if (program.closed &&
|
|
921
|
+
!terminalState?.failed &&
|
|
922
|
+
(terminalState?.activeCalls ?? 0) === 0 &&
|
|
923
|
+
this.items.get(address) === program) {
|
|
924
|
+
this.items.delete(address);
|
|
925
|
+
}
|
|
926
|
+
if (program.closed &&
|
|
927
|
+
!terminalState?.failed &&
|
|
928
|
+
this._failedInitializations.get(address) === program) {
|
|
929
|
+
this._failedInitializations.delete(address);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
for (const [program, residual] of [...this._cleanupResiduals]) {
|
|
933
|
+
try {
|
|
934
|
+
for (const failure of [...residual.failures]) {
|
|
935
|
+
await this.retryCleanupResidual(residual, failure);
|
|
936
|
+
const failureIndex = residual.failures.indexOf(failure);
|
|
937
|
+
if (failureIndex !== -1)
|
|
938
|
+
residual.failures.splice(failureIndex, 1);
|
|
939
|
+
}
|
|
940
|
+
await this.closeCompletely(program);
|
|
941
|
+
}
|
|
942
|
+
catch (error) {
|
|
943
|
+
closeErrors.push(error);
|
|
944
|
+
}
|
|
945
|
+
const terminalState = this._terminalOperationsByProgram.get(program);
|
|
946
|
+
if (program.closed &&
|
|
947
|
+
residual.failures.length === 0 &&
|
|
948
|
+
residual.terminalState == null &&
|
|
949
|
+
!terminalState?.failed &&
|
|
950
|
+
(terminalState?.activeCalls ?? 0) === 0) {
|
|
951
|
+
this.releaseCleanupResidual(residual);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
if (closeErrors.length > 0) {
|
|
955
|
+
throw closeErrors[0];
|
|
956
|
+
}
|
|
40
957
|
this.items = new Map();
|
|
958
|
+
this._failedInitializations.clear();
|
|
959
|
+
for (const residual of [...this._cleanupResiduals.values()]) {
|
|
960
|
+
this.releaseCleanupResidual(residual);
|
|
961
|
+
}
|
|
962
|
+
for (const state of new Set(this._terminalOperationsByAddress.values())) {
|
|
963
|
+
this.restoreTerminalOperations(state);
|
|
964
|
+
}
|
|
965
|
+
this._terminalOperationsByAddress.clear();
|
|
41
966
|
}
|
|
42
967
|
_onProgamClose(program) {
|
|
43
|
-
|
|
968
|
+
const address = program.address.toString();
|
|
969
|
+
const terminalState = this._terminalOperationsByProgram.get(program);
|
|
970
|
+
if (terminalState?.activeCalls || terminalState?.failed) {
|
|
971
|
+
// The base Program has ended, but the dynamically-dispatched subclass
|
|
972
|
+
// close/drop promise is still running. Its wrapper owns final eviction.
|
|
973
|
+
return;
|
|
974
|
+
}
|
|
975
|
+
if (this.items.get(address) === program) {
|
|
976
|
+
this.items.delete(address);
|
|
977
|
+
}
|
|
978
|
+
if (this._failedInitializations.get(address) === program) {
|
|
979
|
+
this._failedInitializations.delete(address);
|
|
980
|
+
}
|
|
44
981
|
// TODO remove item from this._openQueue?
|
|
45
982
|
}
|
|
46
|
-
async
|
|
47
|
-
const
|
|
983
|
+
async closeForReplacement(address, program, parent) {
|
|
984
|
+
const owners = program.parents ?? [];
|
|
985
|
+
if (owners.length > 1 || (owners.length === 1 && owners[0] !== parent)) {
|
|
986
|
+
throw new Error(`Program at ${address} cannot be replaced while it has other owners`);
|
|
987
|
+
}
|
|
988
|
+
const parentReferencesBefore = owners.filter((candidate) => candidate === parent).length;
|
|
989
|
+
let closed;
|
|
990
|
+
this._replacementClosures.add(program);
|
|
991
|
+
try {
|
|
992
|
+
closed = await program.close.call(program, parent);
|
|
993
|
+
}
|
|
994
|
+
finally {
|
|
995
|
+
this._replacementClosures.delete(program);
|
|
996
|
+
}
|
|
997
|
+
const parentReferencesAfter = (program.parents ?? []).filter((candidate) => candidate === parent).length;
|
|
998
|
+
this.removeReleasedParentChildReferences(program, parent, parentReferencesBefore - parentReferencesAfter);
|
|
999
|
+
if (!closed || !program.closed || parentReferencesAfter > 0) {
|
|
1000
|
+
throw new Error(`Program at ${address} cannot be replaced because close was not terminal`);
|
|
1001
|
+
}
|
|
1002
|
+
if (this.items.get(address) === program) {
|
|
1003
|
+
this.items.delete(address);
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
async checkProcessExisting(address, toOpen, mergeSrategy = "reject", parent) {
|
|
1007
|
+
this.assertNoFailedInitialization(address);
|
|
1008
|
+
this.assertNoTerminalOperation(address, toOpen);
|
|
1009
|
+
const prev = this.findMonitoredProgram(address, false, toOpen);
|
|
1010
|
+
if (prev?.closed) {
|
|
1011
|
+
if (this.items.get(address) === prev) {
|
|
1012
|
+
this.items.delete(address);
|
|
1013
|
+
}
|
|
1014
|
+
return undefined;
|
|
1015
|
+
}
|
|
48
1016
|
if (mergeSrategy === "reject") {
|
|
49
1017
|
if (prev) {
|
|
50
1018
|
throw new Error(`Program at ${address} is already open`);
|
|
@@ -52,31 +1020,559 @@ export class Handler {
|
|
|
52
1020
|
}
|
|
53
1021
|
else if (mergeSrategy === "replace") {
|
|
54
1022
|
if (prev && prev !== toOpen) {
|
|
55
|
-
await
|
|
1023
|
+
await this.closeForReplacement(address, prev, parent);
|
|
56
1024
|
}
|
|
57
1025
|
}
|
|
58
1026
|
else if (mergeSrategy === "reuse") {
|
|
1027
|
+
if (prev) {
|
|
1028
|
+
this.attachExistingProgram(address, prev, parent);
|
|
1029
|
+
}
|
|
59
1030
|
return prev;
|
|
60
1031
|
}
|
|
61
1032
|
}
|
|
1033
|
+
async processParentExisting(address, requestedProgram, mergeStrategy, parent) {
|
|
1034
|
+
this.assertNoFailedInitialization(address);
|
|
1035
|
+
this.assertNoTerminalOperation(address, requestedProgram);
|
|
1036
|
+
const existing = this.findMonitoredProgram(address, false, requestedProgram);
|
|
1037
|
+
if (existing?.closed) {
|
|
1038
|
+
if (this.items.get(address) === existing) {
|
|
1039
|
+
this.items.delete(address);
|
|
1040
|
+
}
|
|
1041
|
+
return undefined;
|
|
1042
|
+
}
|
|
1043
|
+
if (!existing) {
|
|
1044
|
+
return undefined;
|
|
1045
|
+
}
|
|
1046
|
+
if (mergeStrategy === "reject") {
|
|
1047
|
+
throw new Error(`Program at ${address} is already open`);
|
|
1048
|
+
}
|
|
1049
|
+
if (mergeStrategy === "replace" && existing !== requestedProgram) {
|
|
1050
|
+
await this.closeForReplacement(address, existing, parent);
|
|
1051
|
+
return undefined;
|
|
1052
|
+
}
|
|
1053
|
+
this.attachExistingProgram(address, existing, parent);
|
|
1054
|
+
try {
|
|
1055
|
+
// A program explicitly opened with a parent is a weak/addressable
|
|
1056
|
+
// reference. Structural beforeOpen() may already have made the exact
|
|
1057
|
+
// instance live without persisting its standalone block.
|
|
1058
|
+
const blocks = this.properties.client.services.blocks;
|
|
1059
|
+
if (!(await blocks.has(address))) {
|
|
1060
|
+
await existing.save(blocks, { skipOnAddress: false });
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
catch (error) {
|
|
1064
|
+
const parentIndex = existing.parents?.lastIndexOf(parent) ?? -1;
|
|
1065
|
+
if (parentIndex !== -1)
|
|
1066
|
+
existing.parents.splice(parentIndex, 1);
|
|
1067
|
+
const childIndex = parent.children?.lastIndexOf(existing) ?? -1;
|
|
1068
|
+
if (childIndex !== -1)
|
|
1069
|
+
parent.children.splice(childIndex, 1);
|
|
1070
|
+
throw error;
|
|
1071
|
+
}
|
|
1072
|
+
return existing;
|
|
1073
|
+
}
|
|
1074
|
+
async rollbackFailedInitialization(address, program, state) {
|
|
1075
|
+
if (this.items.get(address) === program) {
|
|
1076
|
+
this.items.delete(address);
|
|
1077
|
+
}
|
|
1078
|
+
const cleanupErrors = [];
|
|
1079
|
+
const childrenAfterFailure = [...(program.children ?? [])];
|
|
1080
|
+
try {
|
|
1081
|
+
if (!program.closed) {
|
|
1082
|
+
await program.close();
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
catch (cleanupError) {
|
|
1086
|
+
cleanupErrors.push(cleanupError);
|
|
1087
|
+
logger.error(`Failed to close partially opened program at ${address}: ${String(cleanupError)}`);
|
|
1088
|
+
}
|
|
1089
|
+
// beforeOpen() can attach nested children before the parent itself becomes
|
|
1090
|
+
// open. Remove only relationships added by this failed generation, then
|
|
1091
|
+
// restore the exact caller-visible parent/child arrays captured at entry.
|
|
1092
|
+
const baselineChildCounts = new Map();
|
|
1093
|
+
for (const child of state.children ?? []) {
|
|
1094
|
+
baselineChildCounts.set(child, (baselineChildCounts.get(child) ?? 0) + 1);
|
|
1095
|
+
}
|
|
1096
|
+
const currentChildCounts = new Map();
|
|
1097
|
+
for (const child of childrenAfterFailure) {
|
|
1098
|
+
currentChildCounts.set(child, (currentChildCounts.get(child) ?? 0) + 1);
|
|
1099
|
+
}
|
|
1100
|
+
for (const [child, currentCount] of currentChildCounts) {
|
|
1101
|
+
const baselineCount = baselineChildCounts.get(child) ?? 0;
|
|
1102
|
+
let extra = currentCount - baselineCount;
|
|
1103
|
+
while (extra > 0) {
|
|
1104
|
+
const referencesBefore = child.parents?.filter((parent) => parent === program).length ?? 0;
|
|
1105
|
+
let retainedFailure = false;
|
|
1106
|
+
let cleanupReservation;
|
|
1107
|
+
if (!child.closed && referencesBefore > 0) {
|
|
1108
|
+
const checkpoint = this.terminalCheckpoint(child);
|
|
1109
|
+
cleanupReservation = this.acquireCleanupReservation(child);
|
|
1110
|
+
try {
|
|
1111
|
+
await child.close.call(child, program);
|
|
1112
|
+
}
|
|
1113
|
+
catch (cleanupError) {
|
|
1114
|
+
retainedFailure = true;
|
|
1115
|
+
const terminalState = this._terminalOperationsByProgram.get(child);
|
|
1116
|
+
const handlerOwnsFailure = terminalState?.failed === true &&
|
|
1117
|
+
terminalState.failedCall?.type === "close" &&
|
|
1118
|
+
this.sameTerminalArgs(terminalState.failedCall.args, [program]);
|
|
1119
|
+
if (handlerOwnsFailure) {
|
|
1120
|
+
}
|
|
1121
|
+
else {
|
|
1122
|
+
cleanupReservation.failures.push({
|
|
1123
|
+
type: "close",
|
|
1124
|
+
args: [program],
|
|
1125
|
+
commit: this.terminalCommit(child, checkpoint, "close", program),
|
|
1126
|
+
});
|
|
1127
|
+
}
|
|
1128
|
+
this.releaseCleanupReservation(cleanupReservation);
|
|
1129
|
+
cleanupReservation = undefined;
|
|
1130
|
+
cleanupErrors.push(cleanupError);
|
|
1131
|
+
logger.error(`Failed to close partially opened child of ${address}: ${String(cleanupError)}`);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
let referencesAfter = child.parents?.filter((parent) => parent === program).length ?? 0;
|
|
1135
|
+
if (!retainedFailure &&
|
|
1136
|
+
referencesAfter >= referencesBefore &&
|
|
1137
|
+
referencesAfter > 0) {
|
|
1138
|
+
const parentIndex = child.parents.lastIndexOf(program);
|
|
1139
|
+
child.parents.splice(parentIndex, 1);
|
|
1140
|
+
referencesAfter -= 1;
|
|
1141
|
+
}
|
|
1142
|
+
if (cleanupReservation) {
|
|
1143
|
+
this.releaseCleanupReservation(cleanupReservation);
|
|
1144
|
+
}
|
|
1145
|
+
extra -= 1;
|
|
1146
|
+
}
|
|
1147
|
+
const remainingProgramReferences = child.parents?.filter((parent) => parent === program).length ?? 0;
|
|
1148
|
+
if (!child.closed &&
|
|
1149
|
+
baselineCount === 0 &&
|
|
1150
|
+
(remainingProgramReferences > 0 || (child.parents?.length ?? 0) === 0)) {
|
|
1151
|
+
// A failed nested cleanup must remain reachable by stop(); restoring the
|
|
1152
|
+
// caller-visible arrays alone would otherwise orphan a live resource.
|
|
1153
|
+
if (!this._cleanupResiduals.has(child)) {
|
|
1154
|
+
this.addCleanupResidual(child, {
|
|
1155
|
+
type: "close",
|
|
1156
|
+
args: [remainingProgramReferences > 0 ? program : undefined],
|
|
1157
|
+
});
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
program.parents =
|
|
1162
|
+
state.parents ? [...state.parents] : undefined;
|
|
1163
|
+
program.children = state.children
|
|
1164
|
+
? [...state.children]
|
|
1165
|
+
: undefined;
|
|
1166
|
+
if (state.parent) {
|
|
1167
|
+
let currentReferences = state.parent.children?.filter((child) => child === program).length ?? 0;
|
|
1168
|
+
while (currentReferences > state.parentChildReferences) {
|
|
1169
|
+
const childIndex = state.parent.children.lastIndexOf(program);
|
|
1170
|
+
if (childIndex === -1) {
|
|
1171
|
+
break;
|
|
1172
|
+
}
|
|
1173
|
+
state.parent.children.splice(childIndex, 1);
|
|
1174
|
+
currentReferences -= 1;
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
if (!program.closed) {
|
|
1178
|
+
// Never manufacture a terminal state after close() failed. Keep the
|
|
1179
|
+
// partially initialized instance identity-tracked so later opens cannot
|
|
1180
|
+
// reuse it and stop() can retry cleanup.
|
|
1181
|
+
this.items.set(address, program);
|
|
1182
|
+
this._failedInitializations.set(address, program);
|
|
1183
|
+
}
|
|
1184
|
+
else {
|
|
1185
|
+
this._failedInitializations.delete(address);
|
|
1186
|
+
}
|
|
1187
|
+
if (cleanupErrors.length > 0) {
|
|
1188
|
+
logger.error(`Program initialization rollback at ${address} encountered ${cleanupErrors.length} cleanup error(s)`);
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
dependencyGraph(program) {
|
|
1192
|
+
const dependencies = this.properties.getDependencies?.(program) ?? [];
|
|
1193
|
+
return [...new Set([program, ...dependencies])];
|
|
1194
|
+
}
|
|
1195
|
+
async resolveDependencyAddresses(program) {
|
|
1196
|
+
const addresses = new Map();
|
|
1197
|
+
for (const candidate of this.dependencyGraph(program)) {
|
|
1198
|
+
const address = await candidate.save(this.properties.client.services.blocks, {
|
|
1199
|
+
skipOnAddress: true,
|
|
1200
|
+
save: () => false,
|
|
1201
|
+
});
|
|
1202
|
+
addresses.set(candidate, address.toString());
|
|
1203
|
+
}
|
|
1204
|
+
return addresses;
|
|
1205
|
+
}
|
|
1206
|
+
cleanupFailedOpeningMonitoring(reservation) {
|
|
1207
|
+
for (const state of reservation.provisionalTerminalStates) {
|
|
1208
|
+
if (!state.program.closed ||
|
|
1209
|
+
state.activeCalls > 0 ||
|
|
1210
|
+
state.pending.size > 0 ||
|
|
1211
|
+
state.failed ||
|
|
1212
|
+
state.cleanupLease ||
|
|
1213
|
+
state.program.pendingTerminalOperation != null ||
|
|
1214
|
+
this._cleanupResiduals.has(state.program)) {
|
|
1215
|
+
continue;
|
|
1216
|
+
}
|
|
1217
|
+
this.restoreTerminalOperations(state);
|
|
1218
|
+
if (this._terminalOperationsByAddress.get(state.address) === state) {
|
|
1219
|
+
this._terminalOperationsByAddress.delete(state.address);
|
|
1220
|
+
}
|
|
1221
|
+
this._terminalOperationsByProgram.delete(state.program);
|
|
1222
|
+
}
|
|
1223
|
+
reservation.provisionalTerminalStates.clear();
|
|
1224
|
+
}
|
|
1225
|
+
releaseOpeningReservation(reservation, failed = false) {
|
|
1226
|
+
for (const address of reservation.programsByAddress.keys()) {
|
|
1227
|
+
const reservations = this._openingReservationsByAddress.get(address);
|
|
1228
|
+
reservations?.delete(reservation);
|
|
1229
|
+
if (reservations?.size === 0) {
|
|
1230
|
+
this._openingReservationsByAddress.delete(address);
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
if (failed) {
|
|
1234
|
+
for (const participant of reservation.contributedParticipants) {
|
|
1235
|
+
const references = reservation.group.participantReferences.get(participant) ?? 0;
|
|
1236
|
+
if (references <= 1) {
|
|
1237
|
+
reservation.group.participantReferences.delete(participant);
|
|
1238
|
+
}
|
|
1239
|
+
else {
|
|
1240
|
+
reservation.group.participantReferences.set(participant, references - 1);
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
reservation.group.reservations.delete(reservation);
|
|
1245
|
+
this._openingReservations.delete(reservation);
|
|
1246
|
+
reservation.programsByAddress.clear();
|
|
1247
|
+
reservation.programs.clear();
|
|
1248
|
+
reservation.adoptedParents.clear();
|
|
1249
|
+
reservation.contributedParticipants.clear();
|
|
1250
|
+
reservation.wakeWaiters.clear();
|
|
1251
|
+
if (failed) {
|
|
1252
|
+
this.cleanupFailedOpeningMonitoring(reservation);
|
|
1253
|
+
}
|
|
1254
|
+
else {
|
|
1255
|
+
reservation.provisionalTerminalStates.clear();
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
reservationContains(reservation, program) {
|
|
1259
|
+
return reservation.group.participantReferences.has(program);
|
|
1260
|
+
}
|
|
1261
|
+
contributeOpeningParticipant(reservation, program) {
|
|
1262
|
+
if (reservation.contributedParticipants.has(program))
|
|
1263
|
+
return;
|
|
1264
|
+
reservation.contributedParticipants.add(program);
|
|
1265
|
+
reservation.group.participantReferences.set(program, (reservation.group.participantReferences.get(program) ?? 0) + 1);
|
|
1266
|
+
}
|
|
1267
|
+
mergeOpeningReservationGroups(left, right) {
|
|
1268
|
+
if (left.group === right.group)
|
|
1269
|
+
return;
|
|
1270
|
+
const target = left.group;
|
|
1271
|
+
const source = right.group;
|
|
1272
|
+
for (const [participant, references] of source.participantReferences) {
|
|
1273
|
+
target.participantReferences.set(participant, (target.participantReferences.get(participant) ?? 0) + references);
|
|
1274
|
+
}
|
|
1275
|
+
for (const reservation of source.reservations) {
|
|
1276
|
+
reservation.group = target;
|
|
1277
|
+
target.reservations.add(reservation);
|
|
1278
|
+
}
|
|
1279
|
+
source.reservations.clear();
|
|
1280
|
+
source.participantReferences.clear();
|
|
1281
|
+
}
|
|
1282
|
+
canShareOpeningReservation(existing, reservation, address, candidates, parent) {
|
|
1283
|
+
const existingCandidates = existing.programsByAddress.get(address);
|
|
1284
|
+
if (!existingCandidates ||
|
|
1285
|
+
[...candidates].some((candidate) => !existingCandidates.has(candidate))) {
|
|
1286
|
+
return false;
|
|
1287
|
+
}
|
|
1288
|
+
if (existing.group === reservation.group)
|
|
1289
|
+
return true;
|
|
1290
|
+
if (parent && this.reservationContains(existing, parent))
|
|
1291
|
+
return true;
|
|
1292
|
+
return [...reservation.adoptedParents].some((adoptedParent) => this.reservationContains(existing, adoptedParent));
|
|
1293
|
+
}
|
|
1294
|
+
canOpenAlongsideReservation(existing, address, candidates, rootProgram) {
|
|
1295
|
+
const existingCandidates = existing.programsByAddress.get(address);
|
|
1296
|
+
if (!existingCandidates)
|
|
1297
|
+
return false;
|
|
1298
|
+
if ([...candidates].some((candidate) => existingCandidates.has(candidate))) {
|
|
1299
|
+
return false;
|
|
1300
|
+
}
|
|
1301
|
+
return (!candidates.has(rootProgram) &&
|
|
1302
|
+
(existing.rootProgram == null ||
|
|
1303
|
+
!existingCandidates.has(existing.rootProgram)));
|
|
1304
|
+
}
|
|
1305
|
+
adoptOpeningReservation(promise, parent) {
|
|
1306
|
+
const reservation = this._openingReservationsByPromise.get(promise);
|
|
1307
|
+
if (!reservation || reservation.phase !== "opening")
|
|
1308
|
+
return;
|
|
1309
|
+
reservation.adoptedParents.add(parent);
|
|
1310
|
+
this.contributeOpeningParticipant(reservation, parent);
|
|
1311
|
+
for (const wake of [...reservation.wakeWaiters])
|
|
1312
|
+
wake();
|
|
1313
|
+
}
|
|
1314
|
+
async waitForOpeningConflictChange(reservation, waits) {
|
|
1315
|
+
let wake;
|
|
1316
|
+
const adopted = new Promise((resolve) => {
|
|
1317
|
+
wake = resolve;
|
|
1318
|
+
});
|
|
1319
|
+
reservation.wakeWaiters.add(wake);
|
|
1320
|
+
try {
|
|
1321
|
+
await Promise.race([Promise.allSettled(waits), adopted]);
|
|
1322
|
+
}
|
|
1323
|
+
finally {
|
|
1324
|
+
reservation.wakeWaiters.delete(wake);
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
assertParentCanOwnOpen(parent) {
|
|
1328
|
+
if (this._initializationRollbacks.has(parent)) {
|
|
1329
|
+
throw new Error("Parent program is finishing initialization rollback cleanup");
|
|
1330
|
+
}
|
|
1331
|
+
if (this._cleanupResiduals.has(parent) ||
|
|
1332
|
+
[...this._failedInitializations.values()].some((failed) => failed === parent)) {
|
|
1333
|
+
throw new Error("Parent program has failed lifecycle cleanup");
|
|
1334
|
+
}
|
|
1335
|
+
const terminalState = this._terminalOperationsByProgram.get(parent);
|
|
1336
|
+
if ((terminalState?.activeCalls ?? 0) > 0 ||
|
|
1337
|
+
terminalState?.failed ||
|
|
1338
|
+
parent.acceptsParentAttachments === false) {
|
|
1339
|
+
throw new Error("Parent program is finishing terminal cleanup");
|
|
1340
|
+
}
|
|
1341
|
+
if (parent.closed) {
|
|
1342
|
+
throw new Error("Parent program is closed");
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
retainParentAttachment(parent) {
|
|
1346
|
+
this._parentAttachmentReservations.set(parent, (this._parentAttachmentReservations.get(parent) ?? 0) + 1);
|
|
1347
|
+
}
|
|
1348
|
+
releaseParentAttachment(parent) {
|
|
1349
|
+
const reservations = this._parentAttachmentReservations.get(parent) ?? 0;
|
|
1350
|
+
if (reservations <= 1) {
|
|
1351
|
+
this._parentAttachmentReservations.delete(parent);
|
|
1352
|
+
}
|
|
1353
|
+
else {
|
|
1354
|
+
this._parentAttachmentReservations.set(parent, reservations - 1);
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
isReservedNestedUse(address, program, parent) {
|
|
1358
|
+
return [...(this._openingReservationsByAddress.get(address) ?? [])].some((reservation) => this.reservationContains(reservation, parent) &&
|
|
1359
|
+
(program == null ||
|
|
1360
|
+
reservation.programsByAddress.get(address)?.has(program) === true));
|
|
1361
|
+
}
|
|
1362
|
+
async waitForOpeningReservations(address, program, parent, observedReservations = new Set()) {
|
|
1363
|
+
while (true) {
|
|
1364
|
+
const waits = new Set();
|
|
1365
|
+
for (const reservation of this._openingReservationsByAddress.get(address) ?? []) {
|
|
1366
|
+
if (parent &&
|
|
1367
|
+
this.reservationContains(reservation, parent) &&
|
|
1368
|
+
(program == null ||
|
|
1369
|
+
reservation.programsByAddress.get(address)?.has(program) === true)) {
|
|
1370
|
+
continue;
|
|
1371
|
+
}
|
|
1372
|
+
if (reservation.phase === "rollback" &&
|
|
1373
|
+
!observedReservations.has(reservation)) {
|
|
1374
|
+
throw new Error(`Program at ${address} is finishing initialization rollback cleanup`);
|
|
1375
|
+
}
|
|
1376
|
+
waits.add(reservation.promise);
|
|
1377
|
+
}
|
|
1378
|
+
if (waits.size === 0)
|
|
1379
|
+
return;
|
|
1380
|
+
await Promise.allSettled(waits);
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
async prepareOpeningGraph(program, reservation, parent, resolvedAddresses) {
|
|
1384
|
+
const addresses = resolvedAddresses ?? (await this.resolveDependencyAddresses(program));
|
|
1385
|
+
reservation.rootProgram = program;
|
|
1386
|
+
const programs = [...addresses.keys()];
|
|
1387
|
+
for (const candidate of programs) {
|
|
1388
|
+
this.contributeOpeningParticipant(reservation, candidate);
|
|
1389
|
+
}
|
|
1390
|
+
const programsByAddress = new Map();
|
|
1391
|
+
for (const [candidate, address] of addresses) {
|
|
1392
|
+
const candidates = programsByAddress.get(address) ?? new Set();
|
|
1393
|
+
candidates.add(candidate);
|
|
1394
|
+
programsByAddress.set(address, candidates);
|
|
1395
|
+
}
|
|
1396
|
+
while (true) {
|
|
1397
|
+
const waits = new Set();
|
|
1398
|
+
for (const [address, candidates] of programsByAddress) {
|
|
1399
|
+
for (const existing of this._openingReservationsByAddress.get(address) ?? []) {
|
|
1400
|
+
if (existing === reservation)
|
|
1401
|
+
continue;
|
|
1402
|
+
if (this.canShareOpeningReservation(existing, reservation, address, candidates, parent)) {
|
|
1403
|
+
this.mergeOpeningReservationGroups(reservation, existing);
|
|
1404
|
+
continue;
|
|
1405
|
+
}
|
|
1406
|
+
if (existing.phase === "rollback" &&
|
|
1407
|
+
!reservation.observedReservations.has(existing)) {
|
|
1408
|
+
throw new Error(`Program at ${address} is finishing initialization rollback cleanup`);
|
|
1409
|
+
}
|
|
1410
|
+
if (this.canOpenAlongsideReservation(existing, address, candidates, program)) {
|
|
1411
|
+
continue;
|
|
1412
|
+
}
|
|
1413
|
+
const existingPrograms = existing.programsByAddress.get(address);
|
|
1414
|
+
if (existingPrograms &&
|
|
1415
|
+
![...candidates].some((candidate) => existingPrograms.has(candidate))) {
|
|
1416
|
+
throw new Error(`Program at ${address} is already opening as another instance`);
|
|
1417
|
+
}
|
|
1418
|
+
waits.add(existing.promise);
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
if (waits.size === 0)
|
|
1422
|
+
break;
|
|
1423
|
+
await this.waitForOpeningConflictChange(reservation, waits);
|
|
1424
|
+
}
|
|
1425
|
+
for (const [address, candidates] of programsByAddress) {
|
|
1426
|
+
this.assertNoFailedInitialization(address);
|
|
1427
|
+
for (const candidate of candidates) {
|
|
1428
|
+
this.assertNoTerminalOperation(address, candidate, candidate === program);
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
// No await separates the final conflict check from claiming every address.
|
|
1432
|
+
// Terminal admission reads the same map synchronously, so either the open
|
|
1433
|
+
// generation owns the whole graph or cleanup wins and the checks above fail.
|
|
1434
|
+
reservation.programs = new Set(programs);
|
|
1435
|
+
for (const [address, candidates] of programsByAddress) {
|
|
1436
|
+
for (const existing of this._openingReservationsByAddress.get(address) ??
|
|
1437
|
+
[]) {
|
|
1438
|
+
if (existing === reservation)
|
|
1439
|
+
continue;
|
|
1440
|
+
if (this.canShareOpeningReservation(existing, reservation, address, candidates, parent)) {
|
|
1441
|
+
this.mergeOpeningReservationGroups(reservation, existing);
|
|
1442
|
+
}
|
|
1443
|
+
else if (this.canOpenAlongsideReservation(existing, address, candidates, program)) {
|
|
1444
|
+
continue;
|
|
1445
|
+
}
|
|
1446
|
+
else {
|
|
1447
|
+
throw new TerminalOperationNotStartedError(`Program at ${address} became reserved by another open generation`);
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
reservation.programsByAddress.set(address, new Set(candidates));
|
|
1451
|
+
const reservations = this._openingReservationsByAddress.get(address) ?? new Set();
|
|
1452
|
+
reservations.add(reservation);
|
|
1453
|
+
this._openingReservationsByAddress.set(address, reservations);
|
|
1454
|
+
}
|
|
1455
|
+
for (const [address, candidates] of programsByAddress) {
|
|
1456
|
+
for (const candidate of candidates) {
|
|
1457
|
+
const monitoring = this.monitorTerminalOperations(address, candidate);
|
|
1458
|
+
if (monitoring.newLifecycle) {
|
|
1459
|
+
reservation.provisionalTerminalStates.add(monitoring.state);
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
return addresses;
|
|
1464
|
+
}
|
|
1465
|
+
trackOpening(address, open, observedReservations) {
|
|
1466
|
+
let resolve;
|
|
1467
|
+
let reject;
|
|
1468
|
+
const openPromise = new Promise((promiseResolve, promiseReject) => {
|
|
1469
|
+
resolve = promiseResolve;
|
|
1470
|
+
reject = promiseReject;
|
|
1471
|
+
});
|
|
1472
|
+
const group = {
|
|
1473
|
+
participantReferences: new Map(),
|
|
1474
|
+
reservations: new Set(),
|
|
1475
|
+
};
|
|
1476
|
+
const reservation = {
|
|
1477
|
+
promise: openPromise,
|
|
1478
|
+
phase: "opening",
|
|
1479
|
+
observedReservations,
|
|
1480
|
+
adoptedParents: new Set(),
|
|
1481
|
+
contributedParticipants: new Set(),
|
|
1482
|
+
wakeWaiters: new Set(),
|
|
1483
|
+
group,
|
|
1484
|
+
programsByAddress: new Map(),
|
|
1485
|
+
programs: new Set(),
|
|
1486
|
+
provisionalTerminalStates: new Set(),
|
|
1487
|
+
};
|
|
1488
|
+
group.reservations.add(reservation);
|
|
1489
|
+
this._openingReservations.add(reservation);
|
|
1490
|
+
this._openingReservationsByPromise.set(openPromise, reservation);
|
|
1491
|
+
this._openingPromises.set(address, openPromise);
|
|
1492
|
+
let execution;
|
|
1493
|
+
try {
|
|
1494
|
+
execution = open(reservation);
|
|
1495
|
+
}
|
|
1496
|
+
catch (error) {
|
|
1497
|
+
execution = Promise.reject(error);
|
|
1498
|
+
}
|
|
1499
|
+
void execution.then((program) => {
|
|
1500
|
+
this.releaseOpeningReservation(reservation);
|
|
1501
|
+
if (this._openingPromises.get(address) === openPromise) {
|
|
1502
|
+
this._openingPromises.delete(address);
|
|
1503
|
+
}
|
|
1504
|
+
resolve(program);
|
|
1505
|
+
}, (error) => {
|
|
1506
|
+
this.releaseOpeningReservation(reservation, true);
|
|
1507
|
+
if (this._openingPromises.get(address) === openPromise) {
|
|
1508
|
+
this._openingPromises.delete(address);
|
|
1509
|
+
}
|
|
1510
|
+
reject(error);
|
|
1511
|
+
});
|
|
1512
|
+
return openPromise;
|
|
1513
|
+
}
|
|
62
1514
|
async open(storeOrAddress, options = {}) {
|
|
63
|
-
|
|
1515
|
+
if (!this._acceptingOpens) {
|
|
1516
|
+
throw new Error("Program handler is stopping or stopped");
|
|
1517
|
+
}
|
|
1518
|
+
const attachmentParent = options.parent && options.parent !== storeOrAddress
|
|
1519
|
+
? options.parent
|
|
1520
|
+
: undefined;
|
|
1521
|
+
if (attachmentParent) {
|
|
1522
|
+
this.assertParentCanOwnOpen(attachmentParent);
|
|
1523
|
+
this.retainParentAttachment(attachmentParent);
|
|
1524
|
+
}
|
|
1525
|
+
const observedReservations = new Set([...this._openingReservations].filter((reservation) => reservation.phase === "opening"));
|
|
1526
|
+
let completeAdmission;
|
|
1527
|
+
const admission = new Promise((resolve) => {
|
|
1528
|
+
completeAdmission = resolve;
|
|
1529
|
+
});
|
|
1530
|
+
this._openAdmissions.add(admission);
|
|
1531
|
+
try {
|
|
1532
|
+
return await this.openAdmitted(storeOrAddress, options, observedReservations);
|
|
1533
|
+
}
|
|
1534
|
+
finally {
|
|
1535
|
+
if (attachmentParent) {
|
|
1536
|
+
this.releaseParentAttachment(attachmentParent);
|
|
1537
|
+
}
|
|
1538
|
+
this._openAdmissions.delete(admission);
|
|
1539
|
+
completeAdmission();
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
async openAdmitted(storeOrAddress, options = {}, observedReservations = new Set()) {
|
|
1543
|
+
if (typeof storeOrAddress !== "string" &&
|
|
1544
|
+
this._cleanupResiduals.has(storeOrAddress)) {
|
|
1545
|
+
throw new Error(`Program at ${storeOrAddress.address} has pending cleanup residuals and cannot be reopened before cleanup is retried`);
|
|
1546
|
+
}
|
|
1547
|
+
if (options.parent && options.parent !== storeOrAddress) {
|
|
1548
|
+
this.assertParentCanOwnOpen(options.parent);
|
|
1549
|
+
}
|
|
1550
|
+
// Parent opens historically share subprograms by default. Explicit parent
|
|
1551
|
+
// strategies still have to retain their reject/replace semantics.
|
|
1552
|
+
const mergeStrategy = options.parent
|
|
1553
|
+
? (options.existing ?? "reuse")
|
|
1554
|
+
: options.existing;
|
|
1555
|
+
const fn = async (openingReservation) => {
|
|
64
1556
|
// TODO add locks for store lifecycle, e.g. what happens if we try to open and close a store at the same time?
|
|
65
1557
|
let program = storeOrAddress;
|
|
66
1558
|
if (typeof storeOrAddress === "string") {
|
|
67
1559
|
const address = storeOrAddress.toString();
|
|
68
1560
|
try {
|
|
69
|
-
|
|
1561
|
+
this.assertNoFailedInitialization(address);
|
|
1562
|
+
this.assertNoTerminalOperation(address);
|
|
1563
|
+
const existing = this.findMonitoredProgram(address);
|
|
70
1564
|
if (existing) {
|
|
71
1565
|
// Be defensive: stale handles shouldn't be returned from the cache.
|
|
72
1566
|
if (existing.closed) {
|
|
73
|
-
this.items.
|
|
1567
|
+
if (this.items.get(address) === existing) {
|
|
1568
|
+
this.items.delete(address);
|
|
1569
|
+
}
|
|
74
1570
|
}
|
|
75
|
-
else if (
|
|
76
|
-
return existing;
|
|
1571
|
+
else if (mergeStrategy === "reuse") {
|
|
1572
|
+
return this.attachExistingProgram(address, existing, options.parent);
|
|
77
1573
|
}
|
|
78
|
-
else if (
|
|
79
|
-
await existing.
|
|
1574
|
+
else if (mergeStrategy === "replace") {
|
|
1575
|
+
await this.closeForReplacement(address, existing, options.parent);
|
|
80
1576
|
}
|
|
81
1577
|
else {
|
|
82
1578
|
throw new Error(`Program at ${address} is already open`);
|
|
@@ -100,14 +1596,15 @@ export class Handler {
|
|
|
100
1596
|
throw new Error("Parent program can not be equal to the program");
|
|
101
1597
|
}
|
|
102
1598
|
if (!program.closed) {
|
|
103
|
-
|
|
1599
|
+
this.assertNoFailedInitialization(program.address);
|
|
1600
|
+
this.assertNoTerminalOperation(program.address, program);
|
|
1601
|
+
const existing = this.findMonitoredProgram(program.address);
|
|
104
1602
|
if (existing === program) {
|
|
105
|
-
|
|
106
|
-
return program;
|
|
1603
|
+
return this.attachExistingProgram(program.address, existing, options.parent);
|
|
107
1604
|
}
|
|
108
1605
|
else if (existing) {
|
|
109
1606
|
// we got existing, but it is not the same instance
|
|
110
|
-
const existing = await this.checkProcessExisting(program.address, program, options
|
|
1607
|
+
const existing = await this.checkProcessExisting(program.address, program, mergeStrategy, options.parent);
|
|
111
1608
|
if (existing) {
|
|
112
1609
|
return existing;
|
|
113
1610
|
}
|
|
@@ -116,96 +1613,211 @@ export class Handler {
|
|
|
116
1613
|
if (!program.node.identity.publicKey.equals(this.properties.identity.publicKey)) {
|
|
117
1614
|
throw new Error(`Program at ${program.address} is already opened with a different client`);
|
|
118
1615
|
}
|
|
1616
|
+
assertCanAttachParent(program);
|
|
1617
|
+
if (!openingReservation) {
|
|
1618
|
+
throw new Error("Missing opening graph reservation");
|
|
1619
|
+
}
|
|
1620
|
+
resolvedDependencyAddresses = await this.prepareOpeningGraph(program, openingReservation, options.parent, resolvedDependencyAddresses);
|
|
119
1621
|
// assume new instance was not added to monitored items, just add it
|
|
120
1622
|
// and return it as we would opened it normally
|
|
121
1623
|
await program.save(this.properties.client.services.blocks, {
|
|
122
1624
|
skipOnAddress: false,
|
|
123
|
-
|
|
124
|
-
return !this.
|
|
1625
|
+
save: (address) => {
|
|
1626
|
+
return !this.findMonitoredProgram(address, true);
|
|
125
1627
|
},
|
|
126
1628
|
});
|
|
1629
|
+
assertCanAttachParent(program);
|
|
1630
|
+
if (options.parent) {
|
|
1631
|
+
this.assertParentCanOwnOpen(options.parent);
|
|
1632
|
+
}
|
|
1633
|
+
this.monitorTerminalOperations(program.address, program);
|
|
127
1634
|
this.items.set(program.address, program);
|
|
128
1635
|
addParent(program, options.parent);
|
|
129
1636
|
return program;
|
|
130
1637
|
}
|
|
131
1638
|
}
|
|
132
1639
|
}
|
|
1640
|
+
const existingBeforeSave = await this.checkProcessExisting(program.address, program, mergeStrategy, options.parent);
|
|
1641
|
+
if (existingBeforeSave) {
|
|
1642
|
+
return existingBeforeSave;
|
|
1643
|
+
}
|
|
1644
|
+
if (!openingReservation) {
|
|
1645
|
+
throw new Error("Missing opening graph reservation");
|
|
1646
|
+
}
|
|
1647
|
+
resolvedDependencyAddresses = await this.prepareOpeningGraph(program, openingReservation, options.parent, resolvedDependencyAddresses);
|
|
133
1648
|
logger.trace(`Open database '${program.constructor.name}`);
|
|
134
1649
|
// todo make this nicer
|
|
135
1650
|
let address = await program.save(this.properties.client.services.blocks, {
|
|
136
|
-
skipOnAddress:
|
|
137
|
-
|
|
138
|
-
return !this.
|
|
1651
|
+
skipOnAddress: !resolvedWithoutSaving,
|
|
1652
|
+
save: (address) => {
|
|
1653
|
+
return !this.findMonitoredProgram(address, true);
|
|
139
1654
|
},
|
|
140
1655
|
});
|
|
141
|
-
const existing = await this.checkProcessExisting(address, program, options
|
|
1656
|
+
const existing = await this.checkProcessExisting(address, program, mergeStrategy, options.parent);
|
|
142
1657
|
if (existing) {
|
|
143
1658
|
return existing;
|
|
144
1659
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
1660
|
+
const rollbackState = {
|
|
1661
|
+
parents: program.parents ? [...program.parents] : undefined,
|
|
1662
|
+
children: program.children ? [...program.children] : undefined,
|
|
1663
|
+
parent: options.parent,
|
|
1664
|
+
parentChildReferences: options.parent?.children?.filter((child) => child === program)
|
|
1665
|
+
.length ?? 0,
|
|
1666
|
+
};
|
|
1667
|
+
this.monitorTerminalOperations(address, program);
|
|
1668
|
+
try {
|
|
1669
|
+
const { onBeforeOpen: userOnBeforeOpen, onOpen: userOnOpen, onClose: userOnClose, onDrop: userOnDrop, ...programOptions } = options;
|
|
1670
|
+
if (options.parent) {
|
|
1671
|
+
this.assertParentCanOwnOpen(options.parent);
|
|
1672
|
+
}
|
|
1673
|
+
await this.invokeLifecycleMethod(() => program.beforeOpen(this.properties.client, {
|
|
1674
|
+
...programOptions,
|
|
1675
|
+
onBeforeOpen: async (p) => {
|
|
1676
|
+
if (this.properties.shouldMonitor(program)) {
|
|
1677
|
+
this.items.set(address, program);
|
|
1678
|
+
}
|
|
1679
|
+
if (userOnBeforeOpen) {
|
|
1680
|
+
this._openLifecycleCallbacks += 1;
|
|
1681
|
+
try {
|
|
1682
|
+
await userOnBeforeOpen(p);
|
|
1683
|
+
}
|
|
1684
|
+
finally {
|
|
1685
|
+
this._openLifecycleCallbacks -= 1;
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
},
|
|
1689
|
+
onOpen: async (p) => {
|
|
1690
|
+
if (!userOnOpen)
|
|
1691
|
+
return;
|
|
1692
|
+
this._openLifecycleCallbacks += 1;
|
|
1693
|
+
try {
|
|
1694
|
+
await userOnOpen(p);
|
|
1695
|
+
}
|
|
1696
|
+
finally {
|
|
1697
|
+
this._openLifecycleCallbacks -= 1;
|
|
1698
|
+
}
|
|
1699
|
+
},
|
|
1700
|
+
onClose: async (p) => {
|
|
1701
|
+
if (this.properties.shouldMonitor(p)) {
|
|
1702
|
+
this._onProgamClose(p); // TODO types
|
|
1703
|
+
}
|
|
1704
|
+
if (userOnClose) {
|
|
1705
|
+
this._terminalLifecycleCallbacks += 1;
|
|
1706
|
+
try {
|
|
1707
|
+
await userOnClose(p);
|
|
1708
|
+
}
|
|
1709
|
+
finally {
|
|
1710
|
+
this._terminalLifecycleCallbacks -= 1;
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
},
|
|
1714
|
+
onDrop: async (p) => {
|
|
1715
|
+
if (this.properties.shouldMonitor(p)) {
|
|
1716
|
+
this._onProgamClose(p);
|
|
1717
|
+
}
|
|
1718
|
+
if (userOnDrop) {
|
|
1719
|
+
this._terminalLifecycleCallbacks += 1;
|
|
1720
|
+
try {
|
|
1721
|
+
await userOnDrop(p);
|
|
1722
|
+
}
|
|
1723
|
+
finally {
|
|
1724
|
+
this._terminalLifecycleCallbacks -= 1;
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
},
|
|
1728
|
+
// If the program opens more programs
|
|
1729
|
+
// reset: options.reset,
|
|
1730
|
+
}));
|
|
1731
|
+
await this.invokeLifecycleMethod(() => program.open(options.args));
|
|
1732
|
+
await this.invokeLifecycleMethod(() => program.afterOpen());
|
|
1733
|
+
return program;
|
|
1734
|
+
}
|
|
1735
|
+
catch (error) {
|
|
1736
|
+
try {
|
|
1737
|
+
if (openingReservation) {
|
|
1738
|
+
openingReservation.phase = "rollback";
|
|
149
1739
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
1740
|
+
const rollbackPrograms = new Set([
|
|
1741
|
+
program,
|
|
1742
|
+
...(resolvedDependencyAddresses?.keys() ?? []),
|
|
1743
|
+
]);
|
|
1744
|
+
for (const rollbackProgram of rollbackPrograms) {
|
|
1745
|
+
this._initializationRollbacks.add(rollbackProgram);
|
|
154
1746
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (this.properties.shouldMonitor(p)) {
|
|
158
|
-
return this._onProgamClose(p);
|
|
1747
|
+
try {
|
|
1748
|
+
await this.rollbackFailedInitialization(address, program, rollbackState);
|
|
159
1749
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
1750
|
+
finally {
|
|
1751
|
+
for (const rollbackProgram of rollbackPrograms) {
|
|
1752
|
+
this._initializationRollbacks.delete(rollbackProgram);
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
}
|
|
1756
|
+
catch (cleanupError) {
|
|
1757
|
+
logger.error(`Failed to roll back program at ${address}: ${String(cleanupError)}`);
|
|
1758
|
+
}
|
|
1759
|
+
throw error;
|
|
1760
|
+
}
|
|
168
1761
|
};
|
|
169
1762
|
// Helper to resolve address from storeOrAddress
|
|
1763
|
+
let resolvedWithoutSaving = false;
|
|
1764
|
+
let resolvedDependencyAddresses;
|
|
170
1765
|
const resolveAddress = async () => {
|
|
171
1766
|
if (typeof storeOrAddress === "string") {
|
|
172
1767
|
return storeOrAddress;
|
|
173
1768
|
}
|
|
174
|
-
|
|
175
|
-
|
|
1769
|
+
resolvedWithoutSaving = storeOrAddress.closed;
|
|
1770
|
+
resolvedDependencyAddresses =
|
|
1771
|
+
await this.resolveDependencyAddresses(storeOrAddress);
|
|
1772
|
+
const address = resolvedDependencyAddresses.get(storeOrAddress);
|
|
1773
|
+
if (!address) {
|
|
1774
|
+
throw new Error("Failed to resolve the root program address");
|
|
176
1775
|
}
|
|
177
|
-
return
|
|
178
|
-
skipOnAddress: true,
|
|
179
|
-
condition: (addr) => !this.items.has(addr.toString()),
|
|
180
|
-
});
|
|
1776
|
+
return address;
|
|
181
1777
|
};
|
|
182
1778
|
const address = await resolveAddress();
|
|
1779
|
+
await this.waitForOpeningReservations(address, typeof storeOrAddress === "string" ? undefined : storeOrAddress, options.parent, observedReservations);
|
|
1780
|
+
if (!this._openingPromises.has(address)) {
|
|
1781
|
+
this.assertNoFailedInitialization(address);
|
|
1782
|
+
this.assertNoTerminalOperation(address, typeof storeOrAddress === "string" ? undefined : storeOrAddress);
|
|
1783
|
+
}
|
|
183
1784
|
// For parent opens, check if already opened or in-progress and return early
|
|
184
1785
|
// This prevents race conditions while avoiding deadlocks
|
|
185
1786
|
if (options?.parent) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
this.
|
|
1787
|
+
while (true) {
|
|
1788
|
+
// Check if there's an open in progress FIRST - wait for it. This
|
|
1789
|
+
// must precede items because beforeOpen() adds to items before open()
|
|
1790
|
+
// completes.
|
|
1791
|
+
const existingPromise = this._openingPromises.get(address);
|
|
1792
|
+
if (existingPromise) {
|
|
1793
|
+
const requestedProgram = typeof storeOrAddress === "string" ? undefined : storeOrAddress;
|
|
1794
|
+
this.adoptOpeningReservation(existingPromise, options.parent);
|
|
1795
|
+
if (this.isReservedNestedUse(address, requestedProgram, options.parent)) {
|
|
1796
|
+
const existing = await this.processParentExisting(address, requestedProgram, mergeStrategy ?? "reuse", options.parent);
|
|
1797
|
+
if (existing)
|
|
1798
|
+
return existing;
|
|
1799
|
+
}
|
|
1800
|
+
try {
|
|
1801
|
+
await existingPromise;
|
|
1802
|
+
}
|
|
1803
|
+
catch {
|
|
1804
|
+
// The failed generation has completed its identity-safe rollback.
|
|
1805
|
+
// This admitted request still owns its independent open semantics.
|
|
1806
|
+
}
|
|
1807
|
+
continue;
|
|
1808
|
+
}
|
|
1809
|
+
const existing = await this.processParentExisting(address, typeof storeOrAddress === "string" ? undefined : storeOrAddress, mergeStrategy ?? "reuse", options.parent);
|
|
1810
|
+
if (existing) {
|
|
1811
|
+
return existing;
|
|
1812
|
+
}
|
|
1813
|
+
// processParentExisting() can await replacement and even its empty
|
|
1814
|
+
// async path yields once. Re-read both gates after that yield. With no
|
|
1815
|
+
// await between this final check and trackOpening(), registering a new
|
|
1816
|
+
// parent generation is an atomic singleflight claim on this JS turn.
|
|
1817
|
+
if (this._openingPromises.has(address) || this.items.has(address)) {
|
|
1818
|
+
continue;
|
|
1819
|
+
}
|
|
1820
|
+
return this.trackOpening(address, (reservation) => fn(reservation), observedReservations);
|
|
209
1821
|
}
|
|
210
1822
|
}
|
|
211
1823
|
// Non-parent opens use queue for serialization
|
|
@@ -214,7 +1826,43 @@ export class Handler {
|
|
|
214
1826
|
queue = new PQueue({ concurrency: 1 });
|
|
215
1827
|
this._openQueue.set(address, queue);
|
|
216
1828
|
}
|
|
217
|
-
return queue.add(
|
|
1829
|
+
return queue.add(async () => {
|
|
1830
|
+
while (true) {
|
|
1831
|
+
// Parent opens bypass the queue to avoid nested-open deadlocks. Wait for
|
|
1832
|
+
// such a generation here before applying this root open's own
|
|
1833
|
+
// reject/reuse/replace semantics. Loop so a newer parent generation that
|
|
1834
|
+
// claimed the address while this waiter resumed is also observed.
|
|
1835
|
+
const existingPromise = this._openingPromises.get(address);
|
|
1836
|
+
if (existingPromise) {
|
|
1837
|
+
try {
|
|
1838
|
+
await existingPromise;
|
|
1839
|
+
}
|
|
1840
|
+
catch {
|
|
1841
|
+
// Re-read Handler state and apply this root request's own strategy
|
|
1842
|
+
// after the failed generation has rolled back.
|
|
1843
|
+
}
|
|
1844
|
+
continue;
|
|
1845
|
+
}
|
|
1846
|
+
// Existing live non-replacement operations finish from Handler state
|
|
1847
|
+
// and do not represent a new initialization generation. In particular,
|
|
1848
|
+
// don't make a concurrent parent inherit a duplicate-open rejection. A
|
|
1849
|
+
// stale closed cache entry, however, is evicted before fn() starts the
|
|
1850
|
+
// new generation visible to parent opens.
|
|
1851
|
+
const existing = this.findMonitoredProgram(address);
|
|
1852
|
+
if (existing?.closed) {
|
|
1853
|
+
this.assertNoTerminalOperation(address, typeof storeOrAddress === "string" ? undefined : storeOrAddress);
|
|
1854
|
+
if (this.items.get(address) === existing) {
|
|
1855
|
+
this.items.delete(address);
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
else if (existing && mergeStrategy !== "replace") {
|
|
1859
|
+
return fn();
|
|
1860
|
+
}
|
|
1861
|
+
// No await separates the empty gate from registration, so a parent
|
|
1862
|
+
// open cannot claim this address in between.
|
|
1863
|
+
return this.trackOpening(address, (reservation) => fn(reservation), observedReservations);
|
|
1864
|
+
}
|
|
1865
|
+
});
|
|
218
1866
|
}
|
|
219
1867
|
}
|
|
220
1868
|
//# sourceMappingURL=handler.js.map
|