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