connection-deadlock, socket: improve socket end conditions

This commit is contained in:
Rajeh Taher
2026-01-18 17:15:38 +02:00
parent db86cdc545
commit 282f065f53
2 changed files with 13 additions and 12 deletions
+12 -11
View File
@@ -673,7 +673,7 @@ export const makeSocket = (config: SocketConfig) => {
it could be that the network is down it could be that the network is down
*/ */
if (diff > keepAliveIntervalMs + 5000) { if (diff > keepAliveIntervalMs + 5000) {
end(new Boom('Connection was lost', { statusCode: DisconnectReason.connectionLost })) void end(new Boom('Connection was lost', { statusCode: DisconnectReason.connectionLost }))
} else if (ws.isOpen) { } else if (ws.isOpen) {
// if its all good, send a keep alive request // if its all good, send a keep alive request
query({ query({
@@ -728,7 +728,7 @@ export const makeSocket = (config: SocketConfig) => {
}) })
} }
end(new Boom(msg || 'Intentional Logout', { statusCode: DisconnectReason.loggedOut })) void end(new Boom(msg || 'Intentional Logout', { statusCode: DisconnectReason.loggedOut }))
} }
const requestPairingCode = async (phoneNumber: string, customPairingCode?: string): Promise<string> => { const requestPairingCode = async (phoneNumber: string, customPairingCode?: string): Promise<string> => {
@@ -828,14 +828,15 @@ export const makeSocket = (config: SocketConfig) => {
await validateConnection() await validateConnection()
} catch (err: any) { } catch (err: any) {
logger.error({ err }, 'error in validating connection') logger.error({ err }, 'error in validating connection')
end(err) void end(err)
} }
}) })
ws.on('error', mapWebSocketError(end)) ws.on('error', mapWebSocketError(end))
ws.on('close', () => end(new Boom('Connection Terminated', { statusCode: DisconnectReason.connectionClosed }))) ws.on('close', () => void end(new Boom('Connection Terminated', { statusCode: DisconnectReason.connectionClosed })))
// the server terminated the connection // the server terminated the connection
ws.on('CB:xmlstreamend', () => ws.on(
end(new Boom('Connection Terminated by Server', { statusCode: DisconnectReason.connectionClosed })) 'CB:xmlstreamend',
() => void end(new Boom('Connection Terminated by Server', { statusCode: DisconnectReason.connectionClosed }))
) )
// QR gen // QR gen
ws.on('CB:iq,type:set,pair-device', async (stanza: BinaryNode) => { ws.on('CB:iq,type:set,pair-device', async (stanza: BinaryNode) => {
@@ -863,7 +864,7 @@ export const makeSocket = (config: SocketConfig) => {
const refNode = refNodes.shift() const refNode = refNodes.shift()
if (!refNode) { if (!refNode) {
end(new Boom('QR refs attempts ended', { statusCode: DisconnectReason.timedOut })) void end(new Boom('QR refs attempts ended', { statusCode: DisconnectReason.timedOut }))
return return
} }
@@ -896,7 +897,7 @@ export const makeSocket = (config: SocketConfig) => {
await sendNode(reply) await sendNode(reply)
} catch (error: any) { } catch (error: any) {
logger.info({ trace: error.stack }, 'error in pairing') logger.info({ trace: error.stack }, 'error in pairing')
end(error) void end(error)
} }
}) })
// login complete // login complete
@@ -956,16 +957,16 @@ export const makeSocket = (config: SocketConfig) => {
const { reason, statusCode } = getErrorCodeFromStreamError(node) const { reason, statusCode } = getErrorCodeFromStreamError(node)
end(new Boom(`Stream Errored (${reason})`, { statusCode, data: reasonNode || node })) void end(new Boom(`Stream Errored (${reason})`, { statusCode, data: reasonNode || node }))
}) })
// stream fail, possible logout // stream fail, possible logout
ws.on('CB:failure', (node: BinaryNode) => { ws.on('CB:failure', (node: BinaryNode) => {
const reason = +(node.attrs.reason || 500) const reason = +(node.attrs.reason || 500)
end(new Boom('Connection Failure', { statusCode: reason, data: node.attrs })) void end(new Boom('Connection Failure', { statusCode: reason, data: node.attrs }))
}) })
ws.on('CB:ib,,downgrade_webclient', () => { ws.on('CB:ib,,downgrade_webclient', () => {
end(new Boom('Multi-device beta not joined', { statusCode: DisconnectReason.multideviceMismatch })) void end(new Boom('Multi-device beta not joined', { statusCode: DisconnectReason.multideviceMismatch }))
}) })
ws.on('CB:ib,,offline_preview', async (node: BinaryNode) => { ws.on('CB:ib,,offline_preview', async (node: BinaryNode) => {
@@ -48,7 +48,7 @@ describe('Connection Deadlock Test', () => {
}) })
) )
sock.end(new Error('Test completed')) await sock.end(new Error('Test completed'))
await clear() await clear()
}) })
}) })