Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): withhold the stop-orphans retry for coded import refusals

Yudhi Armyndharis51dd68092940
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e8efec26d..eade90ab5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- **A refused data import no longer offers a retry that stops live engines** — the dashboard read the `409` status rather than its machine code, so a refusal that only asks the operator to wait presented a confirm whose OK re-ran the replace-all with `stopOrphans=true`.
- **A data import is refused with `409` when another transaction holds the connection** — on SQLite it would otherwise nest inside that transaction and commit into it rather than to disk, so a restore reported as successful could vanish with that transaction's rollback.
- **A second data import while one is running is now refused with `409`** — on SQLite both shared a single transaction, so the second one's rollback discarded a restore the first had already reported as successful.
- **A replace-all data import no longer disturbs the engines it left running** — the restore dropped each session's ownership lease and, on SQLite, the heartbeat could read the half-applied transaction, so a renewal concluded the claim was lost and tore down engines that had never stopped.
diff --git a/dashboard/src/hooks/useDataBackup.ts b/dashboard/src/hooks/useDataBackup.ts
index 6a87e8b25..fc2caac0d 100644
--- a/dashboard/src/hooks/useDataBackup.ts
+++ b/dashboard/src/hooks/useDataBackup.ts
@@ -44,10 +44,11 @@ export function useDataBackup(): DataBackup {
};
// POST the replace-all restore and fold the backend's orphan-engine contract into the UI. The route
- // answers 409 for TWO different reasons and they need opposite handling, so branch on the machine
- // code, never on the status alone: IMPORT_ALREADY_RUNNING means another restore is in flight and
- // there is nothing to decide (retrying with stopOrphans would run a second destructive replace the
- // operator never asked for). The orphan case does have a decision — live engines exist for sessions
+ // answers 409 for several different reasons and they need opposite handling, so branch on the
+ // machine code, never on the status alone: a coded refusal (IMPORT_ALREADY_RUNNING, another restore
+ // in flight; IMPORT_NESTED_TRANSACTION, another transaction holds the connection) leaves nothing to
+ // decide, and retrying it with stopOrphans would run a second destructive replace the operator
+ // never asked for. The orphan case does have a decision — live engines exist for sessions
// the backup would remove — and its preferred retry is stopOrphans=true, which stops them inside
// the request, so that one is offered as a confirm. force=true is deliberately not offered (the api
// client does not even send it): it leaves the engines running until a restart.
diff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.ts
index 18a1e6e99..8cce30447 100644
--- a/dashboard/src/services/api.ts
+++ b/dashboard/src/services/api.ts
@@ -1073,10 +1073,12 @@ export const infraApi = {
counts: Record<string, number>;
}>('/infra/export-data'),
// 200 contract includes the orphan-engine reconciliation result (restartRequired / notices /
- // stopped+failed ids). 409 has TWO causes and the error's `code` distinguishes them:
- // IMPORT_ALREADY_RUNNING (another restore is in flight — nothing to retry), or the orphan case
- // (live engines exist for sessions the backup would remove; the message lists them), which the
- // caller retries with stopOrphans=true to stop those engines inside the request. force is
+ // stopped+failed ids). 409 has several causes and the error's `code` distinguishes them: a coded
+ // refusal is a "not now" with nothing to retry (IMPORT_ALREADY_RUNNING, another restore is in
+ // flight; IMPORT_NESTED_TRANSACTION, another transaction holds the connection), while the
+ // code-less orphan case (live engines exist for sessions the backup would remove; the message
+ // lists them) is the one the caller retries with stopOrphans=true to stop those engines inside
+ // the request. force is
// deliberately NOT exposed: it leaves the engines writing into the restored tables until a
// restart — the window stopOrphans exists to close.
importData: (tables: Record<string, unknown[]>, options?: { stopOrphans?: boolean }) =>
diff --git a/dashboard/src/utils/importRefusal.test.ts b/dashboard/src/utils/importRefusal.test.ts
index 02bc10764..9273d2c5d 100644
--- a/dashboard/src/utils/importRefusal.test.ts
+++ b/dashboard/src/utils/importRefusal.test.ts
@@ -25,8 +25,12 @@ test('leaves every other status alone', () => {
assert.equal(shouldOfferStopOrphansRetry(undefined, undefined, false), false);
});
-test('an unknown future 409 code still gets the orphan treatment, not silence', () => {
- // Fail toward showing the operator something actionable rather than swallowing a refusal we do
- // not recognise; only the code we know needs no decision is excluded.
- assert.equal(shouldOfferStopOrphansRetry(409, 'SOME_FUTURE_CODE', false), true);
+test('never offers the retry when another transaction holds the connection', () => {
+ assert.equal(shouldOfferStopOrphansRetry(409, 'IMPORT_NESTED_TRANSACTION', false), false);
+});
+
+test('an unknown future 409 code does not get the destructive retry', () => {
+ // The non-offered branch is not silence — the caller still toasts the server's message — so the
+ // safe default is to withhold a retry that stops live engines and replaces every table again.
+ assert.equal(shouldOfferStopOrphansRetry(409, 'SOME_FUTURE_CODE', false), false);
});
diff --git a/dashboard/src/utils/importRefusal.ts b/dashboard/src/utils/importRefusal.ts
index d7abdad90..672c9caee 100644
--- a/dashboard/src/utils/importRefusal.ts
+++ b/dashboard/src/utils/importRefusal.ts
@@ -1,14 +1,16 @@
/**
- * POST /infra/import-data answers 409 for two unrelated reasons, and they need opposite handling.
+ * POST /infra/import-data answers 409 for unrelated reasons, and they need opposite handling.
*
* The orphan case is a DECISION: live engines exist for sessions the backup would remove, and the
* documented retry is `stopOrphans=true`, which stops them inside the request. Offering that as a
- * confirm is right.
+ * confirm is right — and it is the only refusal the endpoint throws without a machine `code`.
*
- * IMPORT_ALREADY_RUNNING is not a decision — another restore is in flight and the caller can only
- * wait. Retrying it with `stopOrphans` would run a second destructive replace-all the operator never
- * asked for, from a dialog whose message says "wait". So the status alone must never drive the
- * confirm; the machine code has to be read.
+ * Every coded refusal is a "not now" instead: another restore is in flight, or another transaction
+ * holds the connection. The caller can only wait and retry unchanged. Retrying one with
+ * `stopOrphans` would run a second destructive replace-all the operator never asked for — tearing
+ * down live engines — from a dialog whose message says wait. So the presence of a code, not the
+ * status, decides; an unrecognised code withholds the retry, which costs the operator nothing
+ * because the refusal is still shown as an error toast carrying the server's own message.
*/
export function shouldOfferStopOrphansRetry(
status: number | undefined,
@@ -18,5 +20,5 @@ export function shouldOfferStopOrphansRetry(
if (status !== 409) return false;
// A 409 on the retry itself (an engine started mid-import) must not loop.
if (alreadyRetriedWithStopOrphans) return false;
- return code !== 'IMPORT_ALREADY_RUNNING';
+ return code === undefined;
}