Skip to content

Commit 150ee29

Browse files
committed
test
1 parent f63fe0c commit 150ee29

File tree

6 files changed

+35
-10
lines changed

6 files changed

+35
-10
lines changed

include/libp2p/muxer/yamux/yamux_stream.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ namespace libp2p::connection {
133133
closeCompleted();
134134

135135
/// Underlying connection (secured)
136-
std::shared_ptr<connection::SecureConnection> connection_;
136+
std::weak_ptr<connection::SecureConnection> connection_;
137137

138138
/// Yamux-specific interface of connection
139139
YamuxStreamFeedback &feedback_;

include/libp2p/muxer/yamux/yamuxed_connection.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ namespace libp2p::connection {
7878
ReadCallbackFunc cb) override;
7979
void deferWriteCallback(std::error_code ec, WriteCallbackFunc cb) override;
8080

81+
void markAsRegistered();
82+
8183
private:
8284
using Streams = std::unordered_map<StreamId, std::shared_ptr<YamuxStream>>;
8385

@@ -241,6 +243,8 @@ namespace libp2p::connection {
241243

242244
bool close_after_write_ = false;
243245

246+
bool registered_in_manager_ = false;
247+
244248
public:
245249
LIBP2P_METRICS_INSTANCE_COUNT_IF_ENABLED(
246250
libp2p::connection::YamuxedConnection);

src/muxer/yamux/yamux_stream.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ namespace libp2p::connection {
3030
uint32_t stream_id,
3131
size_t maximum_window_size,
3232
size_t write_queue_limit)
33-
: connection_(std::move(connection)),
33+
: connection_(connection),
3434
feedback_(feedback),
3535
stream_id_(stream_id),
3636
window_size_(YamuxFrame::kInitialWindowSize),
3737
peers_window_size_(YamuxFrame::kInitialWindowSize),
3838
maximum_window_size_(maximum_window_size),
3939
write_queue_(write_queue_limit) {
40-
assert(connection_);
40+
assert(connection);
4141
assert(stream_id_ > 0);
4242
assert(window_size_ <= maximum_window_size_);
4343
assert(peers_window_size_ <= maximum_window_size_);
@@ -151,19 +151,31 @@ namespace libp2p::connection {
151151
}
152152

153153
outcome::result<peer::PeerId> YamuxStream::remotePeerId() const {
154-
return connection_->remotePeer();
154+
if (auto conn = connection_.lock()) {
155+
return conn->remotePeer();
156+
}
157+
return Error::STREAM_RESET_BY_HOST;
155158
}
156159

157160
outcome::result<bool> YamuxStream::isInitiator() const {
158-
return connection_->isInitiator();
161+
if (auto conn = connection_.lock()) {
162+
return conn->isInitiator();
163+
}
164+
return Error::STREAM_RESET_BY_HOST;
159165
}
160166

161167
outcome::result<multi::Multiaddress> YamuxStream::localMultiaddr() const {
162-
return connection_->localMultiaddr();
168+
if (auto conn = connection_.lock()) {
169+
return conn->localMultiaddr();
170+
}
171+
return Error::STREAM_RESET_BY_HOST;
163172
}
164173

165174
outcome::result<multi::Multiaddress> YamuxStream::remoteMultiaddr() const {
166-
return connection_->remoteMultiaddr();
175+
if (auto conn = connection_.lock()) {
176+
return conn->remoteMultiaddr();
177+
}
178+
return Error::STREAM_RESET_BY_HOST;
167179
}
168180

169181
void YamuxStream::increaseSendWindow(size_t delta) {

src/muxer/yamux/yamuxed_connection.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ namespace libp2p::connection {
553553
cb(notify_streams_code);
554554
}
555555

556-
if (closed_callback_) {
556+
if (closed_callback_ && registered_in_manager_) {
557557
closed_callback_(remote_peer_, shared_from_this());
558558
}
559559

@@ -779,4 +779,8 @@ namespace libp2p::connection {
779779
},
780780
config_.ping_interval);
781781
}
782+
783+
void YamuxedConnection::markAsRegistered() {
784+
registered_in_manager_ = true;
785+
}
782786
} // namespace libp2p::connection

src/network/impl/connection_manager_impl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,14 @@ namespace libp2p::network {
148148
}
149149
auto it = connections_.find(peer_id);
150150
if (it == connections_.end()) {
151-
log()->error("inconsistency in onConnectionClosed, peer not found");
151+
log()->warn("onConnectionClosed called for peer {} that was not in connection manager (connection may have been closed before being added)", peer_id.toBase58());
152152
return;
153153
}
154154

155155
[[maybe_unused]] auto erased = it->second.erase(conn);
156156
if (erased == 0) {
157-
log()->error("inconsistency in onConnectionClosed, connection not found");
157+
log()->warn("onConnectionClosed called for connection to peer {} that was not in connection manager (connection may have been closed before being added)", peer_id.toBase58());
158+
return;
158159
}
159160

160161
if (it->second.empty()) {

src/network/impl/listener_manager_impl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <libp2p/network/impl/listener_manager_impl.hpp>
88

99
#include <libp2p/log/logger.hpp>
10+
#include <libp2p/muxer/yamux/yamuxed_connection.hpp>
1011

1112
namespace libp2p::network {
1213

@@ -240,6 +241,9 @@ namespace libp2p::network {
240241
});
241242

242243
// store connection
244+
if (auto yamux_conn = std::dynamic_pointer_cast<connection::YamuxedConnection>(conn)) {
245+
yamux_conn->markAsRegistered();
246+
}
243247
this->cmgr_->addConnectionToPeer(id, conn);
244248
}
245249

0 commit comments

Comments
 (0)