Describe the bug
Analyze the log of PING pong, the connection has been successful, but it is constantly trying to reconnect, check the traceback, socketio.exceptions.ConnectionError: Already connected
To Reproduce
Please fill the following code example:
Socket.IO server version: 4.1.z
Server
const { setupWorker } = require("@socket.io/sticky");
const crypto = require("crypto");
const randomId = () => crypto.randomBytes(8).toString("hex");
const { RedisSessionStore } = require("./sessionStore");
const sessionStore = new RedisSessionStore(redisClient, redisClient1);
const { RedisMessageStore } = require("./messageStore");
const messageStore = new RedisMessageStore(redisClient);
io.use(async (socket, next) => {
const sessionID = socket.handshake.auth.sessionID;
if (sessionID) {
const session = await sessionStore.findSession(sessionID);
if (session) {
socket.sessionID = sessionID;
socket.userID = session.userID;
socket.username = session.username;
return next();
}
}
const auth_info = socket.handshake.auth.auth_info;
if (auth_info == 'admin'){
socket.userID = -1
socket.username = 'admin';
socket.room_code = 'admin_-1'
return next();
}
console.log(auth_info,33, auth_info == 'admin')
if (!auth_info) {
return next(new Error("invalid auth_info"));
}
socket.sessionID = randomId();
// socket.userID = randomId();
user_info = await sessionStore.findUserIdForToken(auth_info);
console.log(user_info, auth_info)
if (!user_info){
console.log(64)
return next(new Error("invalid auth_info not found"));
}
const [uid, source] = user_info
socket.userID = uid
socket.username = source;
socket.room_code = source + '_' + uid
next();
});
io.on("connection", async (socket) => {
// persist session
sessionStore.saveSession(socket.sessionID, {
userID: socket.userID,
username: socket.username,
connected: true,
});
Socket.IO client version: python-socketio 5.7.2
Client
import socketio
sio = socketio.Client(logger=True, engineio_logger=True)
@sio.event
def connect():
print('连接成功')
@sio.event
def connect_error(data):
print(14, data)
# exit()
@sio.event
def disconnect():
print('断开连接了')
@sio.on('private message')
def private_message(data):
print(22, data)
@sio.event
def hello(a, b, c):
print(a, b, c)
@sio.on('*')
def catch_all(event, data):
print(11, event,data, 28)
if __name__ == '__main__':
sio.connect('https://ws.xxxx.com', auth={'auth_info': 'admin'}, wait_timeout=10)
sio.emit('private message', {'body': '1213123', 'to': 'cx_67506'})
sio.wait()
code
def _handle_reconnect(self):
if self._reconnect_abort is None: # pragma: no cover
self._reconnect_abort = self.eio.create_event()
self._reconnect_abort.clear()
reconnecting_clients.append(self)
attempt_count = 0
current_delay = self.reconnection_delay
while True:
delay = current_delay
current_delay *= 2
if delay > self.reconnection_delay_max:
delay = self.reconnection_delay_max
delay += self.randomization_factor * (2 * random.random() - 1)
self.logger.info(
'Connection failed, new attempt in {:.02f} seconds'.format(
delay))
if self._reconnect_abort.wait(delay):
self.logger.info('Reconnect task aborted')
break
attempt_count += 1
try:
self.connect(self.connection_url,
headers=self.connection_headers,
auth=self.connection_auth,
transports=self.connection_transports,
namespaces=self.connection_namespaces,
socketio_path=self.socketio_path)
except (exceptions.ConnectionError, ValueError):
# traceback.print_exc()
pass
else:
self.logger.info('Reconnection successful')
self._reconnect_task = None
break
if self.reconnection_attempts and \
attempt_count >= self.reconnection_attempts:
self.logger.info(
'Maximum reconnection attempts reached, giving up')
break
reconnecting_clients.remove(self)
log
Connection failed, new attempt in 5.49 seconds
Traceback (most recent call last):
File "/Users/dai/Dev/aiguo_python/ven3a-socketio/lib/python3.10/site-packages/socketio/client.py", line 661, in _handle_reconnect
self.connect(self.connection_url,
File "/Users/dai/Dev/aiguo_python/ven3a-socketio/lib/python3.10/site-packages/socketio/client.py", line 307, in connect
raise exceptions.ConnectionError('Already connected')
socketio.exceptions.ConnectionError: Already connected
Connection failed, new attempt in 5.21 seconds
Traceback (most recent call last):
File "/Users/dai/Dev/aiguo_python/ven3a-socketio/lib/python3.10/site-packages/socketio/client.py", line 661, in _handle_reconnect
self.connect(self.connection_url,
File "/Users/dai/Dev/aiguo_python/ven3a-socketio/lib/python3.10/site-packages/socketio/client.py", line 307, in connect
raise exceptions.ConnectionError('Already connected')
socketio.exceptions.ConnectionError: Already connected
Connection failed, new attempt in 5.34 seconds
Traceback (most recent call last):
File "/Users/dai/Dev/aiguo_python/ven3a-socketio/lib/python3.10/site-packages/socketio/client.py", line 661, in _handle_reconnect
self.connect(self.connection_url,
File "/Users/dai/Dev/aiguo_python/ven3a-socketio/lib/python3.10/site-packages/socketio/client.py", line 307, in connect
raise exceptions.ConnectionError('Already connected')
socketio.exceptions.ConnectionError: Already connected
log2
WebSocket upgrade was successful
Received packet MESSAGE data 0{"sid":"gvr4xtgkcIBMFnAzAAAP"}
Namespace / is connected
连接成功
Reconnection successful
Reconnection successful
Connection failed, new attempt in 4.22 seconds
Connection failed, new attempt in 4.12 seconds
Connection failed, new attempt in 5.02 seconds
Connection failed, new attempt in 5.34 seconds
Connection failed, new attempt in 5.02 seconds
Connection failed, new attempt in 5.21 seconds
Connection failed, new attempt in 4.81 seconds
Connection failed, new attempt in 5.21 seconds
Connection failed, new attempt in 4.70 seconds
Connection failed, new attempt in 5.24 seconds
Connection failed, new attempt in 5.39 seconds
Received packet PING data
Sending packet PONG data
Connection failed, new attempt in 5.17 seconds
Connection failed, new attempt in 5.15 seconds
Connection failed, new attempt in 5.46 seconds
Connection failed, new attempt in 5.06 seconds
Connection failed, new attempt in 4.51 seconds
Connection failed, new attempt in 4.81 seconds
Connection failed, new attempt in 5.16 seconds
Platform:
- client
- Device: mac pro
- OS: macos
- server
- Device: pc
- OS: ubuntu20.04
Additional context
Add any other context about the problem here.
question