Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnect debug #10831

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/server/graphql/mutations/setAppLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default {
return url?.includes(meetPrefix) ? url.slice(meetPrefix.length) : null
})

console.log('setAtLocation', connectedSocketStr, JSON.stringify(updatedConnection))
const [lastMeeting, nextMeeting] = await Promise.all([
lastMeetingId ? dataLoader.get('newMeetings').load(lastMeetingId) : undefined,
nextMeetingId ? dataLoader.get('newMeetings').load(nextMeetingId) : undefined,
Expand Down
2 changes: 1 addition & 1 deletion packages/server/graphql/private/mutations/connectSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const connectSocket: MutationResolvers['connectSocket'] = async (
`presence:${userId}`,
JSON.stringify({lastSeenAtURL: null, socketInstanceId, socketId} as UserPresence)
)

console.log('connectSocket pushed presence', socketInstanceId, socketId)
// If this is the first socket, tell everyone they're online
if (socketCount === 1) {
const listeningUserIds = await getListeningUserIds(RedisCommand.ADD, tms, userId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const disconnectSocket: MutationResolvers['disconnectSocket'] = async (
if (!disconnectingSocket) {
// this happens a lot on server restart in dev mode
if (!__PRODUCTION__) return {user}
console.log('disconnectSocket error', socketId, userPresence)
throw new Error('Called disconnect without a valid socket')
}
await redis.lrem(`presence:${userId}`, 0, disconnectingSocket)
Expand Down
1 change: 1 addition & 0 deletions packages/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ process.on('SIGTERM', async (signal) => {
Object.values(activeClients.store).map(async (connectionContext) => {
const disconnectIn = Math.floor(Math.random() * RECONNECT_WINDOW)
await sleep(disconnectIn)
console.log('SIGTERM handleDisconnect', connectionContext.id)
await handleDisconnect(connectionContext)
})
)
Expand Down
10 changes: 8 additions & 2 deletions packages/server/socketHandlers/handleClose.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import {WebSocket} from 'uWebSockets.js'
import {type WebSocketBehavior} from 'uWebSockets.js'
import sendToSentry from '../utils/sendToSentry'
import handleDisconnect from './handleDisconnect'
import {SocketUserData} from './handleOpen'

const handleClose = (ws: WebSocket<SocketUserData>) => {
const handleClose: WebSocketBehavior<SocketUserData>['close'] = (ws, code, message) => {
const userData = ws.getUserData()
const {connectionContext} = userData
if (!connectionContext) return
userData.done = true
try {
console.log(
'handleClose handleDisconnect',
connectionContext.id,
code,
Buffer.from(message).toString('utf-8')
)
handleDisconnect(connectionContext)
} catch (e) {
const error = e instanceof Error ? e : new Error('handleDisconnect failed')
Expand Down
1 change: 1 addition & 0 deletions packages/server/socketHandlers/handleConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const handleConnect = async (connectionContext: ConnectionContext) => {
connectionContext.ready()
return null
}
console.log('handleConnect calling publish', INSTANCE_ID, socketId)
const result = await publishInternalGQL<{data: {connectSocket: {tms: string[]}}}>({
authToken,
ip,
Expand Down
1 change: 1 addition & 0 deletions packages/server/socketHandlers/handleDisconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const handleDisconnect = async (connectionContext: ConnectionContext, options: O
relayUnsubscribeAll(connectionContext)
if (authToken.rol !== 'impersonate') {
const userId = getUserId(authToken)
console.log('handleDisconnect calling publish', socketId)
await publishInternalGQL({authToken, ip, query: disconnectQuery, socketId, variables: {userId}})
}
activeClients.delete(connectionContext.id)
Expand Down
1 change: 1 addition & 0 deletions packages/server/socketHandlers/handleOpen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const handleOpen: WebSocketBehavior<SocketUserData>['open'] = async (socket) =>

// messages will start coming in before handleConnect completes & sit in the readyQueue
const nextAuthToken = await handleConnect(connectionContext)
console.log('sendEncoded message for version', connectionContext.id)
sendEncodedMessage(connectionContext, {version: __APP_VERSION__, authToken: nextAuthToken})
keepAlive(connectionContext)
}
Expand Down
1 change: 1 addition & 0 deletions packages/server/socketHelpers/keepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const keepAlive = (connectionContext: ConnectionContext) => {
clearInterval(connectionContext.cancelKeepAlive!)
connectionContext.cancelKeepAlive = setInterval(() => {
if (connectionContext.isAlive === false) {
console.log('keepAlive handleDisconnect', connectionContext.id)
handleDisconnect(connectionContext)
} else {
connectionContext.isAlive = false
Expand Down
1 change: 1 addition & 0 deletions packages/server/sse/SSEConnectionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const SSEConnectionHandler = uWSAsyncHandler(async (res: HttpResponse, req: Http
if (res.done) return
res.tryEnd(`retry: 1000\n`, 1e8)
sendSSEMessage(res, connectionContext.id, 'id')
console.log('sendEncoded message for version SSE', connectionContext.id)
sendEncodedMessage(connectionContext, {version: __APP_VERSION__, authToken: nextAuthToken})
keepAlive(connectionContext)
}, true)
Expand Down
1 change: 1 addition & 0 deletions packages/server/utils/handleReliableMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const handleReliableMessage = (messageBuffer: Buffer, connectionContext: Connect
if (message) {
sendAndPushToReliableQueue(connectionContext, mid, message)
} else {
console.log('reliable handleDisconnect', connectionContext.id)
handleDisconnect(connectionContext)
}
return
Expand Down
Loading