@remix_labs/machine-starter 2.12970.0-dev → 2.12974.0-dev

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/common.js CHANGED
@@ -14,7 +14,24 @@ let blobs = {};
14
14
  let blobsLoops = {};
15
15
  let coordination = {};
16
16
 
17
+ class TaskQueue {
18
+ constructor() {
19
+ this.tail = Promise.resolve();
20
+ this.length = 0;
21
+ }
22
+ run(fn) { // fn shall return a promise. run also returns a promise
23
+ length++;
24
+ const result = this.tail.then(() => fn());
25
+ this.tail = result.catch(() => {});
26
+ length--;
27
+ return result;
28
+ }
29
+ }
30
+
17
31
  function terminateAllOnExit() {
32
+ // this is a simplified termination function that kills all VMs, but
33
+ // doesn't clean everything properly up. Use this only for the "on-exit"
34
+ // callback of node.
18
35
  console.log("[machine-starter] terminate all VMs");
19
36
  for (let [vmID, worker] of Object.entries(workers)) {
20
37
  worker.terminate();
@@ -32,75 +49,69 @@ function terminateAllOnExit() {
32
49
  blobsLoop.terminate();
33
50
  }
34
51
  workers = {};
52
+ worker_topics = {};
35
53
  children = {};
36
54
  ffiLoops = {};
37
55
  blobs = {};
38
56
  blobsLoops = {};
57
+ coordination = {};
39
58
  }
40
59
 
41
- async function terminateVM(vmID) {
60
+ function getCoord(vmID) {
42
61
  let coord = coordination[vmID];
43
- if (coord) {
62
+ if (!coord) {
63
+ coord = { queue: new TaskQueue() };
64
+ coordination[vmID] = coord;
65
+ }
66
+ return coord;
67
+ }
68
+
69
+ async function terminateVM(vmID) {
70
+ let coord = getCoord(vmID);
71
+ await coord.queue.run(async () => {
44
72
  console.log("[machine-starter] terminate VM:", vmID);
45
- const do_it = (async () => {
46
- // make the VM first unknown to the outside and disconnect it,
47
- // then clean up resources (including stopping the VM)
48
- let channel = await last_hub.newChannel();
49
- let topic = worker_topics[vmID];
50
- let payload = { _rmx_type:"msg_vm_register", id: vmID, alive: false };
51
- let msg = channel.newJSONMessage("msg_vm_register", "vm", payload);
52
- await channel.publish(topic + "/announce", msg, true);
53
- // NB. This msg is a leak - it will never be removed.
54
- await channel.disconnect("vm_" + vmID);
55
- // now the VM is isolated and cannot communicate with the
56
- // hub anymore
57
- let worker = workers[vmID];
58
- let vmChildren = children[vmID] || [];
59
- let blobList = blobs[vmID] || [];
60
- let ffiLoop = ffiLoops[vmID];
61
- let blobsLoop = blobsLoops[vmID];
62
- delete children[vmID];
63
- delete ffiLoops[vmID];
64
- delete blobsLoops[vmID];
65
- delete worker_topics[vmID];
66
- delete workers[vmID];
67
- // terminate our endpoints of communication
68
- if (ffiLoop) {
69
- ffiLoop.terminate();
70
- }
71
- if (blobsLoop) {
72
- blobsLoop.terminate();
73
- }
74
- for (let blob_url of blobList) {
75
- console.log("[machine-starter] revoke ", blob_url);
76
- URL.revokeObjectURL(blob_url);
77
- }
78
- // kill the worker:
79
- console.log("[machine-starter] terminate web worker for VM:", vmID);
80
- worker.terminate();
81
- // go on with sub VMs
82
- for (let subID of vmChildren) {
83
- terminateVM(subID) // do not wait
84
- }
85
- });
86
- // if termination is already in progress, just wait for completion
87
- while (coord.term_promise) {
88
- console.log("[machine-starter] termination already in progress, waiting until completed:", vmID);
89
- await coord.term_promise;
90
- return; // that's it!
73
+ // make the VM first unknown to the outside and disconnect it,
74
+ // then clean up resources (including stopping the VM)
75
+ let channel = await last_hub.newChannel();
76
+ let topic = worker_topics[vmID];
77
+ let payload = { _rmx_type:"msg_vm_register", id: vmID, alive: false };
78
+ let msg = channel.newJSONMessage("msg_vm_register", "vm", payload);
79
+ await channel.publish(topic + "/announce", msg, true);
80
+ // NB. This msg is a leak - it will never be removed.
81
+ await channel.disconnect("vm_" + vmID);
82
+ // now the VM is isolated and cannot communicate with the
83
+ // hub anymore
84
+ let worker = workers[vmID];
85
+ let vmChildren = children[vmID] || [];
86
+ let blobList = blobs[vmID] || [];
87
+ let ffiLoop = ffiLoops[vmID];
88
+ let blobsLoop = blobsLoops[vmID];
89
+ delete children[vmID];
90
+ delete ffiLoops[vmID];
91
+ delete blobsLoops[vmID];
92
+ delete worker_topics[vmID];
93
+ delete workers[vmID];
94
+ // terminate our endpoints of communication
95
+ if (ffiLoop) {
96
+ ffiLoop.terminate();
91
97
  }
92
- let term_done = (() => null);
93
- coord.term_promise = new Promise((succeed, reject) => term_done = succeed);
94
- // if start-up is still in progress, wait for completion
95
- if (coord.start_promise) {
96
- console.log("[machine-starter] termination during start-up, waiting until completed:", vmID);
97
- await coord.start_promise;
98
+ if (blobsLoop) {
99
+ blobsLoop.terminate();
100
+ }
101
+ for (let blob_url of blobList) {
102
+ console.log("[machine-starter] revoke ", blob_url);
103
+ URL.revokeObjectURL(blob_url);
104
+ }
105
+ // kill the worker:
106
+ console.log("[machine-starter] terminate web worker for VM:", vmID);
107
+ worker.terminate();
108
+ // go on with sub VMs
109
+ for (let subID of vmChildren) {
110
+ terminateVM(subID) // do not wait
98
111
  }
99
- // now: do it!
100
- await do_it();
101
- coord.term_promise = null;
102
- if (!coord.will_start) delete coordination[vmID];
103
- term_done();
112
+ });
113
+ if (coord.queue.length == 0) {
114
+ delete coordination[vmID];
104
115
  }
105
116
  }
106
117
 
@@ -157,29 +168,8 @@ async function StartWASM2(hub, config, getWorker) {
157
168
  };
158
169
  last_hub = hub;
159
170
  // get thr coord object - perhaps the ID was already used for a VM
160
- let coord = coordination[config.vmID];
161
- if (!coord) {
162
- coord = { start_promise: null, term_promise: null, will_start: false };
163
- coordination[config.vmID] = coord;
164
- }
165
- // if there is already a worker with the same ID, check whether it is
166
- // already terminating. If so, wait for completion.
167
- coord.will_start = true;
168
- while (coord.term_promise) {
169
- if (initialMask & DEBUG_STATE) {
170
- console.debug("[machine-starter] waiting for termination of VM: " + config.vmID);
171
- };
172
- await coord.term_promise;
173
- }
174
- // if already starting up, this is an error.
175
- if (coord.start_promise || workers[config.vmID]) {
176
- throw new Error("[machine-starter] Cannot start VM, the ID is used: " + config.vmID);
177
- }
178
- // set to starting:
179
- coord.will_start = false;
180
- let start_done = (() => null);
181
- coord.start_promise = new Promise((succeed,fail) => start_done = succeed);
182
- try {
171
+ let coord = getCoord(vmID);
172
+ await coord.queue.run(async () => {
183
173
  let channel = await hub.newChannel("vm_" + config.vmID);
184
174
  // injected by either index.js or node.js
185
175
  let worker = await getWorker();
@@ -256,10 +246,7 @@ async function StartWASM2(hub, config, getWorker) {
256
246
  };
257
247
  worker.postMessage(config_msg, [ channel.port ]);
258
248
  return worker;
259
- } finally {
260
- coord.start_promise = null;
261
- start_done();
262
- }
249
+ });
263
250
  }
264
251
 
265
252
  async function mixcoreStarterFFIs(mixcore) {
@@ -445,7 +432,7 @@ async function starterFFIsMessageLoop(hub, config, starterFFIs, terminate) {
445
432
  let args = decode(msg.payload.args);
446
433
  let fun = starterFFIs[name];
447
434
  if (initialMask & DEBUG_STATE)
448
- console.debug("[groovebox-starter] starter FFI call", vmID, call_id, name);
435
+ console.debug("[machine-starter] starter FFI call", vmID, call_id, name);
449
436
  try {
450
437
  if (!fun) throw new Error("No such starter FFI in groovebox starter: " + name);
451
438
  let connector =
@@ -459,21 +446,21 @@ async function starterFFIsMessageLoop(hub, config, starterFFIs, terminate) {
459
446
  if (r instanceof Promise) {
460
447
  // r.catch(_ => null); // prevent "unhandled rejection"
461
448
  if (initialMask & DEBUG_STATE)
462
- console.debug("[groovebox-starter] running FFI call asynchronously", vmID, call_id, name);
449
+ console.debug("[machine-starter] running FFI call asynchronously", vmID, call_id, name);
463
450
  await comm.later(call_id);
464
451
  let value = await r;
465
452
  if (initialMask & DEBUG_STATE)
466
- console.debug("[groovebox-starter] async FFI call returned", vmID, call_id, name);
453
+ console.debug("[machine-starter] async FFI call returned", vmID, call_id, name);
467
454
  await comm.returnOrFail(fun, call_id, value);
468
455
  } else {
469
456
  if (initialMask & DEBUG_STATE)
470
- console.debug("[groovebox-starter] sync FFI call returned", vmID, call_id, name);
457
+ console.debug("[machine-starter] sync FFI call returned", vmID, call_id, name);
471
458
  await comm.returnOrFail(fun, call_id, r);
472
459
  }
473
460
  } catch (reason) {
474
461
  if (initialMask & DEBUG_STATE) {
475
- console.debug("[groovebox-starter] FFI call exception", vmID, call_id, name);
476
- console.debug("[groovebox-starter] exception", reason);
462
+ console.debug("[machine-starter] FFI call exception", vmID, call_id, name);
463
+ console.debug("[machine-starter] exception", reason);
477
464
  };
478
465
  await comm.error(call_id, reason.message, reason.stack);
479
466
  }
@@ -1,2 +1,2 @@
1
- var GROOVEBOX_BUILD = "12970";
1
+ var GROOVEBOX_BUILD = "12974";
2
2
  export { GROOVEBOX_BUILD }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remix_labs/machine-starter",
3
- "version": "2.12970.0-dev",
3
+ "version": "2.12974.0-dev",
4
4
  "description": "start the groove",
5
5
  "main": "node.js",
6
6
  "browser": "index.js",
@@ -11,7 +11,7 @@
11
11
  "author": "Remixlabs staff",
12
12
  "license": "ISC",
13
13
  "dependencies": {
14
- "@remix_labs/hub-client": "2.12970.0-dev",
14
+ "@remix_labs/hub-client": "2.12974.0-dev",
15
15
  "nanoid": "^5.0.2",
16
16
  "web-worker": "^1.2.0"
17
17
  },