This commit is contained in:
Raven Scott
2025-12-20 16:15:31 -05:00
parent 954b98d9ce
commit a2cdd0497d
2 changed files with 45 additions and 46 deletions
Vendored
BIN
View File
Binary file not shown.
+36 -37
View File
@@ -1472,12 +1472,30 @@ async function main() {
// If peer is already connected, check if old connection is still valid // If peer is already connected, check if old connection is still valid
if (connectedPeers.has(peerId)) { if (connectedPeers.has(peerId)) {
const existingChannels = peerChannels.get(peerId); const existingChannels = peerChannels.get(peerId);
const existingConnValid = existingChannels && existingChannels.conn && !existingChannels.conn.destroyed; let existingConnValid = false;
if (existingChannels && existingChannels.conn) {
// More thorough connection validation
const existingConn = existingChannels.conn;
existingConnValid = !existingConn.destroyed &&
existingConn.readable &&
existingConn.writable &&
!existingConn.ended &&
!existingConn.finished;
logDebug('Swarm', `Peer ${peerId} already connected, existing connection state: destroyed=${existingConn.destroyed}, readable=${existingConn.readable}, writable=${existingConn.writable}, ended=${existingConn.ended}, finished=${existingConn.finished}`);
} else {
logDebug('Swarm', `Peer ${peerId} in connectedPeers but no channels entry found`);
}
if (existingConnValid) { if (existingConnValid) {
// Existing connection is still valid - this is a true duplicate, reject the new one // Existing connection is still valid - this is a true duplicate, reject the new one
logWarn('Swarm', `Peer ${peerId} already has a valid connection, rejecting duplicate connection`); logWarn('Swarm', `Peer ${peerId} already has a valid connection, rejecting duplicate connection`);
try {
conn.destroy(); conn.destroy();
} catch (err) {
logDebug('Swarm', `Error destroying duplicate connection: ${err.message}`);
}
return; return;
} else { } else {
// Existing connection is dead/invalid - clean it up and accept the new one // Existing connection is dead/invalid - clean it up and accept the new one
@@ -1489,16 +1507,28 @@ async function main() {
// Clear timeouts // Clear timeouts
if (existingChannels.inviteRequestTimeout) { if (existingChannels.inviteRequestTimeout) {
clearTimeout(existingChannels.inviteRequestTimeout); clearTimeout(existingChannels.inviteRequestTimeout);
existingChannels.inviteRequestTimeout = null;
} }
if (existingChannels.reconnectTimeout) { if (existingChannels.reconnectTimeout) {
clearTimeout(existingChannels.reconnectTimeout); clearTimeout(existingChannels.reconnectTimeout);
existingChannels.reconnectTimeout = null;
} }
// Close existing connection if it's still open // Close existing connection if it's still open
if (existingChannels.conn && !existingChannels.conn.destroyed) { if (existingChannels.conn && !existingChannels.conn.destroyed) {
logDebug('Swarm', `Closing stale connection for peer ${peerId}`); logDebug('Swarm', `Closing stale connection for peer ${peerId}`);
try {
existingChannels.conn.end();
// Give it a moment to close gracefully, then destroy if needed
setTimeout(() => {
if (!existingChannels.conn.destroyed) {
existingChannels.conn.destroy(); existingChannels.conn.destroy();
} }
}, 100);
} catch (err) {
logDebug('Swarm', `Error closing stale connection: ${err.message}`);
}
}
// Handle plugin channel disconnection // Handle plugin channel disconnection
try { try {
@@ -1519,7 +1549,7 @@ async function main() {
connectedPeers.delete(peerId); connectedPeers.delete(peerId);
// Wait a brief moment for cleanup to complete // Wait a brief moment for cleanup to complete
await new Promise(resolve => setTimeout(resolve, 50)); await new Promise(resolve => setTimeout(resolve, 200));
} }
} }
@@ -1975,41 +2005,10 @@ async function main() {
// Handle duplicate connection errors gracefully - these are expected during reconnections // Handle duplicate connection errors gracefully - these are expected during reconnections
if (errMsg.includes('Duplicate connection') || errMsg.includes('duplicate')) { if (errMsg.includes('Duplicate connection') || errMsg.includes('duplicate')) {
logWarn('Swarm', `Duplicate connection detected for peer ${peerId}, cleaning up and accepting new connection`); logDebug('Swarm', `Duplicate connection error for peer ${peerId} - letting main connection logic handle it`);
// Don't interfere with duplicate connection handling - let the main connection logic handle it
// Clean up this connection since it's a duplicate // The main logic already properly detects and handles duplicate connections
try { return; // Don't log as error, don't do disconnect handling
const channels = peerChannels.get(peerId);
if (channels && channels.inviteRequestTimeout) {
clearTimeout(channels.inviteRequestTimeout);
channels.inviteRequestTimeout = null;
}
if (channels && channels.reconnectTimeout) {
clearTimeout(channels.reconnectTimeout);
channels.reconnectTimeout = null;
}
// Handle plugin channel disconnection
try {
const channelManager = require('./includes/plugins/channel-manager');
channelManager.handlePeerDisconnect(peerId);
} catch (pluginErr) {
logDebug('Swarm', `Error handling plugin channel disconnect during duplicate cleanup: ${pluginErr.message}`);
}
// Remove from peer channels
peerChannels.delete(peerId);
connectedPeers.delete(peerId);
// Destroy the duplicate connection
conn.removeAllListeners();
if (!conn.destroyed) {
conn.destroy();
}
} catch (cleanupErr) {
logDebug('Swarm', `Error during duplicate connection cleanup: ${cleanupErr.message}`);
}
return; // Don't log as error, don't do full disconnect handling
} }
// Handle common benign network errors - these are normal in P2P networks // Handle common benign network errors - these are normal in P2P networks