Skip to content

Commit bbca4c1

Browse files
committed
Bug #36824054: scope_guard's use of "commit" and "rollback" confusing and non-standard
Renamed scope_guard::rollback() to scope_guard::reset() Renamed scope_guard::commit() to scope_guard::release() Added Doxygen for the class and the functor. Change-Id: Ided6a71aa468f39eb1f9cea838e42da60ddf2f41
1 parent c79a2fb commit bbca4c1

23 files changed

+110
-58
lines changed

components/libminchassis/dynamic_loader.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ bool mysql_dynamic_loader_imp::load_do_load_component_by_scheme(
698698
bool res = mysql_dynamic_loader_imp::load_do_collect_services_provided(
699699
loaded_components);
700700
if (!res) {
701-
guard.commit();
701+
guard.release();
702702
}
703703
return res;
704704
}
@@ -827,7 +827,7 @@ bool mysql_dynamic_loader_imp::load_do_register_services(
827827
bool res =
828828
mysql_dynamic_loader_imp::load_do_resolve_dependencies(loaded_components);
829829
if (!res) {
830-
guard.commit();
830+
guard.release();
831831
}
832832
return res;
833833
}
@@ -873,7 +873,7 @@ bool mysql_dynamic_loader_imp::load_do_resolve_dependencies(
873873
bool res = mysql_dynamic_loader_imp::load_do_initialize_components(
874874
loaded_components);
875875
if (!res) {
876-
guard.commit();
876+
guard.release();
877877
}
878878
return res;
879879
}
@@ -919,7 +919,7 @@ bool mysql_dynamic_loader_imp::load_do_initialize_components(
919919

920920
bool res = mysql_dynamic_loader_imp::load_do_commit(loaded_components);
921921
if (!res) {
922-
guard.commit();
922+
guard.release();
923923
}
924924
return res;
925925
}

components/libminchassis/dynamic_loader_scheme_file.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ DEFINE_BOOL_METHOD(mysql_dynamic_loader_scheme_file_imp::load,
179179
return true;
180180
}
181181

182-
guard_library.commit();
183-
guard_library_set.commit();
182+
guard_library.release();
183+
guard_library_set.release();
184184

185185
return false;
186186
} catch (...) {

components/test/test_mysql_thd_store_service.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ long long test_thd_store_service_function(UDF_INIT *, UDF_ARGS *,
111111
return 0;
112112
}
113113

114-
cleanup.commit();
114+
cleanup.release();
115115

116116
return 1;
117117
}
@@ -179,7 +179,7 @@ static mysql_service_status_t init() {
179179
if (mysql_thd_store_service->set(o_thd, g_slot, test_mysql_thd_data))
180180
return true;
181181

182-
cleanup_guard.commit();
182+
cleanup_guard.release();
183183

184184
return false;
185185
}

components/test/test_sensitive_system_variables.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ test_component_sensitive_system_variables_service_init() {
141141
return 1;
142142
}
143143

144-
cleanup.commit();
144+
cleanup.release();
145145
return 0;
146146
}
147147

include/scope_guard.h

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,91 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
2424
#ifndef SCOPE_GUARD_H
2525
#define SCOPE_GUARD_H
2626

27+
/**
28+
@brief A Lambda to be called at scope exit.
29+
30+
Used as std::scope_exit of sorts.
31+
Useful if you can't use unique_ptr to install a
32+
specific deleter but still want to do automatic
33+
cleanup at scope exit.
34+
35+
@note Always use @ref create_scope_guard() instead of this template directly!
36+
37+
Typical use is:
38+
@code
39+
...
40+
foo_init();
41+
auto cleanup_foo = create_scope_guard([&] {
42+
foo_deinit();
43+
}
44+
...
45+
if (some_error)
46+
return; // cleanup_foo calls foo_deinit()
47+
...
48+
// foo_deinit is not going to be called past this point
49+
cleanup_foo.release();
50+
return; // return with foo initialized.
51+
...
52+
@endcode
53+
*/
2754
template <typename TLambda>
2855
class Scope_guard {
2956
public:
30-
Scope_guard(const TLambda &rollback_lambda)
31-
: m_committed(false), m_rollback_lambda(rollback_lambda) {}
57+
Scope_guard(const TLambda &cleanup_lambda)
58+
: m_is_released(false), m_cleanup_lambda(cleanup_lambda) {}
3259
Scope_guard(const Scope_guard<TLambda> &) = delete;
3360
Scope_guard(Scope_guard<TLambda> &&moved)
34-
: m_committed(moved.m_committed),
35-
m_rollback_lambda(moved.m_rollback_lambda) {
36-
/* Set moved guard to "invalid" state, the one in which the rollback lambda
61+
: m_is_released(moved.m_is_released),
62+
m_cleanup_lambda(moved.m_cleanup_lambda) {
63+
/* Set moved guard to "invalid" state, the one in which the cleanup lambda
3764
will not be executed. */
38-
moved.m_committed = true;
65+
moved.m_is_released = true;
3966
}
4067
~Scope_guard() {
41-
if (!m_committed) {
42-
m_rollback_lambda();
68+
if (!m_is_released) {
69+
m_cleanup_lambda();
4370
}
4471
}
4572

46-
inline void commit() { m_committed = true; }
73+
/**
74+
@brief Releases the scope guard
75+
76+
Makes sure that when scope guard goes out of scope the
77+
cleanup lambda is not going to be called.
78+
*/
79+
inline void release() { m_is_released = true; }
4780

48-
inline void rollback() {
49-
if (!m_committed) {
50-
m_rollback_lambda();
51-
m_committed = true;
81+
/**
82+
@brief Calls the cleanup lambda and releases the scope guard
83+
84+
Useful if you want to explicitly provoke the cleanup earlier
85+
than when going out of scope.
86+
*/
87+
inline void reset() {
88+
if (!m_is_released) {
89+
m_cleanup_lambda();
90+
m_is_released = true;
5291
}
5392
}
5493

5594
private:
56-
bool m_committed;
57-
const TLambda m_rollback_lambda;
95+
/** If true the cleanup is not going to be called */
96+
bool m_is_released;
97+
/** The cleanup to be called */
98+
const TLambda m_cleanup_lambda;
5899
};
59100

101+
/**
102+
@brief Create a scope guard object
103+
104+
Always use this instead of the @ref Scope_guard template itself!
105+
@sa @ref Scope_guard
106+
107+
@tparam TLambda The type of the lambda. Inferred, never specify it.
108+
@param rollback_lambda The lambda to execute.
109+
@return Scope_guard<TLambda> An specialization of the @ref Scope_guard
110+
template.
111+
*/
60112
template <typename TLambda>
61113
Scope_guard<TLambda> create_scope_guard(const TLambda rollback_lambda) {
62114
return Scope_guard<TLambda>(rollback_lambda);

mysys/my_winfile.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ FILE *my_win_fopen(const char *filename, const char *mode) {
684684
flags) < 0)
685685
return nullptr;
686686

687-
sg.commit(); // Do not close the stream we are about to return
687+
sg.release(); // Do not close the stream we are about to return
688688
return stream;
689689
}
690690

@@ -784,8 +784,8 @@ FILE *my_win_freopen(const char *path, const char *mode, FILE *stream) {
784784
if (_dup2(handle_fd, fd) < 0) return nullptr;
785785

786786
// Leave the handle and fd open, but close handle_fd
787-
chg.commit();
788-
cfdg.commit();
787+
chg.release();
788+
cfdg.release();
789789

790790
return stream;
791791
}

plugin/keyring_udf/keyring_udf.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ static bool fetch(const char *function_name, char *key_id, char **a_key,
427427

428428
if (a_key_len != nullptr) *a_key_len = fetched_key_len;
429429

430-
cleanup_guard.commit();
430+
cleanup_guard.release();
431431

432432
return false;
433433
}

plugin/x/src/module_mysqlx.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ int Module_mysqlx::initialize(MYSQL_PLUGIN plugin_handle) {
193193
m_server->delayed_start_tasks();
194194
}
195195

196-
guard_of_server_start.commit();
196+
guard_of_server_start.release();
197197
} catch (const std::exception &e) {
198198
log_error(ER_XPLUGIN_STARTUP_FAILED, e.what());
199199
return 1;

router/src/connection_pool/src/connection_pool_plugin.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static void init(mysql_harness::PluginFuncEnv *env) {
131131
std::chrono::seconds{config.idle_timeout}));
132132
}
133133

134-
init_guard.commit();
134+
init_guard.release();
135135
} catch (const std::invalid_argument &exc) {
136136
set_error(env, mysql_harness::kConfigInvalidArgument, "%s", exc.what());
137137

router/src/http/src/http_server_plugin.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ static void init(mysql_harness::PluginFuncEnv *env) {
610610
config.static_basedir, config.require_realm));
611611
}
612612

613-
initialization_finished.commit();
613+
initialization_finished.release();
614614
}
615615
} catch (const std::invalid_argument &exc) {
616616
set_error(env, mysql_harness::kConfigInvalidArgument, "%s", exc.what());

router/src/routing/src/mysql_routing.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ stdx::expected<void, std::string> MySQLRouting::run_acceptor(
849849
is_running_ = false;
850850
get_context().shared_quarantine().stop();
851851

852-
stop_acceptors_guard.commit();
852+
stop_acceptors_guard.release();
853853
// routing is no longer running, lets close listening socket
854854
stop_socket_acceptors();
855855

sql/histograms/histogram.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ static bool fill_value_maps(
12261226
if (res != HA_ERR_END_OF_FILE) return true; /* purecov: deadcode */
12271227

12281228
// Close the handler
1229-
handler_guard.commit();
1229+
handler_guard.release();
12301230
if (table->file->ha_sample_end(scan_ctx)) {
12311231
assert(false); /* purecov: deadcode */
12321232
return true;
@@ -1407,7 +1407,7 @@ bool update_histogram(THD *thd, Table_ref *table, const columns_set &columns,
14071407

14081408
bool ret = trans_commit_stmt(thd) || trans_commit(thd);
14091409
close_thread_tables(thd);
1410-
tables_guard.commit();
1410+
tables_guard.release();
14111411

14121412
return ret;
14131413
}
@@ -1480,7 +1480,7 @@ bool update_histogram(THD *thd, Table_ref *table, const columns_set &columns,
14801480

14811481
bool ret = trans_commit_stmt(thd) || trans_commit(thd);
14821482
close_thread_tables(thd);
1483-
tables_guard.commit();
1483+
tables_guard.release();
14841484
return ret;
14851485
}
14861486

sql/iterators/composite_iterators.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ bool MaterializeIterator<Profiler>::Init() {
931931
}
932932
} else {
933933
// We didn't open the index, so we don't need to close it.
934-
end_unique_index.commit();
934+
end_unique_index.release();
935935
}
936936
ha_rows stored_rows = 0;
937937

@@ -953,7 +953,7 @@ bool MaterializeIterator<Profiler>::Init() {
953953
}
954954
}
955955

956-
end_unique_index.rollback();
956+
end_unique_index.reset();
957957
table()->materialized = true;
958958

959959
if (!m_rematerialize) {
@@ -1787,7 +1787,7 @@ bool TemptableAggregateIterator<Profiler>::Init() {
17871787
*/
17881788
if (error != 0 && error != HA_ERR_RECORD_IS_THE_SAME) {
17891789
if (move_table_to_disk(error, /*insert_operation=*/false)) {
1790-
end_unique_index.commit();
1790+
end_unique_index.release();
17911791
return true;
17921792
}
17931793
/*
@@ -1888,7 +1888,7 @@ bool TemptableAggregateIterator<Profiler>::Init() {
18881888
}
18891889

18901890
if (move_table_to_disk(error, /*insert_operation=*/true)) {
1891-
end_unique_index.commit();
1891+
end_unique_index.release();
18921892
return true;
18931893
}
18941894
} else {
@@ -1898,7 +1898,7 @@ bool TemptableAggregateIterator<Profiler>::Init() {
18981898
}
18991899

19001900
table()->file->ha_index_end();
1901-
end_unique_index.commit();
1901+
end_unique_index.release();
19021902

19031903
table()->materialized = true;
19041904

sql/server_component/persistent_dynamic_loader.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ DEFINE_BOOL_METHOD(mysql_persistent_dynamic_loader_imp::load,
441441
return true;
442442
}
443443

444-
guard.commit();
445-
guard_close_tables.commit();
444+
guard.release();
445+
guard_close_tables.release();
446446
trans_commit_stmt(thd);
447447
return false;
448448
} catch (...) {
@@ -551,7 +551,7 @@ DEFINE_BOOL_METHOD(mysql_persistent_dynamic_loader_imp::unload,
551551
return result;
552552
}
553553

554-
guard_close_tables.commit();
554+
guard_close_tables.release();
555555
trans_commit_stmt(thd);
556556
return false;
557557
} catch (...) {

sql/sql_cmd_srs.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ bool Sql_cmd_create_srs::execute(THD *thd) {
212212

213213
if (dd_client->update(srs)) return true; // Error has already been flagged.
214214

215-
rollback_guard.commit();
215+
rollback_guard.release();
216216
if (commit(thd)) return true; /* purecov: inspected */
217217
my_ok(thd);
218218
return false;
@@ -228,7 +228,7 @@ bool Sql_cmd_create_srs::execute(THD *thd) {
228228

229229
if (thd->dd_client()->store(new_srs.get())) return true;
230230

231-
rollback_guard.commit();
231+
rollback_guard.release();
232232
if (commit(thd)) return true; /* purecov: inspected */
233233
my_ok(thd);
234234
return false;
@@ -274,7 +274,7 @@ bool Sql_cmd_drop_srs::execute(THD *thd) {
274274
}
275275

276276
if (dd_client->drop(srs)) return true; /* purecov: inspected */
277-
rollback_guard.commit();
277+
rollback_guard.release();
278278
if (commit(thd)) return true; /* purecov: inspected */
279279
my_ok(thd);
280280
return false;

sql/sql_prepare.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3276,7 +3276,7 @@ bool Prepared_statement::reprepare(THD *thd) {
32763276
*/
32773277
thd->get_stmt_da()->reset_condition_info(thd);
32783278

3279-
copy_guard.commit();
3279+
copy_guard.release();
32803280

32813281
return false;
32823282
}

sql/sql_select.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4909,7 +4909,7 @@ bool JOIN::make_tmp_tables_info() {
49094909
etc).
49104910
*/
49114911
assert(!query_block->is_recursive() || !tmp_tables);
4912-
cleanup_tmp_tables_on_error.commit();
4912+
cleanup_tmp_tables_on_error.release();
49134913
return false;
49144914
}
49154915

sql/sql_table.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11651,7 +11651,7 @@ bool Sql_cmd_secondary_load_unload::mysql_secondary_load_or_unload(
1165111651
("commit succeeded for alter table %s secondary_%s", full_tab_name,
1165211652
(is_load ? "load" : "unload")));
1165311653
// Transaction committed successfully, no rollback will be necessary.
11654-
rollback_guard.commit();
11654+
rollback_guard.release();
1165511655

1165611656
if (cleanup()) return true;
1165711657

0 commit comments

Comments
 (0)