fix(whatsapp): await write stream finish in encryptedStream to fix race-condition ENOENT crash

This commit is contained in:
termtek
2026-04-13 18:50:29 +08:00
committed by Frank Yang
parent 323493fa1b
commit 6c41ff7d22
3 changed files with 32 additions and 2 deletions

View File

@@ -1523,6 +1523,9 @@
"strip-ansi": "^7.2.0"
}
}
},
"patchedDependencies": {
"@whiskeysockets/baileys@7.0.0-rc.9": "patches/@whiskeysockets__baileys@7.0.0-rc.9.patch"
}
}
}

View File

@@ -0,0 +1,22 @@
diff --git a/lib/Utils/messages-media.js b/lib/Utils/messages-media.js
index 0d32dfb4882dfe029ba8804772d7d89404b08e76..4eb2b214b48ef3af48f707237cd19c05e11632a6 100644
--- a/lib/Utils/messages-media.js
+++ b/lib/Utils/messages-media.js
@@ -353,9 +353,17 @@ export const encryptedStream = async (media, mediaType, { logger, saveOriginalFi
const fileSha256 = sha256Plain.digest();
const fileEncSha256 = sha256Enc.digest();
encFileWriteStream.write(mac);
+ // Create finish promises before calling end() to avoid missing the event
+ const encFinishPromise = once(encFileWriteStream, 'finish');
+ const originalFinishPromise = originalFileStream ? once(originalFileStream, 'finish') : Promise.resolve();
encFileWriteStream.end();
originalFileStream?.end?.();
stream.destroy();
+ // Wait for write streams to fully flush to disk before returning encFilePath.
+ // Without this await, the caller may open a read stream on the file before
+ // the OS has created it, causing a race-condition ENOENT crash.
+ await encFinishPromise;
+ await originalFinishPromise;
logger?.debug('encrypted data successfully');
return {
mediaKey,

9
pnpm-lock.yaml generated
View File

@@ -27,6 +27,11 @@ overrides:
packageExtensionsChecksum: sha256-n+P/SQo4Pf+dHYpYn1Y6wL4cJEVoVzZ835N0OEp4TM8=
patchedDependencies:
'@whiskeysockets/baileys@7.0.0-rc.9':
hash: c79c66180c377cf57f4dd1657c85b572fafb3cf9667ea36af5d4a991eb6bb709
path: patches/@whiskeysockets__baileys@7.0.0-rc.9.patch
importers:
.:
@@ -1221,7 +1226,7 @@ importers:
dependencies:
'@whiskeysockets/baileys':
specifier: 7.0.0-rc.9
version: 7.0.0-rc.9(audio-decode@2.2.3)(jimp@1.6.1)(sharp@0.34.5)
version: 7.0.0-rc.9(patch_hash=c79c66180c377cf57f4dd1657c85b572fafb3cf9667ea36af5d4a991eb6bb709)(audio-decode@2.2.3)(jimp@1.6.1)(sharp@0.34.5)
jimp:
specifier: ^1.6.1
version: 1.6.1
@@ -11423,7 +11428,7 @@ snapshots:
'@wasm-audio-decoders/common': 9.0.7
optional: true
'@whiskeysockets/baileys@7.0.0-rc.9(audio-decode@2.2.3)(jimp@1.6.1)(sharp@0.34.5)':
'@whiskeysockets/baileys@7.0.0-rc.9(patch_hash=c79c66180c377cf57f4dd1657c85b572fafb3cf9667ea36af5d4a991eb6bb709)(audio-decode@2.2.3)(jimp@1.6.1)(sharp@0.34.5)':
dependencies:
'@cacheable/node-cache': 1.7.6
'@hapi/boom': 9.1.4