Skip to content
Merged
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
22 changes: 2 additions & 20 deletions apps/sim/app/workspace/providers/socket-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
const positionUpdateTimeouts = useRef<Map<string, number>>(new Map())
const isRejoiningRef = useRef<boolean>(false)
const pendingPositionUpdates = useRef<Map<string, any>>(new Map())
const deletedWorkflowIdRef = useRef<string | null>(null)

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

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

if (
workflowId &&
!isRejoiningRef.current &&
deletedWorkflowIdRef.current !== workflowId
) {
if (workflowId && !isRejoiningRef.current) {
isRejoiningRef.current = true
logger.info(`Session expired, rejoining workflow: ${workflowId}`)
socketInstance.emit('join-workflow', {
Expand Down Expand Up @@ -558,25 +552,13 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
const hydrationPhase = useWorkflowRegistryStore((s) => s.hydration.phase)

useEffect(() => {
if (!socket || !isConnected || !urlWorkflowId) {
if (!urlWorkflowId) {
deletedWorkflowIdRef.current = null
}
return
}
if (!socket || !isConnected || !urlWorkflowId) return

if (hydrationPhase === 'creating') return

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

// Prevent rejoining a workflow that was just deleted. The URL param may
// still reference the old workflow while router.push() propagates.
if (deletedWorkflowIdRef.current === urlWorkflowId) {
return
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted guard allows rejoining a deleted workflow

Medium Severity

Removing deletedWorkflowIdRef reintroduces a race condition. When a workflow-deleted event fires, currentWorkflowId is set to null, but urlWorkflowId still references the deleted workflow (URL hasn't changed). The useEffect sees currentWorkflowId !== urlWorkflowId and immediately emits join-workflow for the deleted workflow. This also affects the operation-forbidden handler, which will attempt to rejoin a deleted workflow on SESSION_ERROR. Since onWorkflowDeleted has no consumers to trigger navigation, this race will reliably trigger for any user viewing a workflow deleted by someone else.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 95e02c2. Configure here.

}
deletedWorkflowIdRef.current = null

logger.info(
`URL workflow changed from ${currentWorkflowId} to ${urlWorkflowId}, switching rooms`
)
Expand Down
Loading