test
This commit is contained in:
@@ -1472,34 +1472,64 @@ async function main() {
|
||||
// If peer is already connected, check if old connection is still valid
|
||||
if (connectedPeers.has(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) {
|
||||
// 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`);
|
||||
conn.destroy();
|
||||
try {
|
||||
conn.destroy();
|
||||
} catch (err) {
|
||||
logDebug('Swarm', `Error destroying duplicate connection: ${err.message}`);
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
// Existing connection is dead/invalid - clean it up and accept the new one
|
||||
logInfo('Swarm', `Peer ${peerId} has stale connection entry, cleaning up and accepting new connection`);
|
||||
|
||||
|
||||
if (existingChannels) {
|
||||
// Clean up existing connection
|
||||
try {
|
||||
// Clear timeouts
|
||||
if (existingChannels.inviteRequestTimeout) {
|
||||
clearTimeout(existingChannels.inviteRequestTimeout);
|
||||
existingChannels.inviteRequestTimeout = null;
|
||||
}
|
||||
if (existingChannels.reconnectTimeout) {
|
||||
clearTimeout(existingChannels.reconnectTimeout);
|
||||
existingChannels.reconnectTimeout = null;
|
||||
}
|
||||
|
||||
|
||||
// Close existing connection if it's still open
|
||||
if (existingChannels.conn && !existingChannels.conn.destroyed) {
|
||||
logDebug('Swarm', `Closing stale connection for peer ${peerId}`);
|
||||
existingChannels.conn.destroy();
|
||||
try {
|
||||
existingChannels.conn.end();
|
||||
// Give it a moment to close gracefully, then destroy if needed
|
||||
setTimeout(() => {
|
||||
if (!existingChannels.conn.destroyed) {
|
||||
existingChannels.conn.destroy();
|
||||
}
|
||||
}, 100);
|
||||
} catch (err) {
|
||||
logDebug('Swarm', `Error closing stale connection: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Handle plugin channel disconnection
|
||||
try {
|
||||
const channelManager = require('./includes/plugins/channel-manager');
|
||||
@@ -1507,19 +1537,19 @@ async function main() {
|
||||
} catch (err) {
|
||||
logDebug('Swarm', `Error handling plugin channel disconnect during cleanup: ${err.message}`);
|
||||
}
|
||||
|
||||
|
||||
// Remove from peer channels
|
||||
peerChannels.delete(peerId);
|
||||
} catch (err) {
|
||||
logError('Swarm', `Error cleaning up stale connection for peer ${peerId}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Remove from connected peers
|
||||
connectedPeers.delete(peerId);
|
||||
|
||||
|
||||
// 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
|
||||
if (errMsg.includes('Duplicate connection') || errMsg.includes('duplicate')) {
|
||||
logWarn('Swarm', `Duplicate connection detected for peer ${peerId}, cleaning up and accepting new connection`);
|
||||
|
||||
// Clean up this connection since it's a duplicate
|
||||
try {
|
||||
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
|
||||
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
|
||||
// The main logic already properly detects and handles duplicate connections
|
||||
return; // Don't log as error, don't do disconnect handling
|
||||
}
|
||||
|
||||
// Handle common benign network errors - these are normal in P2P networks
|
||||
|
||||
Reference in New Issue
Block a user