@remix_labs/machine-starter 2.12935.0-dev → 2.12939.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
@@ -12,6 +12,7 @@ let children = {};
12
12
  let ffiLoops = {};
13
13
  let blobs = {};
14
14
  let blobsLoops = {};
15
+ let coordination = {};
15
16
 
16
17
  function terminateAll() {
17
18
  console.log("[machine-starter] terminate all VMs");
@@ -37,51 +38,66 @@ function terminateAll() {
37
38
  blobsLoops = {};
38
39
  }
39
40
 
40
- function terminateVM(vmID) {
41
+ async function terminateVM(vmID) {
41
42
  console.log("[machine-starter] terminate VM", vmID);
42
- let worker = workers[vmID];
43
- if (worker) {
44
- let vmChildren = children[vmID];
45
- let blobList = blobs[vmID] || [];
46
- let ffiLoop = ffiLoops[vmID];
47
- let blobsLoop = blobsLoops[vmID];
48
- delete children[vmID];
49
- delete ffiLoops[vmID];
50
- delete blobsLoops[vmID];
51
- killVM(vmID);
52
- ffiLoop.terminate();
53
- if (blobsLoop) {
54
- blobsLoop.terminate();
55
- }
56
- for (let blob_url of blobList) {
57
- console.log("[machine-starter] revoke ", blob_url);
58
- URL.revokeObjectURL(blob_url);
59
- };
60
- if (vmChildren) {
43
+ let coord = coordination[vmID];
44
+ if (coord) {
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
+ worker.terminate();
80
+ // go on with sub VMs
61
81
  for (let subID of vmChildren) {
62
- terminateVM(subID)
82
+ terminateVM(subID) // do not wait
63
83
  }
64
- }
65
- }
66
- }
67
-
68
- function killVM(vmID) {
69
- let worker = workers[vmID];
70
- let topic = worker_topics[vmID];
71
- delete workers[vmID];
72
- delete worker_topics[vmID];
73
- if (topic && last_hub) {
74
- let payload = { _rmx_type:"msg_vm_register", id: vmID, alive: false };
75
- last_hub.newChannel().then(channel => {
76
- let msg = channel.newJSONMessage("msg_vm_register", "vm", payload);
77
- channel.publish(topic + "/announce", msg, true).then(_ => {
78
- channel.disconnect("vm_" + vmID);
79
- })
80
- }).then(() => {
81
- worker.terminate()
82
84
  });
83
- } else {
84
- worker.terminate()
85
+ // if termination is already in progress, just wait for completion
86
+ while (coord.term_promise) {
87
+ await coord.term_promise;
88
+ return; // that's it!
89
+ }
90
+ let term_done = (() => null);
91
+ coord.term_promise = new Promise((succeed, reject) => term_done = succeed);
92
+ // if start-up is still in progress, wait for completion
93
+ if (coord.start_promise) {
94
+ await coord.start_promise;
95
+ }
96
+ // now: do it!
97
+ await do_it();
98
+ coord.term_promise = null;
99
+ if (!coord.will_start) delete coordination[vmID];
100
+ term_done();
85
101
  }
86
102
  }
87
103
 
@@ -137,84 +153,110 @@ async function StartWASM2(hub, config, getWorker) {
137
153
  console.log("[machine-starter] Call MixExplain() for explaining debug levels, and MixSetMask(m) for changing these")
138
154
  };
139
155
  last_hub = hub;
140
- let channel = await hub.newChannel("vm_" + config.vmID);
141
- // injected by either index.js or node.js
142
- let worker = await getWorker();
143
- if (workers[config.vmID] || ffiLoops[config.vmID])
144
- throw new Error("Cannot start VM, the ID is used: " + config.vmID);
145
- workers[config.vmID] = worker;
146
- if (config.topicPrefix != undefined && config.topicPrefix != "") {
147
- worker_topics[config.vmID] = config.topicPrefix + "/vm/" + config.vmID;
148
- } else {
149
- worker_topics[config.vmID] = "/org/" + config.org + "/ws/" + config.workspace + "/vm/" + config.vmID;
156
+ // get thr coord object - perhaps the ID was already used for a VM
157
+ let coord = coordination[config.vmID];
158
+ if (!coord) {
159
+ coord = { start_promise: null, term_promise: null, will_start: false };
160
+ coordination[config.vmID] = coord;
161
+ }
162
+ // if there is already a worker with the same ID, check whether it is
163
+ // already terminating. If so, wait for completion.
164
+ coord.will_start = true;
165
+ while (coord.term_promise) {
166
+ if (initialMask & DEBUG_STATE) {
167
+ console.debug("[machine-starter] waiting for termination of VM: " + config.vmID);
168
+ };
169
+ await coord.term_promise;
150
170
  }
151
- let starterFFIs = config.localFFIs || {};
152
- // from now on, the "local" FFIs are called "starter" FFIs, because this
153
- // distinguishes these from other locally defined FFIs
154
- let mixcore = await createStarterMixcore(config.mixcore);
155
- if (mixcore) {
156
- // merge the ffis and store them back into the config so sub vms also get them
157
- config.localFFIs = Object.assign(starterFFIs, await mixcoreStarterFFIs(mixcore));
158
- // delete the mixcore config to avoid being handled further by the worker
159
- delete config.mixcore;
171
+ // if already starting up, this is an error.
172
+ if (coord.start_promise || workers[config.vmID]) {
173
+ throw new Error("[machine-starter] Cannot start VM, the ID is used: " + config.vmID);
160
174
  }
161
- let starterFFIkind = {};
162
- let starterFFIfuns = {};
163
- for (const [k, v] of Object.entries(starterFFIs)) {
164
- starterFFIfuns[k] = {...v};
165
- if (v.isRaw && (v.canFail || v.useJsonDecoder)) {
166
- console.error("A raw, starter FFI must neither be failing nor use the JSON decoder: " + k);
175
+ // set to starting:
176
+ coord.will_start = false;
177
+ let start_done = (() => null);
178
+ coord.start_promise = new Promise((succeed,fail) => start_done = succeed);
179
+ try {
180
+ let channel = await hub.newChannel("vm_" + config.vmID);
181
+ // injected by either index.js or node.js
182
+ let worker = await getWorker();
183
+ workers[config.vmID] = worker;
184
+ if (config.topicPrefix != undefined && config.topicPrefix != "") {
185
+ worker_topics[config.vmID] = config.topicPrefix + "/vm/" + config.vmID;
167
186
  } else {
168
- starterFFIkind[k] = {...v};
169
- delete starterFFIkind[k].run;
187
+ worker_topics[config.vmID] = "/org/" + config.org + "/ws/" + config.workspace + "/vm/" + config.vmID;
170
188
  }
171
- };
172
- let vm_kind = { isRaw:false, canFail:false, useJsonDecoder:false};
173
- starterFFIkind["$vm_start"] = vm_kind;
174
- starterFFIfuns["$vm_start"] = (conn, args) => ffi_vm_start(conn, args, getWorker);
175
- starterFFIkind["$vm_stop"] = vm_kind;
176
- starterFFIfuns["$vm_stop"] = ffi_vm_stop;
177
- let mixrtCode = config.mixrtCode;
178
- if (typeof(mixrtCode) == "string" && mixrtCode.startsWith("remix://")) {
179
- // for the desktop only: precompile the mixrt module to get faster
180
- // startup times
181
- if (globalThis.mixrtCodeModule && globalThis.mixrtCodeURL == mixrtCode) {
182
- console.log("[machine-starter] reusing mixrt");
183
- mixrtCode = globalThis.mixrtCodeModule;
184
- } else {
185
- let url = mixrtCode;
186
- console.log("[machine-starter] pre-compiling mixrt");
187
- mixrtCode = await WebAssembly.compileStreaming(fetch(url));
188
- globalThis.mixrtCodeURL = url;
189
- globalThis.mixrtCodeModule = mixrtCode;
189
+ let starterFFIs = config.localFFIs || {};
190
+ // from now on, the "local" FFIs are called "starter" FFIs, because this
191
+ // distinguishes these from other locally defined FFIs
192
+ let mixcore = await createStarterMixcore(config.mixcore);
193
+ if (mixcore) {
194
+ // merge the ffis and store them back into the config so sub vms also get them
195
+ config.localFFIs = Object.assign(starterFFIs, await mixcoreStarterFFIs(mixcore));
196
+ // delete the mixcore config to avoid being handled further by the worker
197
+ delete config.mixcore;
190
198
  }
191
- };
192
- ffiLoops[config.vmID] = setupStarterFFIs(hub, config, starterFFIfuns);
193
- blobsLoops[config.vmID] = setupBlobs(hub, config);
194
- let config_msg =
195
- { "_rmx_type": "msg_vm_configure",
196
- "baseURL": config.baseURL,
197
- "org": config.org,
198
- "workspace": config.workspace,
199
- "topicPrefix": config.topicPrefix,
200
- "vmID": config.vmID,
201
- "user": config.user,
202
- "token": config.token,
203
- "hub": channel.port,
204
- "outputViaMQTT": !(config.noOutputViaMQTT),
205
- "machType": config.machType,
206
- "debugMask": initialMask,
207
- "starterFFIs": starterFFIkind,
208
- "allowInsecureHttp": globalThis.GROOVEBOX_ALLOW_INSECURE_HTTP,
209
- "interceptFFI": config.interceptFFI,
210
- "interceptDynloadFFI": config.interceptDynloadFFI,
211
- "mixcore": config.mixcore,
212
- "mixrtCode": mixrtCode
213
- // can be: base64-encoded string, Uint8Array, or a URL as string
214
- // (http, https, data)
199
+ let starterFFIkind = {};
200
+ let starterFFIfuns = {};
201
+ for (const [k, v] of Object.entries(starterFFIs)) {
202
+ starterFFIfuns[k] = {...v};
203
+ if (v.isRaw && (v.canFail || v.useJsonDecoder)) {
204
+ console.error("A raw, starter FFI must neither be failing nor use the JSON decoder: " + k);
205
+ } else {
206
+ starterFFIkind[k] = {...v};
207
+ delete starterFFIkind[k].run;
208
+ }
215
209
  };
216
- worker.postMessage(config_msg, [ channel.port ]);
217
- return worker;
210
+ let vm_kind = { isRaw:false, canFail:false, useJsonDecoder:false};
211
+ starterFFIkind["$vm_start"] = vm_kind;
212
+ starterFFIfuns["$vm_start"] = (conn, args) => ffi_vm_start(conn, args, getWorker);
213
+ starterFFIkind["$vm_stop"] = vm_kind;
214
+ starterFFIfuns["$vm_stop"] = ffi_vm_stop;
215
+ let mixrtCode = config.mixrtCode;
216
+ if (typeof(mixrtCode) == "string" && mixrtCode.startsWith("remix://")) {
217
+ // for the desktop only: precompile the mixrt module to get faster
218
+ // startup times
219
+ if (globalThis.mixrtCodeModule && globalThis.mixrtCodeURL == mixrtCode) {
220
+ console.log("[machine-starter] reusing mixrt");
221
+ mixrtCode = globalThis.mixrtCodeModule;
222
+ } else {
223
+ let url = mixrtCode;
224
+ console.log("[machine-starter] pre-compiling mixrt");
225
+ mixrtCode = await WebAssembly.compileStreaming(fetch(url));
226
+ globalThis.mixrtCodeURL = url;
227
+ globalThis.mixrtCodeModule = mixrtCode;
228
+ }
229
+ };
230
+ ffiLoops[config.vmID] = setupStarterFFIs(hub, config, starterFFIfuns);
231
+ blobsLoops[config.vmID] = setupBlobs(hub, config);
232
+ let config_msg =
233
+ { "_rmx_type": "msg_vm_configure",
234
+ "baseURL": config.baseURL,
235
+ "org": config.org,
236
+ "workspace": config.workspace,
237
+ "topicPrefix": config.topicPrefix,
238
+ "vmID": config.vmID,
239
+ "user": config.user,
240
+ "token": config.token,
241
+ "hub": channel.port,
242
+ "outputViaMQTT": !(config.noOutputViaMQTT),
243
+ "machType": config.machType,
244
+ "debugMask": initialMask,
245
+ "starterFFIs": starterFFIkind,
246
+ "allowInsecureHttp": globalThis.GROOVEBOX_ALLOW_INSECURE_HTTP,
247
+ "interceptFFI": config.interceptFFI,
248
+ "interceptDynloadFFI": config.interceptDynloadFFI,
249
+ "mixcore": config.mixcore,
250
+ "mixrtCode": mixrtCode
251
+ // can be: base64-encoded string, Uint8Array, or a URL as string
252
+ // (http, https, data)
253
+ };
254
+ worker.postMessage(config_msg, [ channel.port ]);
255
+ return worker;
256
+ } finally {
257
+ coord.start_promise = null;
258
+ start_done();
259
+ }
218
260
  }
219
261
 
220
262
  async function mixcoreStarterFFIs(mixcore) {
@@ -1,2 +1,2 @@
1
- var GROOVEBOX_BUILD = "12935";
1
+ var GROOVEBOX_BUILD = "12939";
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.12935.0-dev",
3
+ "version": "2.12939.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.12935.0-dev",
14
+ "@remix_labs/hub-client": "2.12939.0-dev",
15
15
  "nanoid": "^5.0.2",
16
16
  "web-worker": "^1.2.0"
17
17
  },