diff --git a/src/box/txn.c b/src/box/txn.c index 818f405bf527e1e4e22003ee78dd8e7edf7deedf..c605345dee047a675dcb003dd7d8589e6e8ffa1a 100644 --- a/src/box/txn.c +++ b/src/box/txn.c @@ -342,11 +342,6 @@ txn_commit_stmt(struct txn *txn, struct request *request) static inline void txn_run_triggers(struct txn *txn, struct rlist *trigger) { - /* - * Some triggers require for in_txn variable to be set so - * restore it for the time triggers are in progress. - */ - fiber_set_txn(fiber(), txn); /* Rollback triggers must not throw. */ if (trigger_run(trigger, txn) != 0) { /* @@ -357,7 +352,6 @@ txn_run_triggers(struct txn *txn, struct rlist *trigger) unreachable(); panic("commit/rollback trigger failed"); } - fiber_set_txn(fiber(), NULL); } /** @@ -412,7 +406,15 @@ txn_entry_done_cb(struct journal_entry *entry, void *data) { struct txn *txn = data; txn->signature = entry->res; + /* + * Some commit/rollback triggers require for in_txn fiber + * variable to be set so restore it for the time triggers + * are in progress. + */ + assert(in_txn() == NULL); + fiber_set_txn(fiber(), txn); txn_complete(txn); + fiber_set_txn(fiber(), NULL); } static int64_t @@ -497,14 +499,15 @@ txn_write(struct txn *txn) * After this point the transaction must not be used * so reset the corresponding key in the fiber storage. */ - fiber_set_txn(fiber(), NULL); txn->start_tm = ev_monotonic_now(loop()); if (txn->n_new_rows + txn->n_applier_rows == 0) { /* Nothing to do. */ txn->signature = 0; txn_complete(txn); + fiber_set_txn(fiber(), NULL); return 0; } + fiber_set_txn(fiber(), NULL); return txn_write_to_wal(txn); } @@ -555,7 +558,12 @@ txn_rollback(struct txn *txn) void txn_abort(struct txn *txn) { + assert(in_txn() == txn); txn_rollback_to_svp(txn, NULL); + if (txn->has_triggers) { + txn_run_triggers(txn, &txn->on_rollback); + txn->has_triggers = false; + } txn->is_aborted = true; } diff --git a/test/box/transaction.result b/test/box/transaction.result index 9da53e5be626f8df11e405a37345b07cb3e73163..857314b72cfae14142150f22515f19ad7e72bf2e 100644 --- a/test/box/transaction.result +++ b/test/box/transaction.result @@ -698,3 +698,27 @@ box.space.memtx:drop() box.space.vinyl:drop() --- ... +-- +-- Check that changes done to the schema by a DDL statement are +-- rolled back when the transaction is aborted on fiber yield. +-- +s = box.schema.space.create('test') +--- +... +box.begin() s:create_index('pk') s:insert{1} +--- +... +fiber.sleep(0) +--- +... +s.index.pk == nil +--- +- true +... +box.commit() -- error +--- +- error: Transaction has been aborted by a fiber yield +... +s:drop() +--- +... diff --git a/test/box/transaction.test.lua b/test/box/transaction.test.lua index a6789316c01ffe6f75dd2bd019f4aeca4401e574..8ffae2fee8e42d29a907fe2644cf3af830314671 100644 --- a/test/box/transaction.test.lua +++ b/test/box/transaction.test.lua @@ -363,3 +363,13 @@ if box.space.test then box.space.test:drop() end box.space.memtx:drop() box.space.vinyl:drop() +-- +-- Check that changes done to the schema by a DDL statement are +-- rolled back when the transaction is aborted on fiber yield. +-- +s = box.schema.space.create('test') +box.begin() s:create_index('pk') s:insert{1} +fiber.sleep(0) +s.index.pk == nil +box.commit() -- error +s:drop()