:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(workflows): keep a broadcast launch going when the person leaves the page (#128883)
Michael Reichenbacha0a056f32864
diff --git a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.tsindex 715c699bdea4..680a8a9bbff7 100644--- a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts+++ b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts@@ -738,6 +738,70 @@ describe('broadcastWizardLogic', () => {})+ it.each([+ { where: 'stays on the page', leaves: false, path: '/broadcasts/broadcast-1' },+ { where: 'leaves while it sends', leaves: true, path: '/broadcasts' },+ ])('finishes the launch and keeps the broadcast live when the person $where', async ({ leaves, path }) => {+ latest = savedBroadcast({ name: '', subject: 'Spring sale', updatedAt: '2026-09-24T10:00:00Z' })+ const statusPatches: string[] = []+ let batchJobStarted: () => void = () => {}+ const batchJobRequested = new Promise<void>((resolve) => {+ batchJobStarted = resolve+ })+ let releaseBatchJob: () => void = () => {}+ const batchJobHeld = new Promise<void>((resolve) => {+ releaseBatchJob = resolve+ })+ useMocks({+ get: { '/api/projects/:team_id/hog_flows/:id/batch_jobs/': { results: [] } },+ post: {+ '/api/projects/:team_id/hog_flows/:id/batch_jobs/': async () => {+ batchJobStarted()+ await batchJobHeld+ return [201, { id: 'job-1', status: 'queued' }]+ },+ },+ patch: {+ '/api/projects/:team_id/hog_flows/:id/': async ({ request }) => {+ const { status } = (await request.json()) as { status?: string }+ if (status) {+ statusPatches.push(status)+ }+ return [200, { ...latest, status: status ?? latest.status }]+ },+ },+ })+ const capture = jest.spyOn(posthog, 'capture')+ router.actions.push('/broadcasts/new')+ logic.actions.setEmail({+ ...DEFAULT_BROADCAST_EMAIL,+ from: { ...DEFAULT_BROADCAST_EMAIL.from, integrationId: 1 },+ subject: 'Spring sale',+ html: '<p>Hi</p>',+ })+ releaseCreate()+ logic.actions.setStep('review')++ logic.actions.launchBroadcast()+ await batchJobRequested+ if (leaves) {+ logic.unmount()+ router.actions.push('/broadcasts')+ }+ releaseBatchJob()+ await expectLogic(logic).toDispatchActions(['launchBroadcastFinished'])++ expect(statusPatches).toEqual(['active'])+ expect(capture).toHaveBeenCalledWith(+ 'broadcast launched',+ expect.objectContaining({ broadcast_id: 'broadcast-1', schedule_mode: 'now' })+ )+ expect(router.values.location.pathname).toMatch(new RegExp(`${path}$`))+ if (leaves) {+ logic.mount()+ }+ })+it('refreshes the realtime cohort status until the cohort is ready, past a failed check', async () => {const states = ['needs_attention', 'error', 'ready']let calls = 0diff --git a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.tsindex 89aa4169f19e..3c67c7a33716 100644--- a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts+++ b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts@@ -1968,8 +1968,10 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([showSavedDraftUrl: () => {// On /broadcasts/new a reload starts over and orphans the saved draft. The new URL remounts the// wizard from the saved copy, so an unsaved email edit moves it only once its autosave lands.+ // A launch holds this logic mounted after the person leaves, so check they are still on the page.if (props.id === 'new' &&+ router.values.location.pathname.endsWith(urls.broadcast('new')) &&!cache.handedOffToWorkflowEditor &&!cache.emailEditPending &&values.broadcastId &&@@ -2243,118 +2245,158 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([}},launchBroadcast: async (_, breakpoint) => {- // Launch sends the email as saved, so a brand that arrives after this click must not change the preview.- cache.brandClosed = true- if (!values.currentProjectId) {- actions.launchBroadcastFinished()- return- }- if (values.firstInvalidStep) {- actions.launchBroadcastFinished()- actions.setStep(values.firstInvalidStep)- return- }- const projectId = String(values.currentProjectId)- // The cohort cards poll, so a list can finish or fail between the last poll and this click.- const cohortErrors = await loadAudienceCohortLaunchErrors(projectId, values.audienceProperties)- breakpoint()- actions.setAudienceCohortLaunchErrors(cohortErrors)- const blockingErrors = blockingCohortLaunchErrors(cohortErrors, values.blastRadius?.realtime_cohort?.id)- if (blockingErrors.length > 0) {- lemonToast.error(blockingErrors[0])- actions.launchBroadcastFinished()- return- }- // Same ordering as Continue: a save that trails the content step must land first.- cache.autosaveConflict = false- const saves = getSaveQueue(cache, values)- await saves.whenIdle()- breakpoint()- if (cache.autosaveConflict) {- captureLaunchFailed(values.broadcastId, 'edited_elsewhere')- actions.launchBroadcastFinished()- return- }- let broadcastId = values.broadcastId- let activated: HogFlowApi | null = null+ // Leaving the page while the launch runs unmounts this logic, and reading `values` from an+ // unmounted logic throws. Hold a mount until the launch ends, so the send still finishes.+ const releaseMount = broadcastWizardLogic(props).mount()+ const launchedFrom = router.values.location.pathnametry {- // Save the latest edits (creating the draft if the user skipped ahead).- const saved = await saves.run(() =>- broadcastId ? saveWithoutClobbering(projectId, broadcastId, values) : createDraft(projectId, values)- )- broadcastId = saved.id- actions.saveBroadcastFinished(saved)-- // A fresh audience preview mints the confirm token the batch dispatch expects.- const blastRadius = await hogFlowsUserBlastRadiusCreate(- projectId,- audiencePreviewRequest(values.audienceProperties, values.channel)- )-- // The resolver silently caps the batch at this limit, so dispatching an audience over- // it would confirm N recipients and deliver to the first slice. Stop at the step that- // can fix it. Mirrors the guard on the workflow editor's manual trigger.- if (blastRadius.limit != null && blastRadius.affected > blastRadius.limit) {- captureLaunchFailed(broadcastId, 'audience_over_limit')+ // Launch sends the email as saved, so a brand that arrives after this click must not change the preview.+ cache.brandClosed = true+ if (!values.currentProjectId) {+ actions.launchBroadcastFinished()+ return+ }+ if (values.firstInvalidStep) {+ actions.launchBroadcastFinished()+ actions.setStep(values.firstInvalidStep)+ return+ }+ const projectId = String(values.currentProjectId)+ // The cohort cards poll, so a list can finish or fail between the last poll and this click.+ const cohortErrors = await loadAudienceCohortLaunchErrors(projectId, values.audienceProperties)+ breakpoint()+ actions.setAudienceCohortLaunchErrors(cohortErrors)+ const blockingErrors = blockingCohortLaunchErrors(cohortErrors, values.blastRadius?.realtime_cohort?.id)+ if (blockingErrors.length > 0) {+ lemonToast.error(blockingErrors[0])+ actions.launchBroadcastFinished()+ return+ }+ // Same ordering as Continue: a save that trails the content step must land first.+ cache.autosaveConflict = false+ const saves = getSaveQueue(cache, values)+ await saves.whenIdle()+ breakpoint()+ if (cache.autosaveConflict) {+ captureLaunchFailed(values.broadcastId, 'edited_elsewhere')actions.launchBroadcastFinished()- actions.setStep('recipients')- actions.showSavedDraftUrl()- lemonToast.error(- `This project can send a broadcast to up to ${humanFriendlyNumber(- blastRadius.limit- )} people right now. Add filters to narrow the audience, then launch again.`,- {- button: {- label: 'See sending limits',- action: () => router.actions.push(urls.workflows('reputation')),- dataAttr: 'broadcast-launch-limit-see-sending-limits',- },- }- )return}+ let broadcastId = values.broadcastId+ let activated: HogFlowApi | null = null+ try {+ // Save the latest edits (creating the draft if the user skipped ahead).+ const saved = await saves.run(() =>+ broadcastId+ ? saveWithoutClobbering(projectId, broadcastId, values)+ : createDraft(projectId, values)+ )+ broadcastId = saved.id+ actions.saveBroadcastFinished(saved)- // The assistant can edit the draft during the audience request. Activate only the saved- // version, so the send never goes out with an email the user did not see. Schedule- // changes don't bump the flow's updated_at, so the base stays valid across the swap.- const toActivate = broadcastId- const activate = (): Promise<HogFlowApi> =>- saves.run(() =>- patchWithoutClobbering(projectId, toActivate, { status: 'active' }, saved.updated_at)+ // A fresh audience preview mints the confirm token the batch dispatch expects.+ const blastRadius = await hogFlowsUserBlastRadiusCreate(+ projectId,+ audiencePreviewRequest(values.audienceProperties, values.channel))- // An old schedule would fire alongside the new one, and a paused one can't be resumed, so- // launch replaces them. The flow stays a draft until the swap is done, and the scheduler- // skips drafts, so a failure part way never leaves two live schedules or none.- const oldScheduleIds = (values.broadcast?.schedules ?? []).map((existing) => existing.id)- // Stop before touching schedules if the assistant saved meanwhile. Otherwise the guarded- // activation below would 409 after the old schedule is already gone.- const current = await hogFlowsRetrieve(projectId, toActivate)- if (current.updated_at !== saved.updated_at) {- throw new EditedElsewhereError(current)- }- if (values.scheduleMode === 'now') {- for (const id of oldScheduleIds) {- await hogFlowsSchedulesDestroy(projectId, broadcastId, id)++ // The resolver silently caps the batch at this limit, so dispatching an audience over+ // it would confirm N recipients and deliver to the first slice. Stop at the step that+ // can fix it. Mirrors the guard on the workflow editor's manual trigger.+ if (blastRadius.limit != null && blastRadius.affected > blastRadius.limit) {+ captureLaunchFailed(broadcastId, 'audience_over_limit')+ actions.launchBroadcastFinished()+ actions.setStep('recipients')+ actions.showSavedDraftUrl()+ lemonToast.error(+ `This project can send a broadcast to up to ${humanFriendlyNumber(+ blastRadius.limit+ )} people right now. Add filters to narrow the audience, then launch again.`,+ {+ button: {+ label: 'See sending limits',+ action: () => router.actions.push(urls.workflows('reputation')),+ dataAttr: 'broadcast-launch-limit-see-sending-limits',+ },+ }+ )+ return}- activated = await activate()- const batchJob = await hogFlowsBatchJobsCreate(projectId, broadcastId, {- hog_flow: broadcastId,- variables: {},- confirm_token: blastRadius.confirm_token,- } as any)- lemonToast.success(- batchJob.status === 'waiting'- ? 'Broadcast will send once its cohort finishes calculating'- : 'Broadcast is sending'- )- } else {- await hogFlowsSchedulesCreate(projectId, broadcastId, wizardSchedule(values) as any)- for (const id of oldScheduleIds) {- await hogFlowsSchedulesDestroy(projectId, broadcastId, id)++ // The assistant can edit the draft during the audience request. Activate only the saved+ // version, so the send never goes out with an email the user did not see. Schedule+ // changes don't bump the flow's updated_at, so the base stays valid across the swap.+ const toActivate = broadcastId+ const activate = (): Promise<HogFlowApi> =>+ saves.run(() =>+ patchWithoutClobbering(projectId, toActivate, { status: 'active' }, saved.updated_at)+ )+ // An old schedule would fire alongside the new one, and a paused one can't be resumed, so+ // launch replaces them. The flow stays a draft until the swap is done, and the scheduler+ // skips drafts, so a failure part way never leaves two live schedules or none.+ const oldScheduleIds = (values.broadcast?.schedules ?? []).map((existing) => existing.id)+ // Stop before touching schedules if the assistant saved meanwhile. Otherwise the guarded+ // activation below would 409 after the old schedule is already gone.+ const current = await hogFlowsRetrieve(projectId, toActivate)+ if (current.updated_at !== saved.updated_at) {+ throw new EditedElsewhereError(current)+ }+ if (values.scheduleMode === 'now') {+ for (const id of oldScheduleIds) {+ await hogFlowsSchedulesDestroy(projectId, broadcastId, id)+ }+ activated = await activate()+ const batchJob = await hogFlowsBatchJobsCreate(projectId, broadcastId, {+ hog_flow: broadcastId,+ variables: {},+ confirm_token: blastRadius.confirm_token,+ } as any)+ lemonToast.success(+ batchJob.status === 'waiting'+ ? 'Broadcast will send once its cohort finishes calculating'+ : 'Broadcast is sending'+ )+ } else {+ await hogFlowsSchedulesCreate(projectId, broadcastId, wizardSchedule(values) as any)+ for (const id of oldScheduleIds) {+ await hogFlowsSchedulesDestroy(projectId, broadcastId, id)+ }+ activated = await activate()+ lemonToast.success('Broadcast scheduled')+ }+ } catch (error: any) {+ if (error instanceof EditedElsewhereError) {+ captureLaunchFailed(broadcastId, 'edited_elsewhere')+ actions.applyExternalEdit(error.latest, values.broadcast)+ actions.launchBroadcastFinished()+ lemonToast.info(+ 'This email changed while you were editing it. Review the latest version, then launch.'+ )+ actions.showSavedDraftUrl()+ return}- activated = await activate()- lemonToast.success('Broadcast scheduled')+ if (activated && broadcastId) {+ // Activation landed but the send did not. An active broadcast with no job and no+ // schedule is read-only, so leaving it there would strand it with no way to retry.+ try {+ await hogFlowsPartialUpdate(projectId, broadcastId, { status: 'draft' })+ } catch {+ lemonToast.error(+ 'The broadcast is still active but has nothing scheduled. Reload the page.'+ )+ }+ }+ captureLaunchFailed(broadcastId, 'error')+ actions.launchBroadcastFinished()+ lemonToast.error(+ `Couldn't launch the broadcast: ${error?.detail || error?.message || 'unknown error'}`+ )+ // The launch saved the draft first, so a reload of /broadcasts/new would orphan it.+ actions.showSavedDraftUrl()+ return}+ // The send is out from here on. Moving the broadcast back to draft now would cancel every+ // queued recipient run, so the steps below stay outside the rollback above.// Resuming a draft launches from the broadcast's own URL, so the router push below is a// no-op there. Store the activated broadcast so the scene swaps to the read-only summary// instead of leaving a live send button on a broadcast that already went out.@@ -2389,32 +2431,11 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([})actions.loadBatchJobs()actions.launchBroadcastFinished()- router.actions.push(urls.broadcast(broadcastId))- } catch (error: any) {- if (error instanceof EditedElsewhereError) {- captureLaunchFailed(broadcastId, 'edited_elsewhere')- actions.applyExternalEdit(error.latest, values.broadcast)- actions.launchBroadcastFinished()- lemonToast.info(- 'This email changed while you were editing it. Review the latest version, then launch.'- )- actions.showSavedDraftUrl()- return- }- if (activated && broadcastId) {- // Activation landed but the send did not. An active broadcast with no job and no- // schedule is read-only, so leaving it there would strand it with no way to retry.- try {- await hogFlowsPartialUpdate(projectId, broadcastId, { status: 'draft' })- } catch {- lemonToast.error('The broadcast is still active but has nothing scheduled. Reload the page.')- }+ if (router.values.location.pathname === launchedFrom) {+ router.actions.push(urls.broadcast(broadcastId))}- captureLaunchFailed(broadcastId, 'error')- actions.launchBroadcastFinished()- lemonToast.error(`Couldn't launch the broadcast: ${error?.detail || error?.message || 'unknown error'}`)- // The launch saved the draft first, so a reload of /broadcasts/new would orphan it.- actions.showSavedDraftUrl()+ } finally {+ releaseMount()}},moveToDraft: async () => {