Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.

Commit ef1ffc2

Browse files
authored
fix(console): stop retrying /api/v1/dev/metadata-events after the first 404 (#7266)
* wip: metadata-events give-up-after-first-failure + tests * chore(console): add changeset for metadata-events 404-storm fix Companion to the previous commit — declares the @object-ui/console patch release note for objectui#7257.
1 parent ab92940 commit ef1ffc2

3 files changed

Lines changed: 241 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
'@object-ui/console': patch
3+
---
4+
5+
`MetadataHmrReloader` stops flooding a production-posture deployment with
6+
`GET /api/v1/dev/metadata-events` 404s (objectui#7257).
7+
8+
The dev-only HMR component subscribes via `EventSource`, gated on
9+
`import.meta.env.DEV`. That gate is not airtight against every rig: a
10+
"prod-like" build/serve setup that forces `NODE_ENV=development` for the
11+
*build tooling* while running the server itself in production posture can
12+
bake `DEV === true` into the shipped bundle even though the server never
13+
mounts the dev route there — and the old reconnect loop treated every closed
14+
connection as transient, retrying on a fixed `reconnectDelayMs` (2s)
15+
forever. On an env host that is ~30 requests/minute of 404s per open
16+
record/list page, drowning out the legitimate `sys_inbox_message` /
17+
`sys_notification_receipt` polling in the same console.
18+
19+
The first `connect()` attempt now doubles as the real capability probe: if
20+
the stream closes before it ever reaches `open`, the component gives up for
21+
good instead of retrying (and specifically not a longer interval either —
22+
that would still spam 404s, just slower). A stream that DID open at least
23+
once and later drops — a real dev-server restart or network blip — keeps
24+
reconnecting exactly as before.
25+
26+
No production-side replacement is introduced here: this component's only
27+
job is turning "a metadata file changed on disk" into a full reload, and
28+
production deployments have no such file-system event to watch. The
29+
separate Studio-left-nav-doesn't-refresh caching issue does not go through
30+
this SSE stream and needs its own fix.
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* objectui#7257 — a production-posture console kept polling
5+
* `GET /api/v1/dev/metadata-events` every `reconnectDelayMs` forever, each
6+
* attempt landing on a 404 because the server doesn't mount this dev-only
7+
* route there (~30 requests/min, flooding the console and drowning out the
8+
* legitimate `sys_inbox_message` / `sys_notification_receipt` polling).
9+
*
10+
* `enabled` (default `import.meta.env.DEV`) is the primary gate, but it is
11+
* not airtight: a build can end up with `DEV === true` baked in even while
12+
* the server it talks to is in production posture (a "prod-like" rig that
13+
* forces `NODE_ENV=development` for the *build tooling* is one concrete way
14+
* this happens — see the file header of `MetadataHmrReloader.tsx`). So the
15+
* fix under test lives one layer down, in the reconnect loop itself: the
16+
* first `connect()` attempt doubles as a capability probe. A stream that
17+
* closes before ever reaching `open` is read as "the server doesn't support
18+
* this", and the component gives up for good — no retry, and specifically
19+
* NOT a longer retry interval (that would still spam 404s, just slower). A
20+
* stream that DID open at least once and later drops is a different case —
21+
* that proves the route exists, so ordinary reconnect-on-drop keeps working
22+
* for real dev-server restarts / network blips.
23+
*
24+
* A fake `EventSource` drives both branches directly (this repo's jsdom-ish
25+
* `happy-dom` test environment does not implement `EventSource`), so each
26+
* case controls exactly which lifecycle events fire and in what order.
27+
*/
28+
29+
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
30+
import { render, cleanup } from '@testing-library/react';
31+
import { MetadataHmrReloader } from './MetadataHmrReloader';
32+
33+
type Listener = (event: unknown) => void;
34+
35+
/** Minimal, test-controlled stand-in for the browser `EventSource`. */
36+
class FakeEventSource {
37+
static readonly CONNECTING = 0;
38+
static readonly OPEN = 1;
39+
static readonly CLOSED = 2;
40+
/** Every instance constructed across the whole test file, in order. */
41+
static instances: FakeEventSource[] = [];
42+
43+
readyState = FakeEventSource.CONNECTING;
44+
private readonly listeners = new Map<string, Listener[]>();
45+
46+
constructor(public readonly url: string) {
47+
FakeEventSource.instances.push(this);
48+
}
49+
50+
addEventListener(type: string, listener: Listener) {
51+
const existing = this.listeners.get(type) ?? [];
52+
existing.push(listener);
53+
this.listeners.set(type, existing);
54+
}
55+
56+
removeEventListener() {
57+
/* not exercised — the component never calls this */
58+
}
59+
60+
close() {
61+
this.readyState = FakeEventSource.CLOSED;
62+
}
63+
64+
private emit(type: string, event: unknown = {}) {
65+
for (const listener of this.listeners.get(type) ?? []) listener(event);
66+
}
67+
68+
/** The 404 case: the connection never reaches `open` before it closes. */
69+
failWithoutOpening() {
70+
this.readyState = FakeEventSource.CLOSED;
71+
this.emit('error');
72+
}
73+
74+
/** The dev-server-restart case: a real connection that later drops. */
75+
openThenDrop() {
76+
this.readyState = FakeEventSource.OPEN;
77+
this.emit('open');
78+
this.readyState = FakeEventSource.CLOSED;
79+
this.emit('error');
80+
}
81+
}
82+
83+
describe('MetadataHmrReloader — give up after the first connection fails without opening', () => {
84+
const originalEventSource = (globalThis as { EventSource?: unknown }).EventSource;
85+
86+
beforeEach(() => {
87+
FakeEventSource.instances = [];
88+
(globalThis as { EventSource?: unknown }).EventSource = FakeEventSource;
89+
});
90+
91+
afterEach(() => {
92+
cleanup();
93+
(globalThis as { EventSource?: unknown }).EventSource = originalEventSource;
94+
vi.useRealTimers();
95+
});
96+
97+
it('THE FIX: a 404-shaped failure (closed before `open`) is not retried at all', () => {
98+
vi.useFakeTimers();
99+
100+
render(
101+
<MetadataHmrReloader
102+
enabled
103+
url="/api/v1/dev/metadata-events"
104+
reconnectDelayMs={2000}
105+
/>,
106+
);
107+
108+
expect(FakeEventSource.instances).toHaveLength(1);
109+
FakeEventSource.instances[0].failWithoutOpening();
110+
111+
// Advance well past what would have been ~15 reconnect attempts under
112+
// the old fixed-interval retry — the regression this pins is "polls
113+
// forever", not "polls a little less".
114+
vi.advanceTimersByTime(2000 * 15);
115+
116+
expect(FakeEventSource.instances).toHaveLength(1);
117+
});
118+
119+
it('⛔ does not fall back to a longer interval either — zero further attempts, not a slower loop', () => {
120+
vi.useFakeTimers();
121+
122+
render(
123+
<MetadataHmrReloader
124+
enabled
125+
url="/api/v1/dev/metadata-events"
126+
reconnectDelayMs={2000}
127+
/>,
128+
);
129+
130+
FakeEventSource.instances[0].failWithoutOpening();
131+
132+
// Even an interval an order of magnitude longer than the configured one
133+
// must not produce a second attempt.
134+
vi.advanceTimersByTime(2000 * 100);
135+
136+
expect(FakeEventSource.instances).toHaveLength(1);
137+
});
138+
139+
it('CONTROL: a stream that opened once and then drops keeps reconnecting (real dev-server churn)', () => {
140+
vi.useFakeTimers();
141+
142+
render(
143+
<MetadataHmrReloader
144+
enabled
145+
url="/api/v1/dev/metadata-events"
146+
reconnectDelayMs={2000}
147+
/>,
148+
);
149+
150+
expect(FakeEventSource.instances).toHaveLength(1);
151+
FakeEventSource.instances[0].openThenDrop();
152+
153+
vi.advanceTimersByTime(2000);
154+
expect(FakeEventSource.instances).toHaveLength(2);
155+
156+
// The reconnect is itself a fresh probe: if it also opens and later
157+
// drops, the loop keeps going — this is the behavior the fix must NOT
158+
// disturb for a route that genuinely exists.
159+
FakeEventSource.instances[1].openThenDrop();
160+
vi.advanceTimersByTime(2000);
161+
expect(FakeEventSource.instances).toHaveLength(3);
162+
});
163+
164+
it('does not subscribe at all when disabled (unaffected by this change)', () => {
165+
render(<MetadataHmrReloader enabled={false} url="/api/v1/dev/metadata-events" />);
166+
167+
expect(FakeEventSource.instances).toHaveLength(0);
168+
});
169+
});

‎apps/console/src/components/MetadataHmrReloader.tsx‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@
1919
* - `enabled` defaults to `import.meta.env.DEV` so production builds
2020
* never run this component.
2121
* - SSR-safe: no-op when `window`/`EventSource` are unavailable.
22+
*
23+
* Give-up-after-first-failure (objectui#7257)
24+
* `enabled` alone is not a hard guarantee that the server actually mounts
25+
* `/api/v1/dev/metadata-events` — a build can end up with
26+
* `import.meta.env.DEV === true` baked in (e.g. a "prod-like" rig that
27+
* forces `NODE_ENV=development` for the *build tooling* while running the
28+
* server itself in production posture) even though the route is absent
29+
* there. Treat the very first `connect()` attempt as the real capability
30+
* probe: if the stream is closed before it ever reaches `open`, the server
31+
* doesn't support it (a 404, most likely), and retrying on a fixed
32+
* interval would just recreate that 404 every `reconnectDelayMs` forever —
33+
* so this gives up permanently instead of retrying or backing off. A
34+
* stream that HAS opened at least once and later drops (dev server
35+
* restart, network blip) keeps reconnecting as before — that scenario
36+
* means the route genuinely exists, so it is ordinary dev-time churn, not
37+
* a production posture mismatch.
38+
*
39+
* No production-posture substitute
40+
* This component's only job is dev-time "a metadata file changed on disk"
41+
* -> full reload. Production deployments never have metadata files
42+
* changing on the server's disk after a publish, so there is nothing for
43+
* an equivalent production channel to watch — this card intentionally
44+
* ships no replacement. A separate, unrelated caching bug (the Studio left
45+
* nav not refreshing after a metadata change) needs its own fix elsewhere;
46+
* it does not go through this SSE stream today either way.
2247
*/
2348

2449
import { useEffect, useRef } from 'react';
@@ -50,6 +75,11 @@ export function MetadataHmrReloader({
5075
let es: EventSource | null = null;
5176
let retryTimer: ReturnType<typeof setTimeout> | null = null;
5277
let cancelled = false;
78+
// Set once the stream has ever reached `open`. Until then, a closed
79+
// connection is read as "this server doesn't mount the route" rather
80+
// than "a transient drop" — see the give-up-after-first-failure note
81+
// in the file header (objectui#7257).
82+
let hasOpenedOnce = false;
5383

5484
const scheduleReload = (reason: string) => {
5585
if (reloadTimerRef.current) clearTimeout(reloadTimerRef.current);
@@ -92,16 +122,28 @@ export function MetadataHmrReloader({
92122
if (cancelled) return;
93123
try {
94124
es = new EventSource(url);
125+
es.addEventListener('open', () => {
126+
hasOpenedOnce = true;
127+
});
95128
es.addEventListener('metadata-change', onChange as EventListener);
96129
es.addEventListener('reload', onReload as EventListener);
97130
es.addEventListener('error', () => {
98131
if (cancelled) return;
99132
if (es?.readyState === EventSource.CLOSED) {
100133
es = null;
134+
if (!hasOpenedOnce) {
135+
// Never got past the first attempt — most likely a 404
136+
// (production posture doesn't mount this dev-only route).
137+
// Retrying on a timer would just recreate the same 404 every
138+
// `reconnectDelayMs` forever; give up instead of extending
139+
// the interval.
140+
return;
141+
}
101142
retryTimer = setTimeout(connect, reconnectDelayMs);
102143
}
103144
});
104145
} catch {
146+
if (!hasOpenedOnce) return;
105147
retryTimer = setTimeout(connect, reconnectDelayMs);
106148
}
107149
};

0 commit comments

Comments
 (0)