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

yamux close underlying connection #277

Merged
merged 1 commit into from
Oct 16, 2024
Merged
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
2 changes: 2 additions & 0 deletions include/libp2p/muxer/yamux/yamuxed_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ namespace libp2p::connection {

uint32_t ping_counter_ = 0;

bool close_after_write_ = false;

public:
LIBP2P_METRICS_INSTANCE_COUNT_IF_ENABLED(
libp2p::connection::YamuxedConnection);
Expand Down
20 changes: 13 additions & 7 deletions src/muxer/yamux/yamuxed_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,6 @@ namespace libp2p::connection {

SL_DEBUG(log(), "closing connection, reason: {}", notify_streams_code);

write_queue_.clear();

if (reply_to_peer_code.has_value() && !connection_->isClosed()) {
enqueue(goAwayMsg(reply_to_peer_code.value()));
}

Streams streams;
streams.swap(streams_);

Expand All @@ -560,6 +554,14 @@ namespace libp2p::connection {
if (closed_callback_) {
closed_callback_(remote_peer_, shared_from_this());
}

close_after_write_ = true;
if (reply_to_peer_code) {
enqueue(goAwayMsg(*reply_to_peer_code));
} else {
write_queue_.clear();
std::ignore = connection_->close();
}
}

void YamuxedConnection::writeStreamData(uint32_t stream_id, BytesIn data) {
Expand Down Expand Up @@ -634,6 +636,8 @@ namespace libp2p::connection {
void YamuxedConnection::onDataWritten(outcome::result<size_t> res,
StreamId stream_id) {
if (!res) {
write_queue_.clear();
std::ignore = connection_->close();
// write error
close(res.error(), boost::none);
return;
Expand Down Expand Up @@ -672,10 +676,12 @@ namespace libp2p::connection {

is_writing_ = false;

if (started_ && !write_queue_.empty()) {
if (not write_queue_.empty()) {
auto next_packet = std::move(write_queue_.front());
write_queue_.pop_front();
doWrite(std::move(next_packet));
} else if (close_after_write_) {
std::ignore = connection_->close();
}
}

Expand Down
Loading