-
Notifications
You must be signed in to change notification settings - Fork 21
task: add task_context propagation support (v2) #271
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
base: v26.1.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * This file is open source software, licensed to you under the terms | ||
| * of the Apache License, Version 2.0 (the "License"). See the NOTICE file | ||
| * distributed with this work for additional information regarding copyright | ||
| * ownership. You may not use this file except in compliance with the License. | ||
| * | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| /* | ||
| * Copyright 2026 Redpanda Data, Inc. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <seastar/core/future.hh> | ||
| #include <seastar/core/task.hh> | ||
| #include <seastar/util/defer.hh> | ||
|
|
||
| namespace seastar { | ||
|
|
||
| /// \addtogroup future-util | ||
| /// @{ | ||
|
|
||
| /// \brief Run a callable with \c ctx installed as the current task context. | ||
| /// | ||
| /// TLS is set to \c ctx for the duration of \c func's synchronous | ||
| /// execution, so tasks created inline (coroutine promises, \c .then() | ||
| /// continuations, etc.) inherit \c ctx at construction and carry it | ||
| /// through their own suspensions via their own \c task._context. The | ||
| /// caller's \c task._context is never modified, so this is safe | ||
| /// regardless of what task is running when invoked. | ||
| /// | ||
| /// \param ctx context to install; empty clears the context for the scope. | ||
| /// \param func callable returning a future; its async work inherits \c ctx. | ||
| /// \param args forwarded to \c func. | ||
| template <typename Func, typename... Args> | ||
| inline | ||
| auto | ||
| with_context([[maybe_unused]] const lw_shared_ptr<task_context>& ctx, | ||
| Func&& func, Args&&... args) noexcept { | ||
| #ifdef SEASTAR_TASK_CONTEXT | ||
| auto* prev = current_task_context(); | ||
| set_current_task_context(ctx.get()); | ||
| auto restore = defer([prev] () noexcept { | ||
| set_current_task_context(prev); | ||
| }); | ||
| #endif | ||
| return futurize_invoke(std::forward<Func>(func), std::forward<Args>(args)...); | ||
| } | ||
|
|
||
| /// @} | ||
|
|
||
| } // namespace seastar |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2744,6 +2744,9 @@ bool reactor::task_queue::run_tasks() { | |||||||||||
| STAP_PROBE(seastar, reactor_run_tasks_single_start); | ||||||||||||
| internal::task_histogram_add_task(*tsk); | ||||||||||||
| r._current_task = tsk; | ||||||||||||
| #ifdef SEASTAR_TASK_CONTEXT | ||||||||||||
| internal::current_task_context_ref() = tsk->context_ptr(); | ||||||||||||
| #endif | ||||||||||||
| tsk->run_and_dispose(); | ||||||||||||
| r._current_task = nullptr; | ||||||||||||
|
||||||||||||
| r._current_task = nullptr; | |
| r._current_task = nullptr; | |
| #ifdef SEASTAR_TASK_CONTEXT | |
| internal::current_task_context_ref() = nullptr; | |
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seastar_TASK_CONTEXTdefaults toON, but enabling it increases everyseastar::taskby an extra pointer-sized field (and adds per-task-construction work viainherit_task_context()). This changes baseline memory footprint/ABI for all consumers, even those not using task context.Consider defaulting this option to
OFF(similar toSeastar_TASK_BACKTRACE) and letting users opt in explicitly, unless there’s a strong compatibility/performance reason to enable by default.