Compare commits

...
Author SHA1 Message Date
Sonarly Claude Code 0df60256ba chore: improve monitoring for Race condition: debounced save fires while workflo
Applied the same `isReady` guard to `useUpdateWorkflowVersionTrigger`, which has the identical race condition pattern — it's called from debounced trigger save callbacks that can fire during transient workflow data loading states. Without this fix, the same "Failed to get updatable workflow version" error would generate noisy Sentry events from trigger editing flows as well.

The code fix itself is the primary monitoring improvement: by preventing the throw during expected transient states, it eliminates the noisy Sentry error events (currently tagged `handled: yes, mechanism: generic`) that provide no actionable signal. The remaining throw in `getUpdatableWorkflowVersion()` still fires for genuine configuration errors (e.g., missing `workflowVisualizerWorkflowId`), preserving alerting for real bugs.
2026-03-07 10:15:04 +00:00
Sonarly Claude Code de31a2822c Race condition: debounced save fires while workflow data is transiently undefined during refetch
https://sonarly.com/issue/5150?type=bug

The `useGetUpdatableWorkflowVersionOrThrow` hook throws "Failed to get updatable workflow version" when a debounced save callback fires during a transient window where `useWorkflowWithCurrentVersion` returns `undefined` — typically caused by an SSE-triggered workflow refetch that changes the version ID and causes a cache miss on the second sequential Apollo query.

Fix: Added an `isReady` boolean to the return value of `useGetUpdatableWorkflowVersionOrThrow` that callers can check synchronously before calling the async `getUpdatableWorkflowVersion()`. This flag is `true` only when both `workflowVisualizerWorkflowId` and `workflow` are defined (i.e., not in a transient loading/refetch state).

Updated `useUpdateStep` and `useUpdateWorkflowVersionTrigger` — the two hooks called from debounced save callbacks — to check `isReady` and return early when the workflow data is temporarily unavailable. This prevents the error from being thrown during the transient window when `useWorkflowWithCurrentVersion` returns `undefined` (e.g., after an SSE-triggered refetch changes the version ID and the second Apollo query has a cache miss).

The `isReady` flag is captured in the same React render cycle as the `workflow` value, so they're always consistent in the closure. When the debounced callback fires, `useDebouncedCallback`'s ref points to the latest callback which captures the latest `isReady` value. If `isReady` is `false`, the save is silently skipped. Once the data loads and the component re-renders with `isReady = true`, the next debounced save succeeds.
2026-03-07 10:15:03 +00:00
5 changed files with 16 additions and 3 deletions
@@ -13,6 +13,9 @@ export const useGetUpdatableWorkflowVersionOrThrow = (instanceId?: string) => {
);
const workflow = useWorkflowWithCurrentVersion(workflowVisualizerWorkflowId);
const isReady =
isDefined(workflowVisualizerWorkflowId) && isDefined(workflow);
const getUpdatableWorkflowVersion = async (): Promise<string> => {
if (!isDefined(workflowVisualizerWorkflowId) || !isDefined(workflow)) {
throw new Error('Failed to get updatable workflow version');
@@ -34,5 +37,5 @@ export const useGetUpdatableWorkflowVersionOrThrow = (instanceId?: string) => {
return draftVersionId;
};
return { getUpdatableWorkflowVersion };
return { getUpdatableWorkflowVersion, isReady };
};
@@ -18,6 +18,7 @@ jest.mock(
jest.mock('@/workflow/hooks/useGetUpdatableWorkflowVersionOrThrow', () => ({
useGetUpdatableWorkflowVersionOrThrow: () => ({
getUpdatableWorkflowVersion: mockGetUpdatableWorkflowVersion,
isReady: true,
}),
}));
@@ -4,12 +4,16 @@ import { useUpdateWorkflowVersionStep } from '@/workflow/workflow-steps/hooks/us
import { useStepsOutputSchema } from '@/workflow/workflow-variables/hooks/useStepsOutputSchema';
export const useUpdateStep = () => {
const { getUpdatableWorkflowVersion } =
const { getUpdatableWorkflowVersion, isReady } =
useGetUpdatableWorkflowVersionOrThrow();
const { updateWorkflowVersionStep } = useUpdateWorkflowVersionStep();
const { markStepForRecomputation } = useStepsOutputSchema();
const updateStep = async (updatedStep: WorkflowAction) => {
if (!isReady) {
return { updatedStep: undefined };
}
const workflowVersionId = await getUpdatableWorkflowVersion();
const result = await updateWorkflowVersionStep({
@@ -16,6 +16,7 @@ jest.mock('@/object-record/hooks/useUpdateOneRecord', () => ({
jest.mock('@/workflow/hooks/useGetUpdatableWorkflowVersionOrThrow', () => ({
useGetUpdatableWorkflowVersionOrThrow: jest.fn(() => ({
getUpdatableWorkflowVersion: mockGetUpdatableWorkflowVersion,
isReady: true,
})),
}));
@@ -9,12 +9,16 @@ import { TRIGGER_STEP_ID } from 'twenty-shared/workflow';
export const useUpdateWorkflowVersionTrigger = () => {
const { updateOneRecord: updateOneWorkflowVersion } = useUpdateOneRecord();
const { getUpdatableWorkflowVersion } =
const { getUpdatableWorkflowVersion, isReady } =
useGetUpdatableWorkflowVersionOrThrow();
const { markStepForRecomputation } = useStepsOutputSchema();
const updateTrigger = async (updatedTrigger: WorkflowTrigger) => {
if (!isReady) {
return;
}
const workflowVersionId = await getUpdatableWorkflowVersion();
await updateOneWorkflowVersion({