Skip to content

Commit 7acb869

Browse files
committed
prevent memory leak risk
1 parent bc6cad4 commit 7acb869

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

include/asyncio/gather.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ class GatherAwaiter: NonCopyable {
2828
void await_suspend(std::coroutine_handle<Promise> continuation) noexcept {
2929
continuation_ = &continuation.promise();
3030
// set continuation_ to PENDING, don't schedule anymore, until it resume continuation_
31-
continuation_->set_state(PromiseState::PENDING);
31+
continuation.promise().set_state(PromiseState::PENDING);
32+
}
33+
34+
~GatherAwaiter() {
35+
if (continuation_) {
36+
continuation_->set_state(PromiseState::UNSCHEDULED);
37+
}
3238
}
3339

3440
template<concepts::Awaitable... Futs>

include/asyncio/handle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ enum class PromiseState: uint8_t {
1515
PENDING,
1616
};
1717

18-
struct Handle {
18+
struct Handle { // type erase for EventLoop
1919
virtual void run() = 0;
2020
std::string frame_name() const {
2121
const auto& frame_info = get_frame_info();

include/asyncio/wait_for.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ struct WaitForAwaiter: NonCopyable {
2121
}
2222

2323
template<typename Promise>
24-
void await_suspend(std::coroutine_handle<Promise> caller) noexcept {
25-
continuation_ = &caller.promise();
24+
void await_suspend(std::coroutine_handle<Promise> continuation) noexcept {
25+
continuation_ = &continuation.promise();
2626
// set continuation_ to PENDING, don't schedule anymore, until it resume continuation_
27-
continuation_->set_state(PromiseState::PENDING);
27+
continuation.promise().set_state(PromiseState::PENDING);
28+
}
29+
30+
~WaitForAwaiter() {
31+
if (continuation_) {
32+
continuation_->set_state(PromiseState::UNSCHEDULED);
33+
}
2834
}
2935

3036
template<concepts::Awaitable Fut>

0 commit comments

Comments
 (0)