Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(mount): address review findings on push state and token retry

Raul427dd03d94cd
diff --git a/apps/erp/app/modules/settings/ui/Integrations/MountPublishStatus.tsx b/apps/erp/app/modules/settings/ui/Integrations/MountPublishStatus.tsx
index e03606a93c..f613c7ca53 100644
--- a/apps/erp/app/modules/settings/ui/Integrations/MountPublishStatus.tsx
+++ b/apps/erp/app/modules/settings/ui/Integrations/MountPublishStatus.tsx
@@ -19,6 +19,8 @@ import { path } from "~/utils/path";
import type { IntegrationActionState } from "./IntegrationForm";
const POLL_INTERVAL_MS = 3000;
+/** How long a Push may wait for its run to start while no other run is busy. */
+const STARTING_TIMEOUT_MS = 60_000;
const ISSUES_SHOWN = 5;
const ACTIONS = Object.entries(MOUNT_PUBLISH_ACTION_IDS) as Array<
@@ -36,18 +38,24 @@ export function useMountActionStates(metadata: Record<string, unknown>) {
const { revalidate } = useRevalidator();
const records = useMemo(() => parseMountPublishRecords(metadata), [metadata]);
- // The request id of the run this page started, per action. It is what
- // the person did here, not a copy of the loader data.
- const [started, setStarted] = useState<Record<string, string>>({});
+ // The request id of the run this page started, and when, per action. It
+ // is what the person did here, not a copy of the loader data.
+ const [started, setStarted] = useState<
+ Record<string, { requestId: string; at: number }>
+ >({});
const announced = useRef(new Set<string>());
+ // Advanced while polling, so the give-up check below re-runs.
+ const [now, setNow] = useState(() => Date.now());
+
const actionStates: Record<string, IntegrationActionState> = {};
let polling = false;
for (const [entityType, actionId] of ACTIONS) {
const record = records[entityType];
- const requestId = started[actionId];
+ const entry = started[actionId];
// Started here, but the job has not picked it up yet.
- const starting = requestId !== undefined && record?.requestId !== requestId;
+ const starting =
+ entry !== undefined && record?.requestId !== entry.requestId;
const running = starting || record?.status === "running";
if (running) polling = true;
@@ -65,14 +73,48 @@ export function useMountActionStates(metadata: Record<string, unknown>) {
useEffect(() => {
if (!polling) return;
- const id = setInterval(() => revalidate(), POLL_INTERVAL_MS);
+ const id = setInterval(() => {
+ revalidate();
+ setNow(Date.now());
+ }, POLL_INTERVAL_MS);
return () => clearInterval(id);
}, [polling, revalidate]);
+ // A Push whose run never reaches the job (the job service is down, or the
+ // event was lost) leaves no record to wait for, so it is given up after a
+ // while. Only one run per company goes at a time, so the wait counts from
+ // when the last run stopped: a Push queued behind another keeps waiting.
+ const lastBusyAt = useRef(0);
+ useEffect(() => {
+ const busy = ACTIONS.some(
+ ([entityType]) => records[entityType]?.status === "running"
+ );
+ if (busy) {
+ lastBusyAt.current = now;
+ return;
+ }
+ const stalled = ACTIONS.filter(([entityType, actionId]) => {
+ const entry = started[actionId];
+ return (
+ entry !== undefined &&
+ records[entityType]?.requestId !== entry.requestId &&
+ now - Math.max(entry.at, lastBusyAt.current) > STARTING_TIMEOUT_MS
+ );
+ }).map(([, actionId]) => actionId);
+ if (stalled.length === 0) return;
+
+ toast.error(t`The push to Mount did not start. Try again.`);
+ setStarted((current) => {
+ const next = { ...current };
+ for (const actionId of stalled) delete next[actionId];
+ return next;
+ });
+ }, [now, records, started, t]);
+
// Say when a run this page started has ended.
useEffect(() => {
for (const [entityType, actionId] of ACTIONS) {
- const requestId = started[actionId];
+ const requestId = started[actionId]?.requestId;
const record = records[entityType];
if (
!requestId ||
@@ -99,7 +141,10 @@ export function useMountActionStates(metadata: Record<string, unknown>) {
(actionId: string, response: Record<string, unknown>) => {
const requestId = response.requestId;
if (typeof requestId !== "string") return;
- setStarted((current) => ({ ...current, [actionId]: requestId }));
+ setStarted((current) => ({
+ ...current,
+ [actionId]: { requestId, at: Date.now() }
+ }));
revalidate();
},
[revalidate]
diff --git a/packages/ee/src/mount/lib/client.test.ts b/packages/ee/src/mount/lib/client.test.ts
new file mode 100644
index 0000000000..810586c1d2
--- /dev/null
+++ b/packages/ee/src/mount/lib/client.test.ts
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: LicenseRef-Carbon-Commercial
+// Carbon Enterprise file, licensed only under the Carbon Commercial License
+// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.
+
+import {
+ AxiosError,
+ AxiosHeaders,
+ type AxiosRequestConfig,
+ type AxiosResponse
+} from "axios";
+import { describe, expect, it, vi } from "vitest";
+
+vi.mock("@carbon/auth/client.server", () => ({
+ getCarbonServiceRole: () => ({})
+}));
+vi.mock("../../integrations/secrets", () => ({
+ resolveIntegrationSecrets: vi.fn()
+}));
+vi.mock("./service", () => ({
+ getMountIntegration: vi.fn(),
+ MOUNT_INTEGRATION_ID: "mount"
+}));
+
+const { MountClient } = await import("./client");
+
+const COMPANY_ID = "company_1";
+
+function forbidden() {
+ const config = { headers: new AxiosHeaders() };
+ return new AxiosError(
+ "Request failed with status code 403",
+ "ERR_BAD_REQUEST",
+ config,
+ {},
+ {
+ status: 403,
+ statusText: "",
+ data: "",
+ headers: {},
+ config
+ } as AxiosResponse
+ );
+}
+
+/**
+ * A Mount that issues t1, t2, … and refuses t1 once `refuseFirstToken` is
+ * set. Each refusal waits until the test releases it, so the test decides
+ * the order in which concurrent requests learn of their 403.
+ */
+function createMount() {
+ const client = new MountClient();
+ vi.spyOn(client, "getSettings").mockResolvedValue({
+ clientId: "client",
+ clientSecret: "secret",
+ tenant: "tenant"
+ });
+
+ let issued = 0;
+ const pendingRefusals: Array<() => void> = [];
+ const state = { refuseFirstToken: false };
+
+ client.instance.request = vi.fn(async (config: AxiosRequestConfig) => {
+ if (config.url === "/auth/v2/token") {
+ issued += 1;
+ return {
+ data: { accessToken: `t${issued}`, expiresAt: "2999-01-01T00:00:00Z" }
+ };
+ }
+ const token = String(config.headers?.Authorization).replace("Bearer ", "");
+ if (token === "t1" && state.refuseFirstToken) {
+ await new Promise<void>((release) => pendingRefusals.push(release));
+ throw forbidden();
+ }
+ return { data: [] };
+ }) as typeof client.instance.request;
+
+ return {
+ client,
+ state,
+ issued: () => issued,
+ releaseNextRefusal: () => pendingRefusals.shift()?.(),
+ refusalsWaiting: () => pendingRefusals.length
+ };
+}
+
+describe("MountClient 403 handling", () => {
+ it("re-exchanges a stale token once, even when a parallel request already did", async () => {
+ const mount = createMount();
+ // A token issued before the client's Member ID was set in Mount.
+ await mount.client.listCompanyTypes(COMPANY_ID);
+ mount.state.refuseFirstToken = true;
+
+ const first = mount.client.listCompanyTypes(COMPANY_ID);
+ const second = mount.client.listCompanyTypes(COMPANY_ID);
+ await vi.waitFor(() => expect(mount.refusalsWaiting()).toBe(2));
+
+ // The first 403 replaces t1 with t2, a token whose 403 would be final.
+ mount.releaseNextRefusal();
+ await expect(first).resolves.toEqual([]);
+
+ // The second request sent t1 too; its 403 is stale, not final.
+ mount.releaseNextRefusal();
+ await expect(second).resolves.toEqual([]);
+ expect(mount.issued()).toBe(2);
+ });
+});
diff --git a/packages/ee/src/mount/lib/client.ts b/packages/ee/src/mount/lib/client.ts
index 410ce3d9c8..f43fb7616b 100644
--- a/packages/ee/src/mount/lib/client.ts
+++ b/packages/ee/src/mount/lib/client.ts
@@ -268,6 +268,11 @@ export class MountClient {
): Promise<T> {
const settings = await this.getSettings(companyId);
const accessToken = await this.getAccessToken(companyId, settings);
+ // Read with the token, not when its request fails: by then another
+ // request may have replaced it with one whose 403 is final.
+ const cached = this.tokens.get(companyId);
+ const forbiddenIsFinal =
+ cached?.accessToken === accessToken && cached.forbiddenIsFinal;
const domainId = withDomain
? await this.resolveDomainId(companyId, settings)
: null;
@@ -300,11 +305,14 @@ export class MountClient {
// token from before an admin set the client's Member ID or granted that
// member access. Re-exchange once per token for that; a 403 on a token
// issued after one is a real permission problem.
- const staleForbidden =
- status === 403 && !this.tokens.get(companyId)?.forbiddenIsFinal;
+ const staleForbidden = status === 403 && !forbiddenIsFinal;
if (retryOnUnauthorized && (status === 401 || staleForbidden)) {
- this.invalidateToken(companyId);
- if (staleForbidden) this.reissueAfterForbidden.add(companyId);
+ // When another request already replaced this token, replay with
+ // that one instead of exchanging again.
+ if (this.tokens.get(companyId)?.accessToken === accessToken) {
+ this.invalidateToken(companyId);
+ if (staleForbidden) this.reissueAfterForbidden.add(companyId);
+ }
return await this.request<T>(companyId, config, {
retryOnUnauthorized: false,
withDomain
diff --git a/packages/ee/src/mount/lib/run.ts b/packages/ee/src/mount/lib/run.ts
index a461674e90..20dc649cb5 100644
--- a/packages/ee/src/mount/lib/run.ts
+++ b/packages/ee/src/mount/lib/run.ts
@@ -83,7 +83,9 @@ export async function runMountPublishBatch({
const stored = (metadata ?? {}) as StoredSettings;
let current = parseMountPublishRecords(metadata)[entityType];
- if (batch === 0 || current?.runId !== run.runId) {
+ // A record with this run's id already belongs to it: a retried first batch
+ // keeps what the earlier attempt recorded.
+ if (current?.runId !== run.runId) {
current = startPublishRecord(current, run);
await writePublishRecord(serviceRole, companyId, entityType, current);
}
diff --git a/packages/jobs/src/inngest/functions/integrations/mount-publish.ts b/packages/jobs/src/inngest/functions/integrations/mount-publish.ts
index 57985b932a..49bcd8fb4e 100644
--- a/packages/jobs/src/inngest/functions/integrations/mount-publish.ts
+++ b/packages/jobs/src/inngest/functions/integrations/mount-publish.ts
@@ -105,6 +105,7 @@ export const mountPublishFunction = inngest.createFunction(
const records: PublishedRecords = [];
for (const entityType of payload.entityTypes) {
+ let last: MountPublishRecord | null = null;
for (let batch = 0; batch < MOUNT_PUBLISH_MAX_BATCHES; batch++) {
// The sweep is idempotent, so a retried batch replaying records that
// already published is harmless.
@@ -126,14 +127,12 @@ export const mountPublishFunction = inngest.createFunction(
return { skipped: "integration-inactive" as const };
}
- if (result.final) {
- records.push({
- entityType,
- record: result.record as MountPublishRecord
- });
- break;
- }
+ last = result.record as MountPublishRecord;
+ if (result.final) break;
}
+ // After the loop rather than on the final batch, so an entity type is
+ // reported even if the loop's cap and isFinalBatch's ever disagree.
+ if (last) records.push({ entityType, record: last });
}
log.info("Mount publish complete", {
diff --git a/packages/locale/locales/de/erp.po b/packages/locale/locales/de/erp.po
index 2315e27a34..e5d6f5f420 100644
--- a/packages/locale/locales/de/erp.po
+++ b/packages/locale/locales/de/erp.po
@@ -21031,6 +21031,9 @@ msgstr "Der Provider bietet keine Dimensionsziele. Aktivieren Sie die Funktion z
msgid "The purchase order"
msgstr "Die Bestellung"
+msgid "The push to Mount did not start. Try again."
+msgstr ""
+
msgid "The push to Mount failed"
msgstr ""
diff --git a/packages/locale/locales/en/erp.po b/packages/locale/locales/en/erp.po
index 4dbfc0b2a0..e0f6925bb7 100644
--- a/packages/locale/locales/en/erp.po
+++ b/packages/locale/locales/en/erp.po
@@ -21031,6 +21031,9 @@ msgstr "The provider offers no dimension targets. Enable the feature in the prov
msgid "The purchase order"
msgstr "The purchase order"
+msgid "The push to Mount did not start. Try again."
+msgstr "The push to Mount did not start. Try again."
+
msgid "The push to Mount failed"
msgstr "The push to Mount failed"
diff --git a/packages/locale/locales/es/erp.po b/packages/locale/locales/es/erp.po
index 1bc93d4bc0..dbab0129cd 100644
--- a/packages/locale/locales/es/erp.po
+++ b/packages/locale/locales/es/erp.po
@@ -21031,6 +21031,9 @@ msgstr "El proveedor no ofrece destinos de dimensión. Habilita la función en e
msgid "The purchase order"
msgstr "La orden de compra"
+msgid "The push to Mount did not start. Try again."
+msgstr ""
+
msgid "The push to Mount failed"
msgstr ""
diff --git a/packages/locale/locales/fr/erp.po b/packages/locale/locales/fr/erp.po
index 6533ca7c9d..a9a10b02d8 100644
--- a/packages/locale/locales/fr/erp.po
+++ b/packages/locale/locales/fr/erp.po
@@ -21031,6 +21031,9 @@ msgstr "Le fournisseur n'offre pas de cibles de dimension. Activez d'abord la fo
msgid "The purchase order"
msgstr "Le bon de commande"
+msgid "The push to Mount did not start. Try again."
+msgstr ""
+
msgid "The push to Mount failed"
msgstr ""
diff --git a/packages/locale/locales/hi/erp.po b/packages/locale/locales/hi/erp.po
index 2d3b3f8c5b..5b8227c804 100644
--- a/packages/locale/locales/hi/erp.po
+++ b/packages/locale/locales/hi/erp.po
@@ -21031,6 +21031,9 @@ msgstr "प्रदाता कोई आयाम लक्ष्य प्
msgid "The purchase order"
msgstr "क्रय आदेश"
+msgid "The push to Mount did not start. Try again."
+msgstr ""
+
msgid "The push to Mount failed"
msgstr ""
diff --git a/packages/locale/locales/it/erp.po b/packages/locale/locales/it/erp.po
index 5d0303128c..616fa060ae 100644
--- a/packages/locale/locales/it/erp.po
+++ b/packages/locale/locales/it/erp.po
@@ -21031,6 +21031,9 @@ msgstr "Il provider non offre target di dimensione. Abilita prima la funzione ne
msgid "The purchase order"
msgstr "L'ordine di acquisto"
+msgid "The push to Mount did not start. Try again."
+msgstr ""
+
msgid "The push to Mount failed"
msgstr ""
diff --git a/packages/locale/locales/ja/erp.po b/packages/locale/locales/ja/erp.po
index 7ceba4a907..3151bccdc6 100644
--- a/packages/locale/locales/ja/erp.po
+++ b/packages/locale/locales/ja/erp.po
@@ -21031,6 +21031,9 @@ msgstr "プロバイダーはディメンションターゲットを提供して
msgid "The purchase order"
msgstr "発注書"
+msgid "The push to Mount did not start. Try again."
+msgstr ""
+
msgid "The push to Mount failed"
msgstr ""
diff --git a/packages/locale/locales/ko/erp.po b/packages/locale/locales/ko/erp.po
index bd58816487..65ce3ad4a4 100644
--- a/packages/locale/locales/ko/erp.po
+++ b/packages/locale/locales/ko/erp.po
@@ -21031,6 +21031,9 @@ msgstr "제공자가 차원 대상을 제공하지 않습니다. 먼저 제공
msgid "The purchase order"
msgstr "발주서"
+msgid "The push to Mount did not start. Try again."
+msgstr ""
+
msgid "The push to Mount failed"
msgstr ""
diff --git a/packages/locale/locales/pl/erp.po b/packages/locale/locales/pl/erp.po
index e9018e706f..72ccaed99d 100644
--- a/packages/locale/locales/pl/erp.po
+++ b/packages/locale/locales/pl/erp.po
@@ -21031,6 +21031,9 @@ msgstr "Dostawca nie oferuje celów wymiarów. Najpierw włącz funkcję u dosta
msgid "The purchase order"
msgstr "Zamówienie zakupu"
+msgid "The push to Mount did not start. Try again."
+msgstr ""
+
msgid "The push to Mount failed"
msgstr ""
diff --git a/packages/locale/locales/pt/erp.po b/packages/locale/locales/pt/erp.po
index 316d35df4d..c22c93f1d4 100644
--- a/packages/locale/locales/pt/erp.po
+++ b/packages/locale/locales/pt/erp.po
@@ -21031,6 +21031,9 @@ msgstr "O provedor não oferece alvos de dimensão. Habilite o recurso no proved
msgid "The purchase order"
msgstr "O pedido de compra"
+msgid "The push to Mount did not start. Try again."
+msgstr ""
+
msgid "The push to Mount failed"
msgstr ""
diff --git a/packages/locale/locales/ru/erp.po b/packages/locale/locales/ru/erp.po
index 2269d33fcf..70f8a36e01 100644
--- a/packages/locale/locales/ru/erp.po
+++ b/packages/locale/locales/ru/erp.po
@@ -21031,6 +21031,9 @@ msgstr "Поставщик не предлагает целевые показа
msgid "The purchase order"
msgstr "Заказ поставщику"
+msgid "The push to Mount did not start. Try again."
+msgstr ""
+
msgid "The push to Mount failed"
msgstr ""
diff --git a/packages/locale/locales/tr/erp.po b/packages/locale/locales/tr/erp.po
index 16106ab8d6..aa4d3ab6ef 100644
--- a/packages/locale/locales/tr/erp.po
+++ b/packages/locale/locales/tr/erp.po
@@ -21031,6 +21031,9 @@ msgstr "Sağlayıcı boyut hedefleri sunmaz. Önce sağlayıcıda özelliği etk
msgid "The purchase order"
msgstr "Satın alma siparişi"
+msgid "The push to Mount did not start. Try again."
+msgstr ""
+
msgid "The push to Mount failed"
msgstr ""
diff --git a/packages/locale/locales/zh/erp.po b/packages/locale/locales/zh/erp.po
index 089e165e3c..a2c35f0f18 100644
--- a/packages/locale/locales/zh/erp.po
+++ b/packages/locale/locales/zh/erp.po
@@ -21031,6 +21031,9 @@ msgstr "提供商不提供维度目标。首先在提供商中启用该功能(
msgid "The purchase order"
msgstr "采购订单"
+msgid "The push to Mount did not start. Try again."
+msgstr ""
+
msgid "The push to Mount failed"
msgstr ""