Skip to content

fix(timer): resolve service safely and add executor constructors#280

Merged
mvandeberg merged 1 commit into
cppalliance:developfrom
mvandeberg:pr/213-timer-ctor
Jun 17, 2026
Merged

fix(timer): resolve service safely and add executor constructors#280
mvandeberg merged 1 commit into
cppalliance:developfrom
mvandeberg:pr/213-timer-ctor

Conversation

@mvandeberg

Copy link
Copy Markdown
Contributor

Fix UB in the timer constructor (#213). The old timer_service_direct did an unchecked static_cast<io_context&> on a capy::execution_context and dereferenced io_context::timer_svc_, so constructing a timer from any non-io_context (a plain execution_context, a thread_pool, or a user-derived context) read memory at a bogus offset and crashed. Resolve the timer service through the shared io_object::create_handle<timer_service>() path that every other I/O object uses; it throws std::logic_error when the service is absent. Delete the bespoke timer_service_direct helper along with the now-dead timer_service_access struct and io_context::timer_svc_ cache.

Bring timer in line with the other public I/O objects (#231): add executor constructors to both timer and native_timer (Ex const&; Ex const&, time_point; Ex const&, duration), mirroring the tcp_acceptor/signal_set shape and delegating to the executor's context.

For the owning cancel_after/cancel_at path the timer is built inside a noexcept await_suspend from the awaiting coroutine's executor context. A non-io_context executor cannot supply a timer; rather than silently running the operation with no deadline, translate the failure into a clear precondition diagnostic (which aborts by design) and document the requirement on the owning overloads.

Tighten timer_service.hpp to include detail/scheduler.hpp directly instead of pulling it transitively through io_context.hpp.

Add tests for the throw path and for the new executor constructors.

Closes #213

@cppalliance-bot

cppalliance-bot commented Jun 17, 2026

Copy link
Copy Markdown

An automated preview of the documentation is available at https://280.corosio.prtest3.cppalliance.org/index.html

If more commits are pushed to the pull request, the docs will rebuild at the same URL.

2026-06-17 18:43:53 UTC

@cppalliance-bot

cppalliance-bot commented Jun 17, 2026

Copy link
Copy Markdown

GCOVR code coverage report https://280.corosio.prtest3.cppalliance.org/gcovr/index.html
LCOV code coverage report https://280.corosio.prtest3.cppalliance.org/genhtml/index.html
Coverage Diff Report https://280.corosio.prtest3.cppalliance.org/diff-report/index.html

Build time: 2026-06-17 18:55:20 UTC

Fix UB in the timer constructor (cppalliance#213). The old
timer_service_direct did an unchecked static_cast<io_context&> on a
capy::execution_context and dereferenced io_context::timer_svc_, so
constructing a timer from any non-io_context (a plain execution_context,
a thread_pool, or a user-derived context) read memory at a bogus offset
and crashed. Resolve the timer service through the shared
io_object::create_handle<timer_service>() path that every other I/O
object uses; it throws std::logic_error when the service is absent.
Delete the bespoke timer_service_direct helper along with the now-dead
timer_service_access struct and io_context::timer_svc_ cache.

Bring timer in line with the other public I/O objects
(cppalliance#231): add executor constructors to both timer and
native_timer (Ex const&; Ex const&, time_point; Ex const&, duration),
mirroring the tcp_acceptor/signal_set shape and delegating to the
executor's context.

For the owning cancel_after/cancel_at path the timer is built inside a
noexcept await_suspend from the awaiting coroutine's executor context.
A non-io_context executor cannot supply a timer; rather than silently
running the operation with no deadline, translate the failure into a
clear precondition diagnostic (which aborts by design) and document the
requirement on the owning overloads.

Tighten timer_service.hpp to include detail/scheduler.hpp directly
instead of pulling it transitively through io_context.hpp.

Add tests for the throw path and for the new executor constructors.
@mvandeberg mvandeberg merged commit 1b1334f into cppalliance:develop Jun 17, 2026
38 of 39 checks passed
@codecov

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.68%. Comparing base (37451d1) to head (0396347).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #280      +/-   ##
===========================================
- Coverage    77.69%   77.68%   -0.01%     
===========================================
  Files           96       96              
  Lines         7188     7185       -3     
  Branches      1753     1751       -2     
===========================================
- Hits          5585     5582       -3     
  Misses        1099     1099              
  Partials       504      504              
Files with missing lines Coverage Δ
include/boost/corosio/cancel.hpp 100.00% <ø> (ø)
...clude/boost/corosio/detail/cancel_at_awaitable.hpp 100.00% <ø> (ø)
include/boost/corosio/detail/timer_service.hpp 93.05% <ø> (-0.02%) ⬇️
include/boost/corosio/io_context.hpp 96.87% <ø> (ø)
include/boost/corosio/native/native_cancel.hpp 100.00% <ø> (ø)
include/boost/corosio/native/native_timer.hpp 93.93% <ø> (ø)
include/boost/corosio/timer.hpp 100.00% <ø> (ø)
src/corosio/src/timer.cpp 100.00% <ø> (ø)

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 37451d1...0396347. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UB in timer constructor

2 participants