From 6d4530b453f14c0647fc0be20f772199bf524828 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Wed, 11 May 2022 08:53:35 +0100
Subject: [PATCH] Try some fixes for peer remove

---
 src/peer.cpp     |  1 +
 src/universe.cpp | 18 +++++++++---------
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/src/peer.cpp b/src/peer.cpp
index b248217..4ff16f1 100644
--- a/src/peer.cpp
+++ b/src/peer.cpp
@@ -271,6 +271,7 @@ void Peer::data() {
 		
 		if (rc >= cap - 1) {
 			net_->_notifyError(this, ftl::protocol::Error::kBufferSize, "Too much data received"); 
+			// TODO: Increase the buffer size next time
 		}
 		if (cap < (kMaxMessage / 10)) {
 			net_->_notifyError(this, ftl::protocol::Error::kBufferSize, "Buffer is at capacity"); 
diff --git a/src/universe.cpp b/src/universe.cpp
index f31551c..0ce3da3 100644
--- a/src/universe.cpp
+++ b/src/universe.cpp
@@ -361,9 +361,6 @@ void Universe::_removePeer(PeerPtr &p) {
 		p->status() == NodeStatus::kReconnecting ||
 		p->status() == NodeStatus::kDisconnected)) {
 
-		DLOG(1) << "Removing disconnected peer: " << p->id().to_string();
-		on_disconnect_.triggerAsync(p);
-	
 		auto ix = peer_ids_.find(p->id());
 		if (ix != peer_ids_.end()) peer_ids_.erase(ix);
 
@@ -381,6 +378,10 @@ void Universe::_removePeer(PeerPtr &p) {
 		}
 
 		--connection_count_;
+
+		DLOG(1) << "Removing disconnected peer: " << p->id().to_string();
+		on_disconnect_.triggerAsync(p);
+
 		p.reset();
 	}
 }
@@ -396,9 +397,8 @@ void Universe::_cleanupPeers() {
 			lk.unlock();
 			_removePeer(p);
 			lk.lock();
-		} else {
-			i++;
 		}
+		++i;
 	}
 }
 
@@ -573,19 +573,19 @@ void Universe::_run() {
 
 				const auto &fdstruct = impl_->pollfds[impl_->idMap[sock]];
 
-				if (fdstruct.revents & POLLERR) {
+				/*if (fdstruct.revents & POLLERR) {
 					if (s->socketError()) {
 						//lk.unlock();
 						s->close();
 						//lk.lock();
 						continue;  // No point in reading data...
 					}
-				}
+				}*/
 				//If message received from this client then deal with it
 				if (fdstruct.revents & POLLIN) {
-					//lk.unlock();
+					lk.unlock();
 					s->data();
-					//lk.lock();
+					lk.lock();
 				}
 			}
 		}
-- 
GitLab