mirror of
https://github.com/supabase/supabase.git
synced 2026-09-06 09:59:03 +08:00
## Context Related to database connections - specifically for cancelling queries or terminating sessions PIDs can be re-used, so a more accurate check is to use both PID and `backend_start` to uniquely identify the session to cancel or terminate <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved query cancellation and session termination reliability by verifying the active database session before taking action. * Prevented actions from affecting a different session that reused the same process ID. * Added clearer guidance to refresh when a session has changed or is no longer available. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
import { getTerminateSessionSQL } from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { sqlKeys } from './keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import { ResponseError, type UseCustomMutationOptions } from '@/types'
|
|
|
|
type SessionTerminateVariables = {
|
|
pid: number
|
|
/** Pass the pid's last-known backend_start to guard against a reused pid matching an unrelated session */
|
|
backendStart?: string
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
}
|
|
|
|
export async function terminateSession({
|
|
pid,
|
|
backendStart,
|
|
projectRef,
|
|
connectionString,
|
|
}: SessionTerminateVariables) {
|
|
const sql = getTerminateSessionSQL({ pid, backendStart })
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['terminate-session'],
|
|
})
|
|
if (backendStart !== undefined && result.length === 0) {
|
|
throw new ResponseError(
|
|
`Session (PID: ${pid}) has already changed since this list was loaded. Refresh and try again.`
|
|
)
|
|
}
|
|
return result
|
|
}
|
|
|
|
type QueryAbortData = Awaited<ReturnType<typeof terminateSession>>
|
|
|
|
export const useSessionTerminateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<QueryAbortData, ResponseError, SessionTerminateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
return useMutation<QueryAbortData, ResponseError, SessionTerminateVariables>({
|
|
mutationFn: (vars) => terminateSession(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: sqlKeys.ongoingQueries(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to terminate session: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|