fix(server): awaitsendFile (#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.
This commit is contained in:
James Keane 2023-12-06 20:51:51 -05:00 committed by GitHub
parent e2d0e944eb
commit 338a028185
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);
});
});
}