Skip to content

Commit

Permalink
Safeguard for Ticker internal storage that may be changed during call…
Browse files Browse the repository at this point in the history
…back execution (#8820)

Temporarily move callback into the function scope and execute it from there.
Detaching would no longer destroy it, and re-scheduling would no longer be almost immediately cancelled by our code.
  • Loading branch information
mcspr committed Jan 26, 2023
1 parent 9c4a123 commit e25f9e9
Showing 1 changed file with 15 additions and 1 deletion.
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

0 comments on commit e25f9e9

Please sign in to comment.