Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safeguard for Ticker internal storage that may be changed during callback execution #8820

Merged
merged 4 commits into from
Jan 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion libraries/Ticker/src/Ticker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,28 @@ void Ticker::_static_callback()
}
}

// it is technically allowed to call either schedule or detach
// *during* callback execution. allow both to happen
decltype(_callback) tmp;
std::swap(tmp, _callback);

std::visit([](auto&& callback) {
using T = std::decay_t<decltype(callback)>;
if constexpr (std::is_same_v<T, callback_ptr_t>) {
callback.func(callback.arg);
} else if constexpr (std::is_same_v<T, callback_function_t>) {
callback();
}
}, _callback);
}, tmp);

// ...and move ourselves back only when object is in a valid state
// * ticker was not detached, zeroing timer pointer
// * nothing else replaced callback variant
if ((_timer == nullptr) || !std::holds_alternative<std::monostate>(_callback)) {
return;
}

std::swap(tmp, _callback);

if (_repeat) {
if (_tick) {
Expand Down