Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Fix for race condition between project fetchDetails and project.loads()

Joshen Lim82361194f7be
diff --git a/studio/components/layouts/ProjectLayout/ProjectLayout.tsx b/studio/components/layouts/ProjectLayout/ProjectLayout.tsx
index 41bf841aa1d..d154b8db2a7 100644
--- a/studio/components/layouts/ProjectLayout/ProjectLayout.tsx
+++ b/studio/components/layouts/ProjectLayout/ProjectLayout.tsx
@@ -110,12 +110,8 @@ const ContentWrapper: FC<ContentWrapperProps> = observer(({ isLoading, children
const isProjectBuilding = [PROJECT_STATUS.COMING_UP, PROJECT_STATUS.RESTORING].includes(
ui.selectedProject?.status ?? ''
)
-
const isProjectOffline = ui.selectedProject?.postgrestStatus === 'OFFLINE'
- // works if just navigating from homepage, undefined if you go to /project/ref/ and refresh
- console.log(ui.selectedProject)
-
return (
<>
{isLoading || ui.selectedProject === undefined ? (
diff --git a/studio/stores/app/ProjectStore.ts b/studio/stores/app/ProjectStore.ts
index d3504b75cb7..6c972a972ac 100644
--- a/studio/stores/app/ProjectStore.ts
+++ b/studio/stores/app/ProjectStore.ts
@@ -1,4 +1,4 @@
-import { cloneDeep } from 'lodash'
+import { cloneDeep, keyBy } from 'lodash'
import { Project } from 'types'
import { IRootStore } from '../RootStore'
import { constructHeaders } from 'lib/api/apiHelpers'
@@ -24,6 +24,26 @@ export default class ProjectStore extends PostgresMetaInterface<Project> {
super(rootStore, dataUrl, headers, options)
}
+ // [Joshen] Method override here due to race condition between projects.load() and fetchDetail()
+ // There's a race condition that can happen here when these 2 methods are called, such that if
+ // fetchDetail() completes before projects.load() completes, the response from project.loads()
+ // then overrides what fetchDetail() has already populated into the store - causing parameters like
+ // connectionString to become undefined and hence causing the dashboard to stay in a Connecting state.
+ // You can reproduce this by giving a setTimeout to project.loads under withAuth.tsx
+
+ // This thus ensures that the response from project.loads() do not override projects which are already
+ // loaded in the store from a successful fetchDetail() - This should be fine as project.loads() is only
+ // called once in this whole app under withAuth.tsx
+ setDataArray(value: any[]) {
+ const formattedValue = keyBy(value, this.identifier)
+ Object.keys(this.data).forEach((projectId) => {
+ if (formattedValue[projectId] !== undefined) {
+ formattedValue[projectId] = this.data[projectId]
+ }
+ })
+ this.data = formattedValue
+ }
+
async fetchDetail(projectRef: string) {
const url = `${this.url}/${projectRef}`
const headers = constructHeaders(this.headers)