Skip to content

Commit bbde2be

Browse files
Vikhyath MondretiVikhyath Mondreti
authored andcommitted
fix(workspace url): race condition
1 parent bf9b3fd commit bbde2be

5 files changed

Lines changed: 62 additions & 21 deletions

File tree

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/workflow.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,28 @@ const WorkflowContent = React.memo(() => {
861861
// Navigate to existing workflow or first available
862862
if (!workflows[currentId]) {
863863
logger.info(`Workflow ${currentId} not found, redirecting to first available workflow`)
864-
router.replace(`/workspace/${workspaceId}/w/${workflowIds[0]}`)
864+
865+
// Validate that workflows belong to the current workspace before redirecting
866+
const workspaceWorkflows = workflowIds.filter(id => {
867+
const workflow = workflows[id]
868+
return workflow.workspaceId === workspaceId
869+
})
870+
871+
if (workspaceWorkflows.length > 0) {
872+
router.replace(`/workspace/${workspaceId}/w/${workspaceWorkflows[0]}`)
873+
} else {
874+
// No valid workflows for this workspace, redirect to workspace root
875+
router.replace(`/workspace/${workspaceId}/w`)
876+
}
877+
return
878+
}
879+
880+
// Validate that the current workflow belongs to the current workspace
881+
const currentWorkflow = workflows[currentId]
882+
if (currentWorkflow && currentWorkflow.workspaceId !== workspaceId) {
883+
logger.warn(`Workflow ${currentId} belongs to workspace ${currentWorkflow.workspaceId}, not ${workspaceId}`)
884+
// Redirect to the correct workspace for this workflow
885+
router.replace(`/workspace/${currentWorkflow.workspaceId}/w/${currentId}`)
865886
return
866887
}
867888

apps/sim/app/workspace/[workspaceId]/w/page.tsx

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,56 @@
11
'use client'
22

3-
import { useEffect } from 'react'
3+
import { useEffect, useState } from 'react'
44
import { useParams, useRouter } from 'next/navigation'
55
import { LoadingAgent } from '@/components/ui/loading-agent'
66
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
77

88
export default function WorkflowsPage() {
99
const router = useRouter()
10-
const { workflows, isLoading } = useWorkflowRegistry()
10+
const { workflows, isLoading, loadWorkflows } = useWorkflowRegistry()
11+
const [hasInitialized, setHasInitialized] = useState(false)
1112

1213
const params = useParams()
13-
const workspaceId = params.workspaceId
14+
const workspaceId = params.workspaceId as string
1415

16+
// Initialize workspace workflows
1517
useEffect(() => {
16-
// Wait for workflows to load
17-
if (isLoading) return
18+
const initializeWorkspace = async () => {
19+
try {
20+
await loadWorkflows(workspaceId)
21+
setHasInitialized(true)
22+
} catch (error) {
23+
console.error('Failed to load workflows for workspace:', error)
24+
setHasInitialized(true) // Still mark as initialized to show error state
25+
}
26+
}
27+
28+
if (!hasInitialized) {
29+
initializeWorkspace()
30+
}
31+
}, [workspaceId, loadWorkflows, hasInitialized])
32+
33+
// Handle redirection once workflows are loaded
34+
useEffect(() => {
35+
// Only proceed if we've initialized and workflows are not loading
36+
if (!hasInitialized || isLoading) return
1837

1938
const workflowIds = Object.keys(workflows)
2039

21-
// If we have workflows, redirect to the first one
22-
if (workflowIds.length > 0) {
23-
router.replace(`/workspace/${workspaceId}/w/${workflowIds[0]}`)
24-
return
25-
}
40+
// Validate that workflows belong to the current workspace
41+
const workspaceWorkflows = workflowIds.filter(id => {
42+
const workflow = workflows[id]
43+
return workflow.workspaceId === workspaceId
44+
})
2645

27-
// If no workflows exist after loading is complete, this means the workspace creation
28-
// didn't work properly or the user doesn't have any workspaces.
29-
// Redirect to home to let the system handle workspace/workflow creation properly.
30-
router.replace('/')
31-
}, [workflows, isLoading, router, workspaceId])
46+
// If we have valid workspace workflows, redirect to the first one
47+
if (workspaceWorkflows.length > 0) {
48+
router.replace(`/workspace/${workspaceId}/w/${workspaceWorkflows[0]}`)
49+
}
50+
}, [hasInitialized, isLoading, workflows, workspaceId, router])
3251

33-
// Show loading state while determining where to redirect
52+
// Always show loading state until redirect happens
53+
// There should always be a default workflow, so we never show "no workflows found"
3454
return (
3555
<div className='flex h-screen items-center justify-center'>
3656
<div className='text-center'>

apps/sim/db/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const connectionString = env.POSTGRES_URL ?? env.DATABASE_URL
2626
const postgresClient = postgres(connectionString, {
2727
prepare: false, // Disable prefetch as it is not supported for "Transaction" pool mode
2828
idle_timeout: 20, // Reduce idle timeout to 20 seconds to free up connections faster
29-
connect_timeout: 10, // Reduce connect timeout to 10 seconds
30-
max: 3, // Conservative limit - with multiple serverless functions, this prevents pool exhaustion
29+
connect_timeout: 30, // Increase connect timeout to 30 seconds to handle network issues
30+
max: 2, // Further reduced limit to prevent Supabase connection exhaustion
3131
onnotice: () => {}, // Disable notices to reduce noise
3232
})
3333

apps/sim/socket-server/database/operations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const socketDb = drizzle(
1515
postgres(connectionString, {
1616
prepare: false,
1717
idle_timeout: 10, // Shorter idle timeout for socket operations
18-
connect_timeout: 5, // Faster connection timeout
18+
connect_timeout: 20, // Increase connection timeout for socket operations
1919
max: 2, // Very small pool for socket server to avoid exhausting Supabase limit
2020
onnotice: () => {}, // Disable notices
2121
debug: false, // Disable debug for socket operations

apps/sim/socket-server/rooms/manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const db = drizzle(
1313
postgres(connectionString, {
1414
prepare: false,
1515
idle_timeout: 15,
16-
connect_timeout: 5,
16+
connect_timeout: 20, // Increase connection timeout for room operations
1717
max: 1, // Minimal pool for room operations to conserve connections
1818
onnotice: () => {},
1919
}),

0 commit comments

Comments
 (0)