Skip to content
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

Fix stream id overflow problem #893

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions rsocket/statemachine/RSocketStateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1190,23 +1190,15 @@ void RSocketStateMachine::setProtocolVersionOrThrow(
}

StreamId RSocketStateMachine::getNextStreamId() {
constexpr auto limit =
static_cast<uint32_t>(std::numeric_limits<int32_t>::max() - 2);

auto const streamId = nextStreamId_;
if (streamId >= limit) {
throw std::runtime_error{"Ran out of stream IDs"};
}

CHECK_EQ(0, streams_.count(streamId))
<< "Next stream ID already exists in the streams map";

nextStreamId_ += 2;
StreamId streamId;
do {
streamId = nextStreamId_.fetch_add(2);
} while( streamId == 0 || streams_.count(streamId) > 0);
return streamId;
}

void RSocketStateMachine::setNextStreamId(StreamId streamId) {
nextStreamId_ = streamId + 2;
nextStreamId_.store(streamId + 2);
}

bool RSocketStateMachine::registerNewPeerStreamId(StreamId streamId) {
Expand Down
2 changes: 1 addition & 1 deletion rsocket/statemachine/RSocketStateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class RSocketStateMachine final
/// Map of all individual stream state machines.
std::unordered_map<StreamId, std::shared_ptr<StreamStateMachineBase>>
streams_;
StreamId nextStreamId_;
std::atomic<StreamId> nextStreamId_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess that should not be atomic since all the events are serialized over event bus

StreamId lastPeerStreamId_{0};

// Manages all state needed for warm/cold resumption.
Expand Down