Skip to content

Commit

Permalink
Add logging to crypto store transactions
Browse files Browse the repository at this point in the history
We churn through a huge number of crypto store transactions during startup,
which may be the cause of the symptoms in
element-hq/element-web#16194.
  • Loading branch information
jryans committed Feb 25, 2021
1 parent b55e6c4 commit f43fe36
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/crypto/OlmDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import {logger} from '../logger';
import {getPrefixedLogger, logger} from '../logger';
import {IndexedDBCryptoStore} from './store/indexeddb-crypto-store';
import * as algorithms from './algorithms';

Expand Down Expand Up @@ -545,6 +545,7 @@ OlmDevice.prototype.createOutboundSession = async function(
}
});
},
getPrefixedLogger("[createOutboundSession]"),
);
return newSessionId;
};
Expand Down Expand Up @@ -605,6 +606,7 @@ OlmDevice.prototype.createInboundSession = async function(
}
});
},
getPrefixedLogger("[createInboundSession]"),
);

return result;
Expand Down Expand Up @@ -638,6 +640,7 @@ OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityK
},
);
},
getPrefixedLogger("[getSessionIdsForDevice]"),
);

return sessionIds;
Expand Down Expand Up @@ -727,6 +730,7 @@ OlmDevice.prototype.getSessionInfoForDevice = async function(deviceIdentityKey,
}
});
},
getPrefixedLogger("[getSessionInfoForDevice]"),
);

return info;
Expand Down Expand Up @@ -761,6 +765,7 @@ OlmDevice.prototype.encryptMessage = async function(
this._saveSession(theirDeviceIdentityKey, sessionInfo, txn);
});
},
getPrefixedLogger("[encryptMessage]"),
);
return res;
};
Expand Down Expand Up @@ -794,6 +799,7 @@ OlmDevice.prototype.decryptMessage = async function(
this._saveSession(theirDeviceIdentityKey, sessionInfo, txn);
});
},
getPrefixedLogger("[decryptMessage]"),
);
return payloadString;
};
Expand Down Expand Up @@ -825,6 +831,7 @@ OlmDevice.prototype.matchesSession = async function(
matches = sessionInfo.session.matches_inbound(ciphertext);
});
},
getPrefixedLogger("[matchesSession]"),
);
return matches;
};
Expand Down Expand Up @@ -1095,6 +1102,7 @@ OlmDevice.prototype.addInboundGroupSession = async function(
},
);
},
getPrefixedLogger("[addInboundGroupSession]"),
);
};

Expand Down Expand Up @@ -1265,6 +1273,7 @@ OlmDevice.prototype.decryptGroupMessage = async function(
},
);
},
getPrefixedLogger("[decryptGroupMessage]"),
);

if (error) {
Expand Down Expand Up @@ -1310,6 +1319,7 @@ OlmDevice.prototype.hasInboundSessionKeys = async function(roomId, senderKey, se
},
);
},
getPrefixedLogger("[hasInboundSessionKeys]"),
);

return result;
Expand Down Expand Up @@ -1369,6 +1379,7 @@ OlmDevice.prototype.getInboundGroupSessionKey = async function(
},
);
},
getPrefixedLogger("[getInboundGroupSessionKey]"),
);

return result;
Expand Down
14 changes: 13 additions & 1 deletion src/crypto/store/indexeddb-crypto-store-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class Backend {
*/
constructor(db) {
this._db = db;
this._nextTxnId = 0;

// make sure we close the db on `onversionchange` - otherwise
// attempts to delete the database will block (and subsequent
Expand Down Expand Up @@ -757,10 +758,21 @@ export class Backend {
}));
}

doTxn(mode, stores, func) {
doTxn(mode, stores, func, log = logger) {
const txnId = this._nextTxnId++;
const startTime = Date.now();
const description = `${mode} crypto store transaction ${txnId} in ${stores}`;
log.debug(`Starting ${description}`);
const txn = this._db.transaction(stores, mode);
const promise = promiseifyTxn(txn);
const result = func(txn);
promise.then(() => {
const elapsedTime = Date.now() - startTime;
log.debug(`Finished ${description}, took ${elapsedTime} ms`);
}, () => {
const elapsedTime = Date.now() - startTime;
log.error(`Failed ${description}, took ${elapsedTime} ms`);
});
return promise.then(() => {
return result;
});
Expand Down
5 changes: 3 additions & 2 deletions src/crypto/store/indexeddb-crypto-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,15 +596,16 @@ export class IndexedDBCryptoStore {
* @param {function(*)} func Function called with the
* transaction object: an opaque object that should be passed
* to store functions.
* @param {object} [log] A possibly customised log
* @return {Promise} Promise that resolves with the result of the `func`
* when the transaction is complete. If the backend is
* async (ie. the indexeddb backend) any of the callback
* functions throwing an exception will cause this promise to
* reject with that exception. On synchronous backends, the
* exception will propagate to the caller of the getFoo method.
*/
doTxn(mode, stores, func) {
return this._backend.doTxn(mode, stores, func);
doTxn(mode, stores, func, log) {
return this._backend.doTxn(mode, stores, func, log);
}
}

Expand Down

0 comments on commit f43fe36

Please sign in to comment.