Skip to content

fix(orchestrator): catch WorkflowExecutionAlreadyStartedError to prevent retry loop#1323

Open
DarinLevesque wants to merge 7 commits into
gitroomhq:mainfrom
DarinLevesque:fix/streak-workflow-idempotency
Open

fix(orchestrator): catch WorkflowExecutionAlreadyStartedError to prevent retry loop#1323
DarinLevesque wants to merge 7 commits into
gitroomhq:mainfrom
DarinLevesque:fix/streak-workflow-idempotency

Conversation

@DarinLevesque
Copy link
Copy Markdown

Fixes #1321

What kind of change does this PR introduce?

Fixes a critical bug where the postSocial activity enters an infinite retry loop if a streakWorkflow is already active for a given organization.

Why was this change needed?

When postSocial successfully posts to a social network, it attempts to start the streakWorkflow. If that streak timer is already running (e.g., from a previous post that day), Temporal throws a WorkflowExecutionAlreadyStartedError. Because this error was unhandled, the entire activity fails after the post goes live, causing Temporal to retry the activity and endlessly duplicate the post on the user's social feeds.

The Fix:

Wrapped the streakWorkflow start call in a try/catch block within post.activity.ts. It now specifically catches WorkflowExecutionAlreadyStartedError (idempotency collision) and safely ignores it, allowing the postSocial activity to return a success signal to Temporal without triggering a double-post loop.

Checklist:

Put a "X" in the boxes below to indicate you have followed the checklist;

  • I have read the CONTRIBUTING guide.
  • I checked that there were not similar issues or PRs already open for this.
  • This PR fixes just ONE issue (do not include multiple issues or types of change in the same PR) For example, don't try and fix a UI issue and include new dependencies in the same PR.

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 17, 2026

@DarinLevesque is attempting to deploy a commit to the Listinai Team on Vercel.

A member of the Team first needs to authorize it.

@DarinLevesque DarinLevesque changed the title fix(orchestrator): catch WorkflowExecutionAlreadyStartedError to prevent retry loop (#1321) fix(orchestrator): catch WorkflowExecutionAlreadyStartedError to prevent retry loop Mar 17, 2026
Comment on lines +220 to +225
`[Ignored] streakWorkflow already started for org: ${integration.organizationId}`,
);
} else {
// Re-throw any genuine infrastructure errors so Temporal can retry appropriately
throw error;
}

This comment was marked as outdated.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The AI agent makes a great catch regarding the root cause of the streak failing to reset, but it misses the architectural priority.

The Root Cause: The bot is correct that 'TERMINATE_EXISTING' was originally passed as an invalid string literal, causing Temporal to silently default to FAIL_EXISTING. I will push an update to this PR to use the proper WorkflowIdConflictPolicy enum so the streak-reset gamification works as intended.

Why the try/catch MUST stay: The bot suggests fixing the root cause instead of catching the error. However, secondary gamification logic must have an error boundary. If the streak timer fails to start for any reason (e.g., Temporal server race conditions, network blips), it currently crashes postSocial after the payload is published to the social network, triggering an infinite double-posting loop.

The try/catch is a mandatory idempotency boundary to decouple external side-effects from internal metrics. I will push the enum fix to this branch to resolve both issues perfectly.

Comment on lines +219 to +228
(error as Error)?.name === 'WorkflowExecutionAlreadyStartedError'
) {
// Safely catch the idempotency collision so the social post activity succeeds
console.warn(
`[Ignored] streakWorkflow already started for org: ${integration.organizationId}`,
);
} else {
// Re-throw any genuine infrastructure errors so Temporal can retry appropriately
throw error;
}

This comment was marked as outdated.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Excellent catch by the Sentry bot. The original implementation passed a string ('TERMINATE_EXISTING') which caused Temporal to silently default to FAIL, throwing the WorkflowExecutionAlreadyStartedError and triggering the infinite retry loop.

Sentry is correct that WorkflowIdConflictPolicy.USE_EXISTING is the proper business logic for a streak timer (preventing the 24-hour clock from resetting on every post). By using the correct Enum, Temporal natively handles the idempotency collision without throwing an error, rendering the try/catch block unnecessary.

I am pushing an update to remove the try/catch and apply the USE_EXISTING policy. This provides a much cleaner, Temporal-native fix.

@egelhaus egelhaus added the contribution:evaluate Evaluate the PR again label May 4, 2026
@postiz-contribution
Copy link
Copy Markdown

postiz-contribution Bot commented May 4, 2026

Hi @DarinLevesque! Thanks for the PR. Contributions to Postiz are gated behind an application. Please apply at https://contribute.postiz.com/p/postiz and we'll reopen this PR once you're approved.

This project also requires a signed Contributor License Agreement. You can sign it now at https://contribute.postiz.com/p/postiz/cla so you're ready as soon as your application is approved.

@postiz-contribution postiz-contribution Bot added contribution:pending Awaiting application review and removed contribution:evaluate Evaluate the PR again labels May 4, 2026
@DarinLevesque
Copy link
Copy Markdown
Author

I'm trying to apply to contribute at the link above: https://contribute.postiz.com/p/postiz

But it's not letting me finish the form, it just spins and never loads the page. Is there another way to get added as a contributor?

@egelhaus
Copy link
Copy Markdown
Collaborator

egelhaus commented May 5, 2026

I'm trying to apply to contribute at the link above: https://contribute.postiz.com/p/postiz

But it's not letting me finish the form, it just spins and never loads the page. Is there another way to get added as a contributor?

Hi @DarinLevesque ,
No, it isn't possible to be added as a contributor currently, at least not for collaborators.
You can DM me on discord if you'd like (@egelhaus) and I can try to figure out a solution.

@postiz-contribution
Copy link
Copy Markdown

Hi @DarinLevesque! Your application for Postiz is awaiting review. We'll reopen this PR once it's approved. Status: https://contribute.postiz.com/p/postiz

This project also requires a signed Contributor License Agreement. You can sign it now at https://contribute.postiz.com/p/postiz/cla so you're ready as soon as your application is approved.

@postiz-contribution postiz-contribution Bot reopened this May 30, 2026
@postiz-contribution
Copy link
Copy Markdown

@DarinLevesque's application for Postiz was approved. Reopening this PR.

@postiz-contribution postiz-contribution Bot added contribution:approved Approved contributor and removed contribution:pending Awaiting application review labels May 30, 2026
@postiz-contribution
Copy link
Copy Markdown

Contribution-checker quality warning
Heuristic score: 0/100 (low). This is a non-blocking warning surfaced by the project's quality settings.

Heuristics that flagged:

  • Excessive inline code references: 10 inline refs (>3)
  • PR doesn't use the repo's PR template: 2 required checkbox items missing (max 1, match ≥80%)
  • Author mismatch across commits: 2 distinct authors
  • Conventional Commits format: 1 non-conv commits
  • Low global merge ratio: 29% (<30%)
  • No public email

If this is a genuine contribution, please add detail to your PR description and tighten the diff scope before reviewers look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contribution:approved Approved contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

**Critical**: Infinite social posting loop caused by unhandled streakWorkflow error in postSocial activity

2 participants