`timed_thread_context`: an operation stopped just before the context is destroyed never completes
维护者通常 1 天内回复
评估
调研方向
The bug is in timed_thread_context::run() in the timed thread scheduler header that the reproducer includes (exec/timed_thread_scheduler.hpp). Read the loop that drains command_queue_ and the stop branch that closes submissions and calls set_stopped_() on the heap. Build the reproducer, which should report 1000 of 1000 lost completions. Done means every operation completes exactly once after a stop request races with destruction. Linked PR #2335 already proposes a fix, so check it before starting.
由索引模型根据 Issue 内容生成。
描述
Summary
When timed_thread_context::run() sees stop_requested_, it closes submissions, stops the operations in the heap and exits, without draining command_queue_ again:
while (true) {
while (command_type* op = command_queue_.pop_front()) { ... } // the only drain
...
cv_.wait_until(lock, deadline, [this] { return ready_ || stop_requested_; });
if (stop_requested) {
// close submissions (CAS n_submissions_in_flight_ to context_closed)
// set_stopped_() on every operation in heap_
break; // commands pushed since the drain above are never processed
}
}
A stop request from another thread raises the operation's ref_count_ from 1 to 2 and queues a stop command. If the context is destroyed before its thread drains that command, the heap pass only drops the count to 1. The stop command, which would drop it to 0 and call set_stopped, is never processed. So the receiver gets neither set_value nor set_stopped, and anything waiting on it (sync_wait, async_scope::on_empty(), ...) hangs.
A schedule command queued after the last drain is lost the same way: it is never inserted into the heap, so the heap pass doesn't stop it.
This breaks the context's own shutdown guarantee: the destructor stops the operations in the heap, and submissions made after close complete inline with set_stopped.
It seems that the current behavior violates https://eel.is/c++draft/exec.async.ops#8: “After its start operation has begun executing, exactly one completion operation shall execute.”
Reproducer
#include <exec/timed_thread_scheduler.hpp>
#include <stdexec/execution.hpp>
#include <atomic>
#include <chrono>
#include <cstdio>
#include <optional>
#include <thread>
namespace ex = STDEXEC;
struct env {
ex::inplace_stop_token token;
auto query(ex::get_stop_token_t) const noexcept { return token; }
};
struct receiver {
using receiver_concept = ex::receiver_tag;
std::atomic<int>* completed;
ex::inplace_stop_token token;
void set_value() noexcept { completed->store(1); }
void set_stopped() noexcept { completed->store(2); }
env get_env() const noexcept { return {token}; }
};
int main() {
int lost = 0;
const int runs = 1000;
for (int i = 0; i < runs; ++i) {
std::atomic<int> completed{0};
ex::inplace_stop_source source;
std::optional<exec::timed_thread_context> ctx{std::in_place};
auto op = ex::connect(exec::schedule_after(ctx->get_scheduler(), std::chrono::hours(1)),
receiver{&completed, source.get_token()});
ex::start(op);
std::this_thread::sleep_for(std::chrono::microseconds(200)); // the operation reaches the heap
source.request_stop(); // queues a stop command
ctx.reset(); // request_stop() + join, usually before the thread drains the command
if (completed.load() == 0) { // the thread has exited, so nothing will ever complete it
++lost;
}
}
std::printf("receiver never completed in %d of %d runs\n", lost, runs);
return lost == 0 ? 0 : 1;
}
The output will be: receiver never completed in 1000 of 1000 runs. It should be 0 of 1000.
Suggested fix: https://github.com/NVIDIA/stdexec/pull/2335
- 主要语言
- C++
- 星标
- 2.5k
- 派生
- 276
- 平均合并
- 1 天 13 小时
- 30 天内合并 PR
- 62
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
NVIDIA/stdexec 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 54/100
维护者通常 1 天内回复
-
continues_on: hop opstate not connected with secondary environment可能已有人在做 @ericniebler 于 24 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 45/100
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 48/100
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 65/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 66/100
leanprover/lean4#15683 ·
维护者通常 1 天内回复
-
[Bug] [geo] Haversine distance calculation can return NaN for antipodal coordinates可能已有人在做 @516665061 今天认领。 未关闭status:needs-triage
难度 2/5 1-3 小时 新手友好度 72/100
PX4/PX4-Autopilot#29211 ·
维护者通常 1 天内回复
-
llvm.memcpy with destination align but no source align asserts "Missed Source alignment!"可能已有人在做 @pvelesko 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
KhronosGroup/SPIRV-LLVM-Translator#4093 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 1 天内回复