Avoid plug loading race condition

pull/599/head
Zef Hemel 2023-12-10 15:18:34 +01:00
parent 9921d72b96
commit 0475c4c40e
2 changed files with 9 additions and 3 deletions

View File

@ -23,6 +23,7 @@ export class KVPrimitivesManifestCache<T> implements ManifestCache<T> {
const manifest = plug.sandbox.manifest!; const manifest = plug.sandbox.manifest!;
await this.kv.batchSet([{ await this.kv.batchSet([{
key: [this.manifestPrefix, plug.name], key: [this.manifestPrefix, plug.name],
// Deliverately removing the assets from the manifest to preserve space, will be re-added upon load of actual worker
value: { manifest: { ...manifest, assets: undefined }, hash }, value: { manifest: { ...manifest, assets: undefined }, hash },
}]); }]);
return manifest; return manifest;
@ -43,7 +44,11 @@ export class InMemoryManifestCache<T> implements ManifestCache<T> {
} }
await plug.sandbox.init(); await plug.sandbox.init();
const manifest = plug.sandbox.manifest!; const manifest = plug.sandbox.manifest!;
this.cache.set(plug.name!, { manifest, hash }); // Deliverately removing the assets from the manifest to preserve space, will be re-added upon load of actual worker
this.cache.set(plug.name!, {
manifest: { ...manifest, assets: undefined },
hash,
});
return manifest; return manifest;
} }
} }

View File

@ -32,8 +32,9 @@ export class Sandbox<HookT> {
init(): Promise<void> { init(): Promise<void> {
console.log("Booting up worker for", this.plug.name); console.log("Booting up worker for", this.plug.name);
if (this.worker) { if (this.worker) {
// Should not happen // Race condition
console.warn("Double init of sandbox"); console.warn("Double init of sandbox, ignoring");
return Promise.resolve();
} }
this.worker = new Worker(this.plug.workerUrl, { this.worker = new Worker(this.plug.workerUrl, {
...this.workerOptions, ...this.workerOptions,