Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Cancel a backup download whose copy failed

Matthew Jackson8c3e0f40f6b1
diff --git a/src/lib/backup.dbtest.ts b/src/lib/backup.dbtest.ts
index bfbbbd3..fbcd24c 100644
--- a/src/lib/backup.dbtest.ts
+++ b/src/lib/backup.dbtest.ts
@@ -452,3 +452,56 @@ test("a file written again at the same size is copied again", async () => {
assert.equal(third.filesCopied, 0);
assert.equal(third.filesUnchanged, 1);
});
+
+test("a file whose copy fails before it is read is let go, in a backup and in a restore", async () => {
+ let cancelled = 0;
+ const source = memoryStore();
+ const target = memoryStore();
+ const body = () =>
+ new ReadableStream<Uint8Array>({
+ cancel() {
+ cancelled += 1;
+ },
+ pull(controller) {
+ controller.enqueue(new Uint8Array(4));
+ },
+ });
+
+ await source.put("a.pdf", Buffer.from("a"), {
+ contentType: "application/pdf",
+ });
+ source.get = async () => ({ contentType: "application/pdf", stream: body() });
+
+ // The target refuses every file before reading any of it.
+ const put = target.put;
+
+ target.put = async (pathname, stream, options) => {
+ if (pathname.startsWith("files/")) {
+ throw new Error("denied");
+ }
+
+ return put(pathname, stream, options);
+ };
+
+ const summary = await runBackup({ database, keepDays: 30, source, target });
+
+ assert.deepEqual(summary.fileFailures, ["a.pdf"]);
+ assert.equal(cancelled, 1, "the source download was given up");
+
+ // And the other way round.
+ const backup = memoryStore();
+ const app = memoryStore();
+
+ await backup.put("files/b.pdf", Buffer.from("b"), {
+ contentType: "application/pdf",
+ });
+ backup.get = async () => ({ contentType: "application/pdf", stream: body() });
+ app.put = async () => {
+ throw new Error("denied");
+ };
+
+ const restored = await restoreFiles({ source: backup, target: app });
+
+ assert.deepEqual(restored.failures, ["files/b.pdf"]);
+ assert.equal(cancelled, 2, "the backup's copy was given up");
+});
diff --git a/src/lib/backup.ts b/src/lib/backup.ts
index 72b6820..b1aa8bc 100644
--- a/src/lib/backup.ts
+++ b/src/lib/backup.ts
@@ -158,6 +158,18 @@ export function isBackupStoreTheAppStore(
return Boolean(backupStore && appStores.includes(backupStore));
}
+/**
+ * Lets go of a download that was not used up, so its connection is not
+ * held open until it is collected.
+ */
+async function discardStream(stream: ReadableStream<Uint8Array> | undefined) {
+ try {
+ await stream?.cancel();
+ } catch {
+ // Already read, or closed: nothing is held.
+ }
+}
+
async function listAll(store: BackupStore, prefix?: string) {
const blobs: BackupBlob[] = [];
let cursor: string | undefined;
@@ -457,8 +469,10 @@ export async function runBackup({
// Until this copy is done, the one in the backup is not this version.
delete copiedFrom[original.pathname];
+ let found: Awaited<ReturnType<BackupStore["get"]>> = null;
+
try {
- const found = await source.get(original.pathname);
+ found = await source.get(original.pathname);
if (!found) {
continue;
@@ -470,6 +484,7 @@ export async function runBackup({
copiedFrom[original.pathname] = version;
summary.filesCopied += 1;
} catch (error) {
+ await discardStream(found?.stream);
summary.fileFailures.push(original.pathname);
console.error(
`[backup] Could not copy ${original.pathname}:`,
@@ -724,8 +739,10 @@ export async function restoreFiles({
continue;
}
+ let found: Awaited<ReturnType<BackupStore["get"]>> = null;
+
try {
- const found = await source.get(copy.pathname);
+ found = await source.get(copy.pathname);
if (!found) {
failures.push(copy.pathname);
@@ -737,6 +754,7 @@ export async function restoreFiles({
});
copied += 1;
} catch {
+ await discardStream(found?.stream);
failures.push(copy.pathname);
}
}