MatrixAI / js-db

Key-Value DB for TypeScript and JavaScript Applications
https://polykey.com
Apache License 2.0
5 stars 0 forks source link

Embed the `DBTransaction.destroy` into the `DBTransaction.commit` and `DBTransaction.rollback`. #56

Open CMCDragonkai opened 1 year ago

CMCDragonkai commented 1 year ago

Specification

Previously the destroy call was called inside commit and rollback. This has since been removed since #53, since the ResourceAcquire<DBTransaction> already called destroy and it was unnecessary to call destroy twice.

However we discovered in the EFS (https://github.com/MatrixAI/js-encryptedfs/pull/74#issuecomment-1221833309), a dangling/floating promise could still have a reference to the transaction and end up trying to do a get on a transaction that is already committed. This ends up with the C++ code throwing a TRANSACTION_COMMITTED exception. This is not caught on the JS side because it was not expected to happen.

We are relying on the @ready decorator to protect the get methods and other methods if they are called when the transaction is already destroyed. But it does appear that there can be time between commit/rollback and destruction because these are separate functions.

We could make commit/rollback the equivalent of a destruction by applying the commit/rollback code inside the destroy call. And make destroy have a type: 'commit' | 'rollback' union. This way DBTransaction.commit and DBTransaction.rollback is just forwarding the call to the destroy.

This can simplify our transaction lifecycle, and ensure that methods cannot be called when a commit/destroy is being called, because this means it's in the process of being destroyed. It could also mean that we can use the ready blocking that we found we couldn't do when attempting to fix the dangling transactions during db.stop().

Additional context

Tasks

  1. ...
  2. ...
  3. ...