From 338a028185380411d3ed1700c7c4facb7a63b5fd Mon Sep 17 00:00:00 2001 From: James Keane Date: Wed, 6 Dec 2023 20:51:51 -0500 Subject: [PATCH] fix(server): await`sendFile` (#5515) Fixes the intermittent EPIPE errors that myself and others are seeing. By explicitly returning a promise we ensure the caller correctly waits until the `sendFile` is complete before potentially closing or cleaning the socket. This is the most likely bug that would cause EPIPE errors. Fix was confirmed on a live system -- would benefit from a unit test though. --- .../src/immich/api-v1/asset/asset.service.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/server/src/immich/api-v1/asset/asset.service.ts b/server/src/immich/api-v1/asset/asset.service.ts index 48b64672d..cbe63e557 100644 --- a/server/src/immich/api-v1/asset/asset.service.ts +++ b/server/src/immich/api-v1/asset/asset.service.ts @@ -336,14 +336,18 @@ export class AssetService { res.set('Cache-Control', 'private, max-age=86400, no-transform'); res.header('Content-Type', mimeTypes.lookup(filepath)); - res.sendFile(filepath, options, (error: Error) => { - if (!error) { - return; - } + return new Promise((resolve, reject) => { + res.sendFile(filepath, options, (error: Error) => { + if (!error) { + resolve(); + return; + } - if (error.message !== 'Request aborted') { - this.logger.error(`Unable to send file: ${error.name}`, error.stack); - } + if (error.message !== 'Request aborted') { + this.logger.error(`Unable to send file: ${error.name}`, error.stack); + } + reject(error); + }); }); }