Add AbortSignal support to API functions for request cancellation

Add optional signal?: AbortSignal parameter to all API GET functions so they can be
cancelled when components unmount. This prevents state-update warnings and wasted
resources.

Changes:
- frontend/src/api/history.ts: fetchHistory, fetchIpHistory
- frontend/src/api/map.ts: fetchBansByCountry
- frontend/src/api/jails.ts: fetchJails, fetchActiveBans
- frontend/src/api/config.ts: fetchJailConfig, fetchInactiveJails, fetchJailConfigFiles,
  fetchFilterFiles (threads signal through fetchFilters), fetchFilterFile, fetchActionFiles,
  fetchActionFile
- frontend/src/api/blocklist.ts: fetchImportLog, previewBlocklist

Updated all calling hooks to pass the abort signal from their controllers:
- useHistory, useIpHistory
- useMapData
- useActiveBans
- useJails
- useConfigActiveStatus (fetchJails and fetchJailConfigs)
- useJailAdmin (fetchInactiveJails)
- useJailConfigDetail (fetchJailConfig)
- useImportLog (fetchImportLog)
- useBlocklists (previewBlocklist with AbortController)

Updated Docs/Web-Development.md to document the convention.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-04-23 08:33:28 +02:00
parent 1c5b2d36d9
commit 5a6cb640d8
16 changed files with 54 additions and 59 deletions

View File

@@ -1,23 +1,3 @@
### TASK-BUG-09 — `linesCount` Input in `ServerHealthSection` Fires Fetch on Every Keystroke
**Where found**
`frontend/src/components/config/ServerHealthSection.tsx` line 410: `onChange={(_e, d) => { setLinesCount(Number(d.value)); }}`. The `linesCount` value is passed directly to `useServerHealth(linesCount, filterValue)`. `useServerHealth` re-creates its `refresh` callback when `linesCount` changes, which rebuilds `fetchData`, which triggers `useEffect([fetchData])`, firing a `GET /api/config/server/log` for every digit typed. Unlike `filterValue` (which is debounced at 500ms), `linesCount` has no debounce.
**Goal**
Introduce a `debouncedLinesCount` state mirroring the existing `filterValue` / `filterRaw` pattern already in the component. Update the `onChange` handler to set a raw state, and apply a debounce (300500ms) before committing to `linesCount` passed to `useServerHealth`.
**Possible traps and issues**
- The debounce ref pattern (`filterDebounceRef`) is already present in the component; the linesCount debounce should reuse the same approach to avoid introducing a second debounce timer ref.
- `Number(d.value)` on an empty field produces `0`. Guard against `0` or negative values before passing to the API, or the backend may reject them.
**Docs changes needed**
None required.
**Why this is needed**
Typing `"500"` in the Lines field currently fires three HTTP requests (`"5"`, `"50"`, `"500"`). Each request fetches potentially hundreds of log lines and serializes them, adding unnecessary backend load.
---
### TASK-ABORT-01 — Missing `signal` Parameter on Multiple API Functions ### TASK-ABORT-01 — Missing `signal` Parameter on Multiple API Functions
**Where found** **Where found**

View File

@@ -76,14 +76,16 @@ import type { Ban } from "../types/ban";
- Use a **central API client** (e.g., a thin wrapper around `fetch` or `axios`) that returns typed data — individual components never call `fetch` directly. - Use a **central API client** (e.g., a thin wrapper around `fetch` or `axios`) that returns typed data — individual components never call `fetch` directly.
- Validate or assert the response structure at the boundary when dealing with untrusted data; for critical flows, consider a runtime validation library (e.g., `zod`). - Validate or assert the response structure at the boundary when dealing with untrusted data; for critical flows, consider a runtime validation library (e.g., `zod`).
- API endpoint paths are **constants** defined in a single file (`api/endpoints.ts`) — never hard-code URLs in components. - API endpoint paths are **constants** defined in a single file (`api/endpoints.ts`) — never hard-code URLs in components.
- **All API functions that perform a `GET` request must accept an optional `signal?: AbortSignal` parameter and forward it to the HTTP client.** This enables hooks to cancel in-flight requests when components unmount, preventing silent state-update errors and wasted resources. When an API function calls another internal API function, thread the signal through to the underlying call.
```ts ```ts
// api/client.ts // api/client.ts
const BASE_URL = import.meta.env.VITE_API_URL ?? "/api"; const BASE_URL = import.meta.env.VITE_API_URL ?? "/api";
async function get<T>(path: string): Promise<T> { async function get<T>(path: string, signal?: AbortSignal): Promise<T> {
const response: Response = await fetch(`${BASE_URL}${path}`, { const response: Response = await fetch(`${BASE_URL}${path}`, {
credentials: "include", credentials: "include",
signal,
}); });
if (!response.ok) { if (!response.ok) {
throw new ApiError(response.status, await response.text()); throw new ApiError(response.status, await response.text());
@@ -99,11 +101,19 @@ export const api = { get, post, put, del } as const;
import type { BanListResponse } from "../types/ban"; import type { BanListResponse } from "../types/ban";
import { api } from "./client"; import { api } from "./client";
export async function fetchBans(hours: number): Promise<BanListResponse> { export async function fetchBans(hours: number, signal?: AbortSignal): Promise<BanListResponse> {
return api.get<BanListResponse>(`/bans?hours=${hours}`); return api.get<BanListResponse>(`/bans?hours=${hours}`, signal);
} }
``` ```
```ts
// hooks/useBans.ts
const ctrl = new AbortController();
fetchBans(24, ctrl.signal) // Pass the signal to enable cancellation on unmount
.then(resp => { /* ... */ })
.catch(err => { /* ... */ });
```
--- ---
## 4. Code Organization ## 4. Code Organization

View File

@@ -50,8 +50,8 @@ export async function deleteBlocklist(id: number): Promise<void> {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
/** Preview the contents of a blocklist source URL. */ /** Preview the contents of a blocklist source URL. */
export async function previewBlocklist(id: number): Promise<PreviewResponse> { export async function previewBlocklist(id: number, signal?: AbortSignal): Promise<PreviewResponse> {
return get<PreviewResponse>(ENDPOINTS.blocklistPreview(id)); return get<PreviewResponse>(ENDPOINTS.blocklistPreview(id), signal);
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -86,6 +86,7 @@ export async function fetchImportLog(
page = 1, page = 1,
pageSize = 50, pageSize = 50,
sourceId?: number, sourceId?: number,
signal?: AbortSignal,
): Promise<ImportLogListResponse> { ): Promise<ImportLogListResponse> {
const params = new URLSearchParams(); const params = new URLSearchParams();
params.set("page", String(page)); params.set("page", String(page));
@@ -93,5 +94,6 @@ export async function fetchImportLog(
if (sourceId !== undefined) params.set("source_id", String(sourceId)); if (sourceId !== undefined) params.set("source_id", String(sourceId));
return get<ImportLogListResponse>( return get<ImportLogListResponse>(
`${ENDPOINTS.blocklistsLog}?${params.toString()}`, `${ENDPOINTS.blocklistsLog}?${params.toString()}`,
signal,
); );
} }

View File

@@ -57,9 +57,10 @@ signal?: AbortSignal,
} }
export async function fetchJailConfig( export async function fetchJailConfig(
name: string name: string,
signal?: AbortSignal,
): Promise<JailConfigResponse> { ): Promise<JailConfigResponse> {
return get<JailConfigResponse>(ENDPOINTS.configJail(name)); return get<JailConfigResponse>(ENDPOINTS.configJail(name), signal);
} }
export async function updateJailConfig( export async function updateJailConfig(
@@ -161,8 +162,8 @@ export async function updateMapColorThresholds(
// Jail config files (Task 4a) // Jail config files (Task 4a)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export async function fetchJailConfigFiles(): Promise<JailConfigFilesResponse> { export async function fetchJailConfigFiles(signal?: AbortSignal): Promise<JailConfigFilesResponse> {
return get<JailConfigFilesResponse>(ENDPOINTS.configJailFiles); return get<JailConfigFilesResponse>(ENDPOINTS.configJailFiles, signal);
} }
export async function createJailConfigFile( export async function createJailConfigFile(
@@ -202,8 +203,8 @@ export async function setJailConfigFileEnabled(
* returns active-status data) and maps the result down to the simpler * returns active-status data) and maps the result down to the simpler
* ``ConfFilesResponse`` shape expected by the raw-file editor and export tab. * ``ConfFilesResponse`` shape expected by the raw-file editor and export tab.
*/ */
export async function fetchFilterFiles(): Promise<ConfFilesResponse> { export async function fetchFilterFiles(signal?: AbortSignal): Promise<ConfFilesResponse> {
const result = await fetchFilters(); const result = await fetchFilters(signal);
return { return {
files: result.filters.map((f) => ({ name: f.name, filename: f.filename })), files: result.filters.map((f) => ({ name: f.name, filename: f.filename })),
total: result.total, total: result.total,
@@ -211,8 +212,8 @@ export async function fetchFilterFiles(): Promise<ConfFilesResponse> {
} }
/** Fetch the raw content of a filter definition file for the raw editor. */ /** Fetch the raw content of a filter definition file for the raw editor. */
export async function fetchFilterFile(name: string): Promise<ConfFileContent> { export async function fetchFilterFile(name: string, signal?: AbortSignal): Promise<ConfFileContent> {
return get<ConfFileContent>(ENDPOINTS.configFilterRaw(name)); return get<ConfFileContent>(ENDPOINTS.configFilterRaw(name), signal);
} }
/** Save raw content to a filter definition file (``PUT /filters/{name}/raw``). */ /** Save raw content to a filter definition file (``PUT /filters/{name}/raw``). */
@@ -234,12 +235,12 @@ export async function createFilterFile(
// Action files (Task 4e) // Action files (Task 4e)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export async function fetchActionFiles(): Promise<ConfFilesResponse> { export async function fetchActionFiles(signal?: AbortSignal): Promise<ConfFilesResponse> {
return get<ConfFilesResponse>(ENDPOINTS.configActions); return get<ConfFilesResponse>(ENDPOINTS.configActions, signal);
} }
export async function fetchActionFile(name: string): Promise<ConfFileContent> { export async function fetchActionFile(name: string, signal?: AbortSignal): Promise<ConfFileContent> {
return get<ConfFileContent>(ENDPOINTS.configActionRaw(name)); return get<ConfFileContent>(ENDPOINTS.configActionRaw(name), signal);
} }
export async function updateActionFile( export async function updateActionFile(
@@ -495,9 +496,8 @@ export async function updateParsedJailFile(
// Inactive jails (Stage 1) // Inactive jails (Stage 1)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
/** Fetch all inactive jails from config files. */ export async function fetchInactiveJails(signal?: AbortSignal): Promise<InactiveJailListResponse> {
export async function fetchInactiveJails(): Promise<InactiveJailListResponse> { return get<InactiveJailListResponse>(ENDPOINTS.configJailsInactive, signal);
return get<InactiveJailListResponse>(ENDPOINTS.configJailsInactive);
} }
/** /**

View File

@@ -15,6 +15,7 @@ import type {
*/ */
export async function fetchHistory( export async function fetchHistory(
query: HistoryQuery = {}, query: HistoryQuery = {},
signal?: AbortSignal,
): Promise<HistoryListResponse> { ): Promise<HistoryListResponse> {
const params = new URLSearchParams(); const params = new URLSearchParams();
if (query.range) params.set("range", query.range); if (query.range) params.set("range", query.range);
@@ -30,7 +31,7 @@ export async function fetchHistory(
const url = qs const url = qs
? `${ENDPOINTS.history}?${qs}` ? `${ENDPOINTS.history}?${qs}`
: ENDPOINTS.history; : ENDPOINTS.history;
return get<HistoryListResponse>(url); return get<HistoryListResponse>(url, signal);
} }
/** /**
@@ -38,9 +39,9 @@ export async function fetchHistory(
* *
* @returns null when the server returns 404 (no history for this IP). * @returns null when the server returns 404 (no history for this IP).
*/ */
export async function fetchIpHistory(ip: string): Promise<IpDetailResponse | null> { export async function fetchIpHistory(ip: string, signal?: AbortSignal): Promise<IpDetailResponse | null> {
try { try {
return await get<IpDetailResponse>(ENDPOINTS.historyIp(ip)); return await get<IpDetailResponse>(ENDPOINTS.historyIp(ip), signal);
} catch (err: unknown) { } catch (err: unknown) {
if ( if (
typeof err === "object" && typeof err === "object" &&

View File

@@ -27,8 +27,8 @@ import type {
* @returns A {@link JailListResponse} containing summary info for each jail. * @returns A {@link JailListResponse} containing summary info for each jail.
* @throws {ApiError} On non-2xx responses. * @throws {ApiError} On non-2xx responses.
*/ */
export async function fetchJails(): Promise<JailListResponse> { export async function fetchJails(signal?: AbortSignal): Promise<JailListResponse> {
return get<JailListResponse>(ENDPOINTS.jails); return get<JailListResponse>(ENDPOINTS.jails, signal);
} }
/** /**
@@ -213,8 +213,8 @@ export async function unbanIp(
* @returns An {@link ActiveBanListResponse} with geo-enriched entries. * @returns An {@link ActiveBanListResponse} with geo-enriched entries.
* @throws {ApiError} On non-2xx responses. * @throws {ApiError} On non-2xx responses.
*/ */
export async function fetchActiveBans(): Promise<ActiveBanListResponse> { export async function fetchActiveBans(signal?: AbortSignal): Promise<ActiveBanListResponse> {
return get<ActiveBanListResponse>(ENDPOINTS.bansActive); return get<ActiveBanListResponse>(ENDPOINTS.bansActive, signal);
} }
/** /**

View File

@@ -19,6 +19,7 @@ export async function fetchBansByCountry(
origin: BanOriginFilter = "all", origin: BanOriginFilter = "all",
source: "fail2ban" | "archive" = "fail2ban", source: "fail2ban" | "archive" = "fail2ban",
countryCode?: string, countryCode?: string,
signal?: AbortSignal,
): Promise<BansByCountryResponse> { ): Promise<BansByCountryResponse> {
const params = new URLSearchParams({ range }); const params = new URLSearchParams({ range });
if (origin !== "all") { if (origin !== "all") {
@@ -30,5 +31,5 @@ export async function fetchBansByCountry(
if (countryCode) { if (countryCode) {
params.set("country_code", countryCode); params.set("country_code", countryCode);
} }
return get<BansByCountryResponse>(`${ENDPOINTS.dashboardBansByCountry}?${params.toString()}`); return get<BansByCountryResponse>(`${ENDPOINTS.dashboardBansByCountry}?${params.toString()}`, signal);
} }

View File

@@ -35,7 +35,7 @@ export function useActiveBans(): UseActiveBansResult {
setLoading(true); setLoading(true);
setError(null); setError(null);
fetchActiveBans() fetchActiveBans(ctrl.signal)
.then((res) => { .then((res) => {
if (!ctrl.signal.aborted) { if (!ctrl.signal.aborted) {
setBans(res.bans); setBans(res.bans);

View File

@@ -74,7 +74,8 @@ export function useBlocklists(): UseBlocklistsReturn {
); );
const previewSource = useCallback(async (id: number): Promise<PreviewResponse> => { const previewSource = useCallback(async (id: number): Promise<PreviewResponse> => {
return previewBlocklist(id); const ctrl = new AbortController();
return previewBlocklist(id, ctrl.signal);
}, []); }, []);
return { return {

View File

@@ -66,8 +66,8 @@ export function useConfigActiveStatus(): UseConfigActiveStatusResult {
setError(null); setError(null);
Promise.all([ Promise.all([
fetchJails(), fetchJails(ctrl.signal),
fetchJailConfigs(), fetchJailConfigs(ctrl.signal),
]) ])
.then(([jailsResp, configsResp]) => { .then(([jailsResp, configsResp]) => {
if (ctrl.signal.aborted) return; if (ctrl.signal.aborted) return;

View File

@@ -39,7 +39,7 @@ export function useHistory(query: HistoryQuery = {}): UseHistoryResult {
setLoading(true); setLoading(true);
setError(null); setError(null);
fetchHistory({ ...query, page }) fetchHistory({ ...query, page }, abortRef.current.signal)
.then((resp) => { .then((resp) => {
setItems(resp.items); setItems(resp.items);
setTotal(resp.total); setTotal(resp.total);
@@ -85,7 +85,7 @@ export function useIpHistory(ip: string): UseIpHistoryResult {
setLoading(true); setLoading(true);
setError(null); setError(null);
fetchIpHistory(ip) fetchIpHistory(ip, abortRef.current.signal)
.then((resp) => { .then((resp) => {
setDetail(resp); setDetail(resp);
}) })

View File

@@ -37,7 +37,7 @@ export function useImportLog(
setLoading(true); setLoading(true);
setError(null); setError(null);
fetchImportLog(page, pageSize, sourceId) fetchImportLog(page, pageSize, sourceId, ctrl.signal)
.then((result) => { .then((result) => {
if (!ctrl.signal.aborted) { if (!ctrl.signal.aborted) {
setData(result); setData(result);

View File

@@ -49,7 +49,7 @@ export function useJailAdmin(): UseJailAdminResult {
setInactiveLoading(true); setInactiveLoading(true);
setInactiveError(null); setInactiveError(null);
fetchInactiveJails() fetchInactiveJails(ctrl.signal)
.then((resp) => { .then((resp) => {
if (!ctrl.signal.aborted) { if (!ctrl.signal.aborted) {
setInactiveJails(resp.jails); setInactiveJails(resp.jails);

View File

@@ -32,7 +32,7 @@ export function useJailConfigDetail(name: string): UseJailConfigDetailResult {
setLoading(true); setLoading(true);
setError(null); setError(null);
fetchJailConfig(name) fetchJailConfig(name, ctrl.signal)
.then((resp) => { .then((resp) => {
if (!ctrl.signal.aborted) { if (!ctrl.signal.aborted) {
setJail(resp.jail); setJail(resp.jail);

View File

@@ -44,7 +44,7 @@ export function useJails(): UseJailsResult {
setLoading(true); setLoading(true);
setError(null); setError(null);
fetchJails() fetchJails(ctrl.signal)
.then((res) => { .then((res) => {
if (!ctrl.signal.aborted) { if (!ctrl.signal.aborted) {
setJails(res.jails); setJails(res.jails);

View File

@@ -66,7 +66,7 @@ export function useMapData(
abortRef.current?.abort(); abortRef.current?.abort();
abortRef.current = new AbortController(); abortRef.current = new AbortController();
fetchBansByCountry(range, origin, source, countryCode) fetchBansByCountry(range, origin, source, countryCode, abortRef.current.signal)
.then((resp) => { .then((resp) => {
setData(resp); setData(resp);
}) })