Skip to content

Commit 161cd73

Browse files
icecrasher321emir-karabeg
authored andcommitted
fix(sockets): joining currently deleted workflow (#4004)
* fix(sockets): joining currently deleted workflow * address comments
1 parent 0a2a349 commit 161cd73

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

apps/sim/app/workspace/providers/socket-provider.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
166166
const positionUpdateTimeouts = useRef<Map<string, number>>(new Map())
167167
const isRejoiningRef = useRef<boolean>(false)
168168
const pendingPositionUpdates = useRef<Map<string, any>>(new Map())
169+
const deletedWorkflowIdRef = useRef<string | null>(null)
169170

170171
const generateSocketToken = async (): Promise<string> => {
171172
const res = await fetch('/api/auth/socket-token', {
@@ -371,6 +372,7 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
371372

372373
socketInstance.on('workflow-deleted', (data) => {
373374
logger.warn(`Workflow ${data.workflowId} has been deleted`)
375+
deletedWorkflowIdRef.current = data.workflowId
374376
setCurrentWorkflowId((current) => {
375377
if (current === data.workflowId) {
376378
setPresenceUsers([])
@@ -500,7 +502,11 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
500502
if (error?.type === 'SESSION_ERROR') {
501503
const workflowId = urlWorkflowIdRef.current
502504

503-
if (workflowId && !isRejoiningRef.current) {
505+
if (
506+
workflowId &&
507+
!isRejoiningRef.current &&
508+
deletedWorkflowIdRef.current !== workflowId
509+
) {
504510
isRejoiningRef.current = true
505511
logger.info(`Session expired, rejoining workflow: ${workflowId}`)
506512
socketInstance.emit('join-workflow', {
@@ -552,13 +558,25 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
552558
const hydrationPhase = useWorkflowRegistryStore((s) => s.hydration.phase)
553559

554560
useEffect(() => {
555-
if (!socket || !isConnected || !urlWorkflowId) return
561+
if (!socket || !isConnected || !urlWorkflowId) {
562+
if (!urlWorkflowId) {
563+
deletedWorkflowIdRef.current = null
564+
}
565+
return
566+
}
556567

557568
if (hydrationPhase === 'creating') return
558569

559570
// Skip if already in the correct room
560571
if (currentWorkflowId === urlWorkflowId) return
561572

573+
// Prevent rejoining a workflow that was just deleted. The URL param may
574+
// still reference the old workflow while router.push() propagates.
575+
if (deletedWorkflowIdRef.current === urlWorkflowId) {
576+
return
577+
}
578+
deletedWorkflowIdRef.current = null
579+
562580
logger.info(
563581
`URL workflow changed from ${currentWorkflowId} to ${urlWorkflowId}, switching rooms`
564582
)

0 commit comments

Comments
 (0)