Skip to content

Build your personal knowledge base with Trilium Notes

core/backup import: fix an issue

Adorian Doran539521955387
diff --git a/apps/server/src/services/chunked_upload.spec.ts b/apps/server/src/services/chunked_upload.spec.ts
index 8944c9c5dc..778f545d8e 100644
--- a/apps/server/src/services/chunked_upload.spec.ts
+++ b/apps/server/src/services/chunked_upload.spec.ts
@@ -216,6 +216,25 @@ describe("chunked upload: starting and ending a session", () => {
await expect(upload.begin(beginRequest(8))).rejects.toThrow(/already in progress/);
});
+ it("holds that limit against requests that arrive together, not just one after another", async () => {
+ // Nothing authenticates the caller during setup, so the limit is all there is between one
+ // upload and as many multi-gigabyte temporary files as anyone cares to ask for. Looking at
+ // the count and taking a place are several awaits apart, which is a gap two requests can
+ // both fit through unless opening a session is one act.
+ const { upload } = uploadService({ maxTotalBytes: 1024 * 1024 });
+ const outcomes = await Promise.allSettled(
+ Array.from({ length: 5 }, () => upload.begin(beginRequest(1024)))
+ );
+
+ expect(outcomes.filter((outcome) => outcome.status === "fulfilled")).toHaveLength(1);
+ expect(upload.activeSessions()).toBe(1);
+ for (const refused of outcomes.filter((outcome) => outcome.status === "rejected")) {
+ expect(String((refused as PromiseRejectedResult).reason)).toMatch(/already in progress/);
+ }
+ // One upload accepted means one file on disk, whatever was asked for.
+ expect(fs.readdirSync(path.join(tempRoot, "uploads", "spec"))).toHaveLength(1);
+ });
+
it("reduces the stated name to a base name, so it cannot point outside the upload directory", async () => {
const { upload, completed } = uploadService();
const { uploadId } = await upload.begin(beginRequest(4, "../../etc/passwd"));
diff --git a/apps/server/src/services/chunked_upload.ts b/apps/server/src/services/chunked_upload.ts
index daea6b7d83..d62676dd2b 100644
--- a/apps/server/src/services/chunked_upload.ts
+++ b/apps/server/src/services/chunked_upload.ts
@@ -130,12 +130,27 @@ export function createChunkedUpload<T>(config: ChunkedUploadConfig<T>): ChunkedU
const maxConcurrentSessions = config.maxConcurrentSessions ?? 1;
const directory = path.resolve(config.directory, config.name);
const sessions = new Map<string, Session>();
+ /** Lets one caller at a time open a session, so the limit on how many are open holds. */
+ const admission = new Mutex();
// Unreferenced so neither a test nor a shutdown waits on it.
const timer = setInterval(() => void sweep(), SWEEP_INTERVAL_MS);
timer.unref?.();
+ /**
+ * Opens a session, one caller at a time.
+ *
+ * Looking at the count and taking a place are several awaits apart: sweeping first, then reading
+ * the free space, then creating the file. Two requests arriving together would both look while
+ * the other was between those steps, both see room, and both take it — which on an endpoint that
+ * needs no authentication is as many uploads, and as much of the disk, as anyone cares to ask
+ * for. Serialising the whole of it makes the two one act.
+ */
async function begin(req: Request): Promise<ChunkedUploadStatus> {
+ return admission.runExclusive(() => admit(req));
+ }
+
+ async function admit(req: Request): Promise<ChunkedUploadStatus> {
const { fileName, totalBytes, metadata } = (req.body ?? {}) as BeginBody;
// Before the count is checked, so an abandoned session never blocks the next one.