Skip to content

Commit 35496a0

Browse files
authored
[k2] fix error with pending updates (#1337)
1 parent 0106c32 commit 35496a0

4 files changed

Lines changed: 19 additions & 14 deletions

File tree

runtime-light/coroutine/awaitable.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,24 @@ class async_stack_watcher_t {
7676
} // namespace awaitable_impl_
7777

7878
class wait_for_update_t : awaitable_impl_::fork_id_watcher_t, awaitable_impl_::async_stack_watcher_t {
79+
public:
80+
enum class update_kind : uint8_t { read, write };
81+
82+
private:
7983
uint64_t stream_d;
84+
update_kind upd_kind;
8085
SuspendToken suspend_token;
8186
awaitable_impl_::state state{awaitable_impl_::state::init};
8287

8388
public:
84-
explicit wait_for_update_t(uint64_t stream_d_) noexcept
89+
explicit wait_for_update_t(uint64_t stream_d_, update_kind kind_) noexcept
8590
: stream_d(stream_d_),
91+
upd_kind(kind_),
8692
suspend_token(std::noop_coroutine(), WaitEvent::UpdateOnStream{.stream_d = stream_d}) {}
8793

8894
wait_for_update_t(wait_for_update_t&& other) noexcept
8995
: stream_d(std::exchange(other.stream_d, k2::INVALID_PLATFORM_DESCRIPTOR)),
96+
upd_kind(other.upd_kind),
9097
suspend_token(std::exchange(other.suspend_token, std::make_pair(std::noop_coroutine(), WaitEvent::Rechedule{}))),
9198
state(std::exchange(other.state, awaitable_impl_::state::end)) {}
9299

@@ -102,7 +109,12 @@ class wait_for_update_t : awaitable_impl_::fork_id_watcher_t, awaitable_impl_::a
102109

103110
bool await_ready() noexcept {
104111
kphp::log::assertion(state == awaitable_impl_::state::init);
105-
state = InstanceState::get().stream_updated(stream_d) ? awaitable_impl_::state::ready : awaitable_impl_::state::init;
112+
k2::StreamStatus status{};
113+
k2::stream_status(stream_d, std::addressof(status));
114+
state = (upd_kind == update_kind::read && status.read_status == k2::IOStatus::IOAvailable) ||
115+
(upd_kind == update_kind::write && status.write_status == k2::IOStatus::IOAvailable)
116+
? awaitable_impl_::state::ready
117+
: awaitable_impl_::state::init;
106118
return state == awaitable_impl_::state::ready;
107119
}
108120

runtime-light/state/instance-state.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ void InstanceState::process_platform_updates() noexcept {
271271
case ScheduleStatus::Resumed: { // scheduler's resumed a coroutine waiting for update
272272
break;
273273
}
274-
case ScheduleStatus::Skipped: { // no one is waiting for the event yet, so just save it
275-
pending_updates_.insert(stream_d);
274+
case ScheduleStatus::Skipped: { // no one is waiting for the event yet
276275
break;
277276
}
278277
case ScheduleStatus::Error: { // something bad's happened, stop execution
@@ -365,7 +364,6 @@ void InstanceState::release_stream(uint64_t stream_d) noexcept {
365364
standard_stream_ = k2::INVALID_PLATFORM_DESCRIPTOR;
366365
}
367366
opened_streams_.erase(stream_d);
368-
pending_updates_.erase(stream_d); // also erase pending updates if exists
369367
k2::free_descriptor(stream_d);
370368
kphp::log::debug("released a stream {}", stream_d);
371369
}
@@ -377,6 +375,5 @@ void InstanceState::release_all_streams() noexcept {
377375
kphp::log::debug("released a stream {}", stream_d);
378376
}
379377
opened_streams_.clear();
380-
pending_updates_.clear();
381378
incoming_streams_.clear();
382379
}

runtime-light/state/instance-state.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ struct InstanceState final : vk::not_copyable {
8585

8686
void process_platform_updates() noexcept;
8787

88-
bool stream_updated(uint64_t stream_d) const noexcept {
89-
return pending_updates_.contains(stream_d);
90-
}
9188
const unordered_set<uint64_t>& opened_streams() const noexcept {
9289
return opened_streams_;
9390
}
@@ -151,7 +148,6 @@ struct InstanceState final : vk::not_copyable {
151148
uint64_t standard_stream_{k2::INVALID_PLATFORM_DESCRIPTOR};
152149
deque<uint64_t> incoming_streams_;
153150
unordered_set<uint64_t> opened_streams_;
154-
unordered_set<uint64_t> pending_updates_;
155151

156152
static constexpr auto INIT_INSTANCE_ALLOCATOR_SIZE = static_cast<size_t>(16U * 1024U * 1024U); // 16MB
157153
};

runtime-light/streams/streams.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ read_all_from_stream(uint64_t stream_d) noexcept {
4444
}
4545
buffer_size += k2::read(stream_d, STREAM_BATCH_SIZE, buffer + buffer_size);
4646
} else if (status.read_status == k2::IOStatus::IOBlocked) {
47-
co_await wait_for_update_t{stream_d};
47+
co_await wait_for_update_t{stream_d, wait_for_update_t::update_kind::read};
4848
}
4949

5050
} while (status.read_status != k2::IOStatus::IOClosed);
@@ -96,7 +96,7 @@ kphp::coro::task<int32_t> read_exact_from_stream(uint64_t stream_d, char* buffer
9696
if (status.read_status == k2::IOStatus::IOAvailable) {
9797
read += k2::read(stream_d, len - read, buffer + read);
9898
} else if (status.read_status == k2::IOStatus::IOBlocked) {
99-
co_await wait_for_update_t{stream_d};
99+
co_await wait_for_update_t{stream_d, wait_for_update_t::update_kind::read};
100100
} else {
101101
co_return read;
102102
}
@@ -122,7 +122,7 @@ kphp::coro::task<int32_t> write_all_to_stream(uint64_t stream_d, const char* buf
122122
} else if (status.write_status == k2::IOStatus::IOAvailable) {
123123
written += k2::write(stream_d, len - written, buffer + written);
124124
} else if (status.write_status == k2::IOStatus::IOBlocked) {
125-
co_await wait_for_update_t{stream_d};
125+
co_await wait_for_update_t{stream_d, wait_for_update_t::update_kind::write};
126126
} else {
127127
kphp::log::warning("stream closed while writing. Wrote {}. Size {}. Stream {}", written, len, stream_d);
128128
co_return written;
@@ -173,7 +173,7 @@ kphp::coro::task<int32_t> write_exact_to_stream(uint64_t stream_d, const char* b
173173
} else if (status.write_status == k2::IOStatus::IOAvailable) {
174174
written += k2::write(stream_d, len - written, buffer + written);
175175
} else if (status.write_status == k2::IOStatus::IOBlocked) {
176-
co_await wait_for_update_t{stream_d};
176+
co_await wait_for_update_t{stream_d, wait_for_update_t::update_kind::write};
177177
} else {
178178
co_return written;
179179
}

0 commit comments

Comments
 (0)