Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(cloud-sync): serialize upload writes and finalization

Sebastian B97c18393dc28
diff --git a/server/cloud-sync-concurrent.test.ts b/server/cloud-sync-concurrent.test.ts
index 6ca1582cd0..3459b4f271 100644
--- a/server/cloud-sync-concurrent.test.ts
+++ b/server/cloud-sync-concurrent.test.ts
@@ -10,11 +10,12 @@ import { createCloudSync, type CloudSync } from "./cloud-sync.ts";
const PC = "3f9c2a4e-8b1d-4c6e-9a7f-2d5e8c1b0a93";
const GB = 1024 ** 3;
const sha = (text: string) => createHash("sha256").update(text).digest("hex");
-let home: string, sync: CloudSync, folder: files.SyncFolder;
+let home: string, sync: CloudSync, folder: files.SyncFolder, now: number;
beforeEach(() => {
home = mkdtempSync(join(tmpdir(), "omb-sync-concurrent-"));
- sync = createCloudSync({ dataDir: join(home, "data"), root: join(home, "Synced"), now: () => new Date(2026, 9, 15, 12).getTime(), statfs: () => ({ totalBytes: 10 * GB, freeBytes: 9 * GB }) });
+ now = new Date(2026, 9, 15, 12).getTime();
+ sync = createCloudSync({ dataDir: join(home, "data"), root: join(home, "Synced"), now: () => now, statfs: () => ({ totalBytes: 10 * GB, freeBytes: 9 * GB }) });
folder = sync.register({ computerId: PC, computerName: "PC", pcPath: "/project", name: "Project" });
});
afterEach(() => {
@@ -47,6 +48,101 @@ function holdCurrent() {
return { entered: entered.promise, release: release.resolve };
}
+function holdHash() {
+ const entered = gate(), release = gate();
+ const hash = files.sha256File;
+ vi.spyOn(files, "sha256File").mockImplementationOnce(async (path) => {
+ const result = await hash(path);
+ entered.resolve();
+ await release.promise;
+ return result;
+ });
+ return { entered: entered.promise, release: release.resolve };
+}
+
+it("rechecks a duplicate part's offset after the open writer closes", async () => {
+ const { uploadId } = sync.beginUpload(folder.id, { path: "a.txt", size: 4, sha256: sha("good"), baseSha256: null });
+ const entered = gate(), release = gate();
+ const first = sync.writePart(uploadId, 0, (async function* () { entered.resolve(); await release.promise; yield Buffer.from("good"); })());
+ await entered.promise;
+ const duplicate = Promise.allSettled([sync.writePart(uploadId, 0, (async function* () { yield Buffer.from("evil"); })())]);
+ try { await setImmediate(); } finally { release.resolve(); }
+ expect(await first).toEqual({ received: 4 });
+ expect(await duplicate).toMatchObject([{ status: "rejected", reason: { status: 409, body: { received: 4 } } }]);
+ const receipt = await sync.commit(uploadId);
+ expect(readFileSync(join(folder.cloudPath, "a.txt"), "utf8")).toBe("good");
+ expect(receipt).toEqual({ sha256: sha("good") });
+});
+
+it("waits for queued parts before verifying and committing the upload", async () => {
+ const { uploadId } = sync.beginUpload(folder.id, { path: "a.txt", size: 4, sha256: sha("good"), baseSha256: null });
+ const entered = gate(), release = gate();
+ const first = sync.writePart(uploadId, 0, (async function* () { entered.resolve(); await release.promise; yield Buffer.from("go"); })());
+ await entered.promise;
+ const second = Promise.allSettled([sync.writePart(uploadId, 2, (async function* () { yield Buffer.from("od"); })())]);
+ const committed = Promise.allSettled([sync.commit(uploadId)]);
+ try { await setImmediate(); } finally { release.resolve(); }
+ expect(await first).toEqual({ received: 2 });
+ expect(await second).toEqual([{ status: "fulfilled", value: { received: 4 } }]);
+ expect(await committed).toEqual([{ status: "fulfilled", value: { sha256: sha("good") } }]);
+ expect(readFileSync(join(folder.cloudPath, "a.txt"), "utf8")).toBe("good");
+});
+
+it.each([
+ { stage: "hash", outcome: "committed", status: 200 },
+ { stage: "destination", outcome: "committed", status: 200 },
+ { stage: "hash", outcome: "hash mismatch", status: 422 },
+ { stage: "destination", outcome: "base conflict", status: 409 },
+])("refuses a part queued during $stage after $outcome", async ({ stage, status }) => {
+ const { uploadId } = sync.beginUpload(folder.id, { path: "a.txt", size: 4, sha256: sha("good"), baseSha256: null });
+ await sync.writePart(uploadId, 0, (async function* () { yield Buffer.from(status === 422 ? "evil" : "good"); })());
+ if (status === 409) writeFileSync(join(folder.cloudPath, "a.txt"), "original");
+ const held = stage === "hash" ? holdHash() : holdCurrent();
+ const committed = Promise.allSettled([sync.commit(uploadId)]);
+ await held.entered;
+ const release = gate();
+ const late = Promise.allSettled([sync.writePart(uploadId, 4, (async function* () { await release.promise; yield Buffer.alloc(0); })())]);
+ let result;
+ try {
+ held.release();
+ result = await committed;
+ } finally { held.release(); release.resolve(); }
+ expect(await late).toMatchObject([{ status: "rejected", reason: { status: 404 } }]);
+ expect(result).toMatchObject(status === 200
+ ? [{ status: "fulfilled", value: { sha256: sha("good") } }]
+ : [{ status: "rejected", reason: { status } }]);
+ expect(existsSync(join(sync.store.syncDir(), "uploads", `${uploadId}.json`))).toBe(false);
+ expect(existsSync(join(sync.store.syncDir(), "uploads", `${uploadId}.part`))).toBe(false);
+ if (status !== 422) expect(readFileSync(join(folder.cloudPath, "a.txt"), "utf8")).toBe(status === 409 ? "original" : "good");
+ await expect(sync.writePart(uploadId, 0, (async function* () { yield Buffer.from("evil"); })())).rejects.toMatchObject({ status: 404 });
+});
+
+it("does not expire an upload with an open part writer", async () => {
+ const { uploadId } = sync.beginUpload(folder.id, { path: "a.txt", size: 4, sha256: sha("good"), baseSha256: null });
+ const entered = gate(), release = gate();
+ const part = sync.writePart(uploadId, 0, (async function* () { entered.resolve(); await release.promise; yield Buffer.from("good"); })());
+ await entered.promise;
+ try {
+ now += 24 * 60 * 60_000 + 1;
+ sync.summary();
+ } finally { release.resolve(); await part; }
+ expect(await sync.commit(uploadId)).toEqual({ sha256: sha("good") });
+ expect(readFileSync(join(folder.cloudPath, "a.txt"), "utf8")).toBe("good");
+});
+
+it("does not expire an upload while its commit verifies the hash", async () => {
+ const uploadId = await upload("good", null);
+ const held = holdHash();
+ const committed = Promise.allSettled([sync.commit(uploadId)]);
+ await held.entered;
+ try {
+ now += 24 * 60 * 60_000 + 1;
+ sync.summary();
+ } finally { held.release(); }
+ expect(await committed).toEqual([{ status: "fulfilled", value: { sha256: sha("good") } }]);
+ expect(readFileSync(join(folder.cloudPath, "a.txt"), "utf8")).toBe("good");
+});
+
async function checkedCommit(uploadId: string) {
const checked = gate();
const hash = files.sha256File;
diff --git a/server/cloud-sync.ts b/server/cloud-sync.ts
index ee06afc804..14db9df1d8 100644
--- a/server/cloud-sync.ts
+++ b/server/cloud-sync.ts
@@ -86,6 +86,14 @@ export function createCloudSync(options: { dataDir: string; root?: string; now?:
if (!UUID.test(id)) throw new SyncError(404, { error: "no such upload" });
try { return JSON.parse(readFileSync(uploadFile(id, "json"), "utf8")) as Upload; } catch { throw new SyncError(404, { error: "no such upload" }); }
};
+ const pendingUploads = new Map<string, Promise<unknown>>();
+ const mutateUpload = <T>(id: string, work: (upload: Upload) => T | Promise<T>): Promise<T> => {
+ const run = (pendingUploads.get(id) ?? Promise.resolve()).then(() => work(readUpload(id)));
+ const settled = run.catch(() => undefined);
+ pendingUploads.set(id, settled);
+ void settled.then(() => { if (pendingUploads.get(id) === settled) pendingUploads.delete(id); });
+ return run;
+ };
const saveUpload = (upload: Upload) => writeFileAtomic(uploadFile(upload.id, "json"), JSON.stringify(upload), { mode: 0o600 });
const dropUpload = (id: string) => { rmSync(uploadFile(id, "json"), { force: true }); rmSync(uploadFile(id, "part"), { force: true }); };
const uploads = (): Upload[] => {
@@ -107,7 +115,7 @@ export function createCloudSync(options: { dataDir: string; root?: string; now?:
noteChange,
summary() {
store.sweepTrash();
- for (const upload of uploads()) if (upload.updatedAt + UPLOAD_STALE_MS < now()) dropUpload(upload.id);
+ for (const upload of uploads()) if (!pendingUploads.has(upload.id) && upload.updatedAt + UPLOAD_STALE_MS < now()) dropUpload(upload.id);
return { folders: store.folders(), cursor, disk: disk() };
},
register(body: unknown): SyncFolder {
@@ -158,43 +166,44 @@ export function createCloudSync(options: { dataDir: string; root?: string; now?:
return { uploadId: upload.id, received: 0 };
},
async writePart(uploadId: string, offset: number, body: AsyncIterable<Buffer>): Promise<{ received: number }> {
- const upload = readUpload(uploadId);
- if (offset !== upload.received) throw new SyncError(409, { error: "wrong offset", received: upload.received });
- const fd = openSync(uploadFile(uploadId, "part"), "r+");
- let written = 0;
- try {
- for await (const chunk of body) {
- if (written + chunk.length > PART_MAX || upload.received + written + chunk.length > upload.size) throw new SyncError(413, { error: "part too large" });
- writeSync(fd, chunk, 0, chunk.length, upload.received + written);
- written += chunk.length;
- }
- } finally { closeSync(fd); }
- upload.received += written;
- upload.updatedAt = now();
- saveUpload(upload);
- return { received: upload.received };
+ return mutateUpload(uploadId, async (upload) => {
+ if (offset !== upload.received) throw new SyncError(409, { error: "wrong offset", received: upload.received });
+ const fd = openSync(uploadFile(uploadId, "part"), "r+");
+ let written = 0;
+ try {
+ for await (const chunk of body) {
+ if (written + chunk.length > PART_MAX || upload.received + written + chunk.length > upload.size) throw new SyncError(413, { error: "part too large" });
+ writeSync(fd, chunk, 0, chunk.length, upload.received + written);
+ written += chunk.length;
+ }
+ } finally { closeSync(fd); }
+ upload.received += written;
+ upload.updatedAt = now();
+ saveUpload(upload);
+ return { received: upload.received };
+ });
},
async commit(uploadId: string): Promise<{ sha256: string }> {
- const upload = readUpload(uploadId);
- if (upload.received !== upload.size) throw new SyncError(409, { error: "upload incomplete", received: upload.received });
- const part = uploadFile(uploadId, "part");
- if (await sha256File(part) !== upload.sha256) { dropUpload(uploadId); throw new SyncError(422, { error: "the file's bytes do not match its hash" }); }
- return mutate(upload.folderId, upload.path, async () => {
- readUpload(uploadId);
- const folder = folderOf(upload.folderId);
- const target = fileOf(folder, upload.path);
- const current = await store.current(folder, upload.path);
- if ((current?.sha256 ?? null) !== upload.baseSha256) { dropUpload(uploadId); throw new SyncError(409, { error: "changed on My Cloud", current }); }
- if (current) store.toTrash(folder, upload.path, target);
- mkdirSync(dirname(target), { recursive: true, mode: 0o700 });
- renameSync(part, target);
- // A copy from the person's computer: bots read it, and save their own work in their own folder.
- if (folder.kind === "pc") chmodSync(target, 0o444);
- dropUpload(uploadId);
- const stat = lstatSync(target);
- store.remember(folder, { path: upload.path, size: stat.size, mtimeMs: stat.mtimeMs, sha256: upload.sha256 });
- noteChange(folder.id);
- return { sha256: upload.sha256 };
+ return mutateUpload(uploadId, async (upload) => {
+ if (upload.received !== upload.size) throw new SyncError(409, { error: "upload incomplete", received: upload.received });
+ const part = uploadFile(uploadId, "part");
+ if (await sha256File(part) !== upload.sha256) { dropUpload(uploadId); throw new SyncError(422, { error: "the file's bytes do not match its hash" }); }
+ return mutate(upload.folderId, upload.path, async () => {
+ const folder = folderOf(upload.folderId);
+ const target = fileOf(folder, upload.path);
+ const current = await store.current(folder, upload.path);
+ if ((current?.sha256 ?? null) !== upload.baseSha256) { dropUpload(uploadId); throw new SyncError(409, { error: "changed on My Cloud", current }); }
+ if (current) store.toTrash(folder, upload.path, target);
+ mkdirSync(dirname(target), { recursive: true, mode: 0o700 });
+ renameSync(part, target);
+ // A copy from the person's computer: bots read it, and save their own work in their own folder.
+ if (folder.kind === "pc") chmodSync(target, 0o444);
+ dropUpload(uploadId);
+ const stat = lstatSync(target);
+ store.remember(folder, { path: upload.path, size: stat.size, mtimeMs: stat.mtimeMs, sha256: upload.sha256 });
+ noteChange(folder.id);
+ return { sha256: upload.sha256 };
+ });
});
},
async download(id: string, path: unknown): Promise<{ file: string; size: number; sha256: string }> {