Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Cancel protection assessment batches with their consuming read

pulse-triage[bot]494c4079bfa4
diff --git a/docs/release-control/v6/internal/subsystems/storage-recovery.md b/docs/release-control/v6/internal/subsystems/storage-recovery.md
index a4bc29bb87..f1012e7bdc 100644
--- a/docs/release-control/v6/internal/subsystems/storage-recovery.md
+++ b/docs/release-control/v6/internal/subsystems/storage-recovery.md
@@ -21,6 +21,24 @@
## Purpose
+### Protection assessment batches share the consuming read's lifetime
+
+Every protection-posture batch carries a cancellable transport signal owned by
+the one joined query read. Unmounting, replacing a read, changing the resource
+scope or switching organisations cancels all its pending batches. A failed batch
+cancels its siblings without replacing the original error; only a complete
+successful retry restores assessments. The abort listener is removed when the
+joined read settles. Existing stale-result guards and organisation/cache
+boundaries remain in force even for a transport that ignores cancellation.
+
+The registered `useProtectionPostures.test.ts` and
+`useProtectionPostures.cancellation.test.ts` cover successful shared signals,
+scope/disposal/organisation cancellation, late results, polling, failure and
+full-read recovery. Source-bound browser proof uses the real Backups consumer
+and HTTP transport at desktop and narrow widths. This stops unnecessary stale
+reads; it does not establish native guest recovery or change posture policy,
+batch size, cadence, API, identity or access grants.
+
### Protection posture reads validate the whole response
Backups accepts a batched protection assessment only when every returned row,
diff --git a/frontend-modern/src/hooks/__tests__/useProtectionPostures.cancellation.test.ts b/frontend-modern/src/hooks/__tests__/useProtectionPostures.cancellation.test.ts
new file mode 100644
index 0000000000..f95571a78f
--- /dev/null
+++ b/frontend-modern/src/hooks/__tests__/useProtectionPostures.cancellation.test.ts
@@ -0,0 +1,159 @@
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { createRoot, createSignal } from 'solid-js';
+import { resetCreateNonSuspendingQueryCacheForTest } from '@/hooks/createNonSuspendingQuery';
+import { useProtectionPostures } from '@/hooks/useProtectionPostures';
+import { eventBus } from '@/stores/events';
+import type { ProtectionPosturesResponse } from '@/types/recovery';
+import { protectionPolicy, protectionPosture } from '@/utils/__fixtures__/protectionPosture';
+
+const apiFetchJSONMock = vi.hoisted(() => vi.fn());
+vi.mock('@/utils/apiClient', () => ({ apiFetchJSON: apiFetchJSONMock }));
+
+const fleet = Array.from({ length: 401 }, (_, index) => `vm:${index}`);
+const disposers: (() => void)[] = [];
+
+interface PendingBatch {
+ ids: string[];
+ signal?: AbortSignal;
+ resolve: (response: ProtectionPosturesResponse) => void;
+ reject: (reason: unknown) => void;
+}
+
+// Deliberately allow a cancelled request to resolve late as well: the query's
+// existing stale-result guard must still hold if a transport ignores abort.
+function holdBatches() {
+ const requests: PendingBatch[] = [];
+ apiFetchJSONMock.mockImplementation(
+ (url: string, options?: { signal?: AbortSignal }) =>
+ new Promise<ProtectionPosturesResponse>((resolve, reject) => {
+ requests.push({
+ ids: new URL(url, 'https://pulse.invalid').searchParams.getAll('resourceId'),
+ signal: options?.signal,
+ resolve,
+ reject,
+ });
+ }),
+ );
+ return requests;
+}
+
+function mount(ids: () => readonly string[] = () => fleet) {
+ return createRoot((dispose) => {
+ disposers.push(dispose);
+ return useProtectionPostures(ids);
+ });
+}
+
+function finish(batches: PendingBatch[]) {
+ for (const batch of batches) {
+ batch.resolve({ data: batch.ids.map(protectionPosture), policy: protectionPolicy });
+ }
+}
+
+function expectCancelled(batches: PendingBatch[]) {
+ expect(batches).toHaveLength(3);
+ for (const batch of batches) {
+ expect(batch.signal).toBeInstanceOf(AbortSignal);
+ expect(batch.signal?.aborted).toBe(true);
+ }
+}
+
+afterEach(() => {
+ disposers.splice(0).forEach((dispose) => dispose());
+ vi.useRealTimers();
+ apiFetchJSONMock.mockReset();
+ resetCreateNonSuspendingQueryCacheForTest();
+});
+
+describe('protection posture batch lifetime', () => {
+ it('cancels every pending batch when the consuming surface unmounts', async () => {
+ const requests = holdBatches();
+ mount();
+ await vi.waitFor(() => expect(requests).toHaveLength(3));
+ disposers.pop()!();
+ expectCancelled(requests);
+ });
+
+ it('cancels the previous resource scope without publishing its late assessments', async () => {
+ const requests = holdBatches();
+ const [ids, setIDs] = createSignal<readonly string[]>(fleet);
+ const hook = mount(ids);
+ await vi.waitFor(() => expect(requests).toHaveLength(3));
+ setIDs(['vm:replacement']);
+ await vi.waitFor(() => expect(requests).toHaveLength(4));
+ expectCancelled(requests.slice(0, 3));
+ expect(requests[3].signal?.aborted).toBe(false);
+ finish(requests.slice(3));
+ await vi.waitFor(() => expect(hook.postures()).toHaveLength(1));
+ finish(requests.slice(0, 3));
+ await Promise.resolve();
+ expect(hook.postures().map((posture) => posture.subjectResourceId)).toEqual(['vm:replacement']);
+ expect(hook.response.error).toBeNull();
+ });
+
+ it('cancels the entire pending read on an organisation switch', async () => {
+ const requests = holdBatches();
+ const hook = mount();
+ await vi.waitFor(() => expect(requests).toHaveLength(3));
+ eventBus.emit('org_switched', 'replacement-org');
+ expectCancelled(requests.slice(0, 3));
+ expect(requests).toHaveLength(6);
+ expect(hook.postures()).toEqual([]);
+ expect(hook.policy().freshnessWindowSeconds).toBe(0);
+ finish(requests.slice(3));
+ await vi.waitFor(() => expect(hook.postures()).toHaveLength(401));
+ expect(hook.response.error).toBeNull();
+ });
+
+ it('cancels a replaced refresh and an emptied scope', async () => {
+ const requests = holdBatches();
+ const [ids, setIDs] = createSignal<readonly string[]>(fleet);
+ const hook = mount(ids);
+ await vi.waitFor(() => expect(requests).toHaveLength(3));
+ const retry = hook.refetch();
+ expectCancelled(requests.slice(0, 3));
+ expect(requests).toHaveLength(6);
+ setIDs([]);
+ await vi.waitFor(() => expect(hook.response.loading).toBe(false));
+ expectCancelled(requests.slice(3));
+ finish(requests);
+ await retry;
+ expect(hook.postures()).toEqual([]);
+ expect(hook.response.error).toBeNull();
+ expect(hook.resolvedOnce()).toBe(false);
+ });
+
+ it('cancels sibling batches on failure and keeps the original error until a full retry', async () => {
+ const requests = holdBatches();
+ const hook = mount();
+ await vi.waitFor(() => expect(requests).toHaveLength(3));
+ const failure = new Error('One batch is unavailable');
+ requests[1].reject(failure);
+ await vi.waitFor(() => expect(hook.response.error).toBe(failure));
+ expectCancelled(requests);
+ expect(hook.postures()).toEqual([]);
+ const retry = hook.refetch();
+ expect(requests).toHaveLength(6);
+ expect(hook.response.error).toBe(failure);
+ finish(requests.slice(3));
+ await retry;
+ expect(hook.postures()).toHaveLength(401);
+ expect(hook.response.error).toBeNull();
+ expect(requests.slice(3).every((batch) => batch.signal?.aborted === false)).toBe(true);
+ });
+
+ it('lets the poll replace a pending foreground read without leaving its batches alive', async () => {
+ vi.useFakeTimers();
+ const requests = holdBatches();
+ const hook = mount();
+ await vi.advanceTimersByTimeAsync(0);
+ expect(requests).toHaveLength(3);
+ await vi.advanceTimersByTimeAsync(30_000);
+ expectCancelled(requests.slice(0, 3));
+ expect(requests).toHaveLength(6);
+ finish(requests.slice(3));
+ await vi.advanceTimersByTimeAsync(0);
+ expect(hook.postures()).toHaveLength(401);
+ expect(hook.response.loading).toBe(false);
+ });
+});
diff --git a/frontend-modern/src/hooks/__tests__/useProtectionPostures.test.ts b/frontend-modern/src/hooks/__tests__/useProtectionPostures.test.ts
index 3970ccf112..b5d1e57da9 100644
--- a/frontend-modern/src/hooks/__tests__/useProtectionPostures.test.ts
+++ b/frontend-modern/src/hooks/__tests__/useProtectionPostures.test.ts
@@ -77,6 +77,12 @@ describe('useProtectionPostures transport', () => {
([url]) => new URL(url, 'https://pulse.invalid').searchParams.getAll('resourceId').length,
);
expect(batchSizes).toEqual([MAX_PROTECTION_POSTURE_BATCH_SIZE, 1]);
+ // Fan-out stays inside the query owner's cancellable read; successful
+ // batches share a live signal rather than inventing separate lifetimes.
+ const signals = apiFetchJSONMock.mock.calls.map(([, options]) => options?.signal);
+ expect(signals[0]).toBeInstanceOf(AbortSignal);
+ expect(signals[0].aborted).toBe(false);
+ expect(signals[1]).toBe(signals[0]);
// Each batch keeps the order the backend sent; the batches join in request order.
expect(hook.postures().map((posture) => posture.subjectResourceId)).toEqual([
...resourceIDs.slice(0, MAX_PROTECTION_POSTURE_BATCH_SIZE).reverse(),
diff --git a/frontend-modern/src/hooks/useProtectionPostures.ts b/frontend-modern/src/hooks/useProtectionPostures.ts
index 413d2e3cff..4abae6a8f6 100644
--- a/frontend-modern/src/hooks/useProtectionPostures.ts
+++ b/frontend-modern/src/hooks/useProtectionPostures.ts
@@ -48,8 +48,8 @@ export function buildProtectionPostureBatchURL(resourceIDs: readonly string[]):
return `${PROTECTION_POSTURES_URL}?${params.toString()}`;
}
-async function fetchProtectionPostures(resourceIDs: readonly string[]) {
- return apiFetchJSON<unknown>(buildProtectionPostureBatchURL(resourceIDs));
+async function fetchProtectionPostures(resourceIDs: readonly string[], signal: AbortSignal) {
+ return apiFetchJSON<unknown>(buildProtectionPostureBatchURL(resourceIDs), { signal });
}
/**
@@ -58,13 +58,31 @@ async function fetchProtectionPostures(resourceIDs: readonly string[]) {
*/
async function fetchAllProtectionPostures(
resourceIDs: readonly string[],
+ signal: AbortSignal,
): Promise<ProtectionPosturesResponse> {
+ signal.throwIfAborted();
const batches: string[][] = [];
for (let offset = 0; offset < resourceIDs.length; offset += MAX_PROTECTION_POSTURE_BATCH_SIZE) {
batches.push(resourceIDs.slice(offset, offset + MAX_PROTECTION_POSTURE_BATCH_SIZE));
}
- const responses = await Promise.all(batches.map(fetchProtectionPostures));
- return parseProtectionPostureBatches(responses);
+ // Every batch belongs to this one query read. Scope changes, replacement
+ // reads and disposal cancel the transport too, not just its late result.
+ // If one batch fails, the joined assessment is unusable: stop its siblings
+ // rather than leaving them running after the query has already settled.
+ const controller = new AbortController();
+ const cancel = () => controller.abort(signal.reason);
+ signal.addEventListener('abort', cancel, { once: true });
+ try {
+ const responses = await Promise.all(
+ batches.map((batch) => fetchProtectionPostures(batch, controller.signal)),
+ );
+ return parseProtectionPostureBatches(responses);
+ } catch (error) {
+ controller.abort();
+ throw error;
+ } finally {
+ signal.removeEventListener('abort', cancel);
+ }
}
export function useProtectionPostures(resourceIDs: Accessor<readonly string[] | null | undefined>) {
@@ -76,7 +94,7 @@ export function useProtectionPostures(resourceIDs: Accessor<readonly string[] |
const state = createNonSuspendingQuery<ProtectionPosturesResponse, string>({
source,
cacheKey: (key) => `protection-postures:${key}`,
- fetcher: (key) => fetchAllProtectionPostures(JSON.parse(key) as string[]),
+ fetcher: (key, signal) => fetchAllProtectionPostures(JSON.parse(key) as string[], signal),
initialValue: EMPTY_RESPONSE,
pollMs: REFRESH_MS,
});