Skip to content

Commit c96d45f

Browse files
authored
yamux close underlying stream (#277)
Signed-off-by: turuslan <[email protected]>
1 parent 84a7f57 commit c96d45f

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

include/libp2p/muxer/yamux/yamuxed_connection.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ namespace libp2p::connection {
239239

240240
uint32_t ping_counter_ = 0;
241241

242+
bool close_after_write_ = false;
243+
242244
public:
243245
LIBP2P_METRICS_INSTANCE_COUNT_IF_ENABLED(
244246
libp2p::connection::YamuxedConnection);

src/muxer/yamux/yamuxed_connection.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,6 @@ namespace libp2p::connection {
537537

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

540-
write_queue_.clear();
541-
542-
if (reply_to_peer_code.has_value() && !connection_->isClosed()) {
543-
enqueue(goAwayMsg(reply_to_peer_code.value()));
544-
}
545-
546540
Streams streams;
547541
streams.swap(streams_);
548542

@@ -560,6 +554,14 @@ namespace libp2p::connection {
560554
if (closed_callback_) {
561555
closed_callback_(remote_peer_, shared_from_this());
562556
}
557+
558+
close_after_write_ = true;
559+
if (reply_to_peer_code) {
560+
enqueue(goAwayMsg(*reply_to_peer_code));
561+
} else {
562+
write_queue_.clear();
563+
std::ignore = connection_->close();
564+
}
563565
}
564566

565567
void YamuxedConnection::writeStreamData(uint32_t stream_id, BytesIn data) {
@@ -634,6 +636,8 @@ namespace libp2p::connection {
634636
void YamuxedConnection::onDataWritten(outcome::result<size_t> res,
635637
StreamId stream_id) {
636638
if (!res) {
639+
write_queue_.clear();
640+
std::ignore = connection_->close();
637641
// write error
638642
close(res.error(), boost::none);
639643
return;
@@ -672,10 +676,12 @@ namespace libp2p::connection {
672676

673677
is_writing_ = false;
674678

675-
if (started_ && !write_queue_.empty()) {
679+
if (not write_queue_.empty()) {
676680
auto next_packet = std::move(write_queue_.front());
677681
write_queue_.pop_front();
678682
doWrite(std::move(next_packet));
683+
} else if (close_after_write_) {
684+
std::ignore = connection_->close();
679685
}
680686
}
681687

0 commit comments

Comments
 (0)