Fix closing during startup wiping the saved encryption store

Closing an E2EE node before its crypto store was restored wrote an
empty snapshot over the saved one, losing the device keys.
This commit is contained in:
2026-10-16 10:35:23 -06:00
parent 5c5e81ca60
commit a839e0d1e7
2 changed files with 11 additions and 1 deletions
+5
View File
@@ -186,6 +186,11 @@ async function snapshotCryptoStore(filePath, dbNamePrefix) {
db.close();
out.push({ name, version, stores });
}
if (!out.length) {
// Nothing in memory (e.g. the store was not restored yet). Writing
// that would replace a good snapshot with an empty one.
return false;
}
const tmp = `${filePath}.tmp`;
fs.writeFileSync(tmp, v8.serialize(out));
+6 -1
View File
@@ -227,6 +227,10 @@ module.exports = function(RED) {
// endpoint. Stops any further snapshot from writing the (deleted
// or recreated-empty) store back to disk before the redeploy.
cryptoCleared = false,
// Set once the Rust crypto stack is initialized. Until then the
// in-memory store may not be restored yet, and snapshotting it
// would overwrite the saved store on disk.
cryptoReady = false,
// Set when the node is closed. Startup is asynchronous (SDK import,
// homeserver discovery, /whoami, crypto init), so every step checks
// it: a node closed mid-startup (e.g. deployed again quickly) must
@@ -306,7 +310,7 @@ module.exports = function(RED) {
// Snapshot the Rust crypto store to disk so E2EE state survives
// restarts. No-op when E2EE is disabled.
async function persistCrypto() {
if(!cryptoSnapshotPath || cryptoCleared) {
if(!cryptoSnapshotPath || cryptoCleared || !cryptoReady) {
return;
}
try {
@@ -1009,6 +1013,7 @@ module.exports = function(RED) {
{ cause: cryptoInitError }
);
}
cryptoReady = true;
let crypto = node.matrixClient.getCrypto();
if(crypto) {
// Blacklist (refuse to encrypt to) unverified devices only