Skip to content

Commit

Permalink
Merge pull request #8429 from Agoric/mhofman/8423-crank-ensure-transa…
Browse files Browse the repository at this point in the history
…ction

fix(swing-store): ensure crank savepoint is wrapped in transaction
  • Loading branch information
mergify[bot] authored and mhofman committed Nov 8, 2023
2 parents c9c8395 + 2b61471 commit eab0443
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/swing-store/src/swingStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ export function makeSwingStore(dirPath, forceReset, options = {}) {
inCrank || Fail`establishCrankSavepoint outside of crank`;
const savepointOrdinal = savepoints.length;
savepoints.push(savepoint);
// We must be in a transaction when creating the savepoint or releasing it
// later will cause an autocommit.
// See https://github.com/Agoric/agoric-sdk/issues/8423
ensureTxn();
const sql = db.prepare(`SAVEPOINT t${savepointOrdinal}`);
sql.run();
}
Expand Down
34 changes: 34 additions & 0 deletions packages/swing-store/test/test-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,37 @@ test('close will abort transaction', async t => {
t.is(kvStore.get('key2'), undefined);
t.falsy(kvStore.has('key2'));
});

test('savepoints', async t => {
const [dbDir, cleanup] = await tmpDir('testdb');
t.teardown(cleanup);
const ss1 = initSwingStore(dbDir);
ss1.kernelStorage.startCrank();
ss1.kernelStorage.kvStore.set('key', 'value1');
ss1.kernelStorage.establishCrankSavepoint('sp1');
ss1.kernelStorage.kvStore.set('key', 'value2');
ss1.kernelStorage.establishCrankSavepoint('sp2');
ss1.kernelStorage.kvStore.set('key', 'value3');
ss1.kernelStorage.rollbackCrank('sp1');
ss1.kernelStorage.endCrank();
await ss1.hostStorage.commit();
await ss1.hostStorage.close();

const ss2 = openSwingStore(dbDir);
t.is(ss2.kernelStorage.kvStore.get('key'), 'value1');
});

test('savepoints do not automatically commit', async t => {
const [dbDir, cleanup] = await tmpDir('testdb');
t.teardown(cleanup);
const ss1 = initSwingStore(dbDir);
ss1.kernelStorage.startCrank();
ss1.kernelStorage.establishCrankSavepoint('sp1');
ss1.kernelStorage.kvStore.set('key', 'value1');
// #8423 meant this .endCrank() accidentally did a commit()
ss1.kernelStorage.endCrank();
await ss1.hostStorage.close();

const ss2 = openSwingStore(dbDir);
t.false(ss2.kernelStorage.kvStore.has('key'));
});

0 comments on commit eab0443

Please sign in to comment.