Skip to content

Commit 889b562

Browse files
authored
Merge pull request TriDefender#51 from TriDefender/fix/captcha-memory
fix(captcha): 修复 serve 常驻进程 RSS 单调增长(issue TriDefender#50)
2 parents 97124b4 + 7937669 commit 889b562

2 files changed

Lines changed: 126 additions & 9 deletions

File tree

‎src/proxy/captcha-happy.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { describe, expect, test } from "bun:test";
22
import { GlobalWindow } from "happy-dom";
33
import {
4+
__captchaMemStats,
45
createDom,
56
destroyDom,
67
installGlobalWindowAlias,
8+
noteRequest,
9+
reclaimCaptchaHeap,
10+
rememberCdnBody,
711
removeGlobalWindowAlias,
812
} from "./captcha-happy.js";
913

@@ -307,3 +311,52 @@ describe("alias lifecycle (install/remove descriptor contract)", () => {
307311
}
308312
});
309313
});
314+
315+
// Memory guards for long-running serve processes (issue #50: monotonic RSS
316+
// growth, 2.9 days → 9.95GB). Every structure here is module-scoped and shared
317+
// across solves, so without an explicit bound each one grows for the process
318+
// lifetime.
319+
describe("memory guards (issue #50)", () => {
320+
test("guest request log is a ring buffer, not an append-only array", () => {
321+
const cap = __captchaMemStats().requestLogCap;
322+
const beyond = cap + 44;
323+
for (let i = 0; i < beyond; i++) {
324+
noteRequest({ at: Date.now(), method: "GET", url: `https://example.com/${i}` });
325+
}
326+
const stats = __captchaMemStats();
327+
expect(stats.requestLogLength).toBe(cap);
328+
});
329+
330+
test("in-memory CDN cache evicts oldest entries at the cap (pe rotation)", () => {
331+
for (let i = 0; i < 20; i++) {
332+
rememberCdnBody(`https://o.alicdn.com/dynamicJS/v/pe.${1000 + i}.js`, Buffer.from("x"));
333+
}
334+
expect(__captchaMemStats().cdnCacheSize).toBe(__captchaMemStats().cdnCacheCap);
335+
});
336+
337+
test("heap reclaim runs at most once per throttle window", () => {
338+
const saved = process.env.CAPTCHA_GC_MIN_INTERVAL_MS;
339+
try {
340+
const before = __captchaMemStats().gcCalls;
341+
process.env.CAPTCHA_GC_MIN_INTERVAL_MS = "0";
342+
reclaimCaptchaHeap();
343+
reclaimCaptchaHeap();
344+
expect(__captchaMemStats().gcCalls).toBe(before + 2);
345+
346+
process.env.CAPTCHA_GC_MIN_INTERVAL_MS = "3600000";
347+
reclaimCaptchaHeap();
348+
expect(__captchaMemStats().gcCalls).toBe(before + 2);
349+
} finally {
350+
if (saved === undefined) delete process.env.CAPTCHA_GC_MIN_INTERVAL_MS;
351+
else process.env.CAPTCHA_GC_MIN_INTERVAL_MS = saved;
352+
}
353+
});
354+
355+
test("reused-window solve budget defaults to the reduced cap", () => {
356+
// 25 → 8 (issue #50): each solve leaves SDK instance graphs resident in a
357+
// reused window, so the cap bounds a generation's retention peak.
358+
if (process.env.CAPTCHA_REUSE_MAX_SOLVES === undefined) {
359+
expect(__captchaMemStats().reuseMaxSolves).toBe(8);
360+
}
361+
});
362+
});

‎src/proxy/captcha-happy.ts‎

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ function shutdownSyncFetchWorker(): void {
122122

123123
const CDN_CACHE_DIR = path.join(os.homedir(), ".zcode-captcha-cdn-cache");
124124
const _memCdnCache = new Map();
125+
// pe bundles rotate (pe.0xx…); every rotation would otherwise pin a fresh
126+
// multi-hundred-KB body for the process lifetime (issue #50). Insertion-ordered
127+
// FIFO — the oldest rotation ages out first; disk cache still serves re-reads.
128+
const MEM_CDN_CACHE_CAP = 16;
129+
function rememberCdnBody(url, body) {
130+
_memCdnCache.set(url, body);
131+
if (_memCdnCache.size > MEM_CDN_CACHE_CAP) {
132+
for (const key of _memCdnCache.keys()) {
133+
_memCdnCache.delete(key);
134+
if (_memCdnCache.size <= MEM_CDN_CACHE_CAP) break;
135+
}
136+
}
137+
}
125138
let _cookieCache = { cookies: [], ts: 0 };
126139
const COOKIE_CACHE_TTL_MS = 5 * 60 * 1000;
127140
const _DEBUG = /^(1|true|yes)$/i.test(
@@ -136,8 +149,17 @@ if (proxyUrl) {
136149
}
137150

138151
// ── Globals shared across solves ────────────────────────────────────────────
152+
// Ring buffer: the stall detector reads only the newest entry and failure
153+
// diagnostics the last ~12 entries of the CURRENT solve — anything older is
154+
// dead weight. Unbounded it grew for the process lifetime (issue #50).
155+
const REQUEST_LOG_CAP = 256;
139156
const _requestLog = [];
140-
const solveTimes = [];
157+
function noteRequest(entry) {
158+
_requestLog.push(entry);
159+
if (_requestLog.length > REQUEST_LOG_CAP) {
160+
_requestLog.splice(0, _requestLog.length - REQUEST_LOG_CAP);
161+
}
162+
}
141163
// Consecutive-stall tracker per pe bundle URL: the same cached pe version
142164
// can stall every attempt (bad rotated VM variant / stale cache). After two
143165
// stalls on one URL, evict its memory + disk cache entry so the next init
@@ -225,7 +247,7 @@ function getCachedBody(url) {
225247
const p = diskPathFor(url);
226248
if (fs.existsSync(p)) {
227249
const body = fs.readFileSync(p);
228-
_memCdnCache.set(url, body);
250+
rememberCdnBody(url, body);
229251
return body;
230252
}
231253
} catch (_) {}
@@ -237,7 +259,7 @@ async function fetchAndStore(url) {
237259
const res = await fetch(url, { headers: { "user-agent": fp.userAgent } });
238260
const buf = Buffer.from(await res.arrayBuffer());
239261
if (buf.length > 0) {
240-
_memCdnCache.set(url, buf);
262+
rememberCdnBody(url, buf);
241263
try {
242264
const p = diskPathFor(url);
243265
fs.mkdirSync(CDN_CACHE_DIR, { recursive: true });
@@ -339,7 +361,7 @@ function makeInterceptor(bypassPeCache = false) {
339361
return {
340362
async beforeAsyncRequest({ request, window: w }) {
341363
const url = request.url;
342-
_requestLog.push({ at: Date.now(), method: request.method, url });
364+
noteRequest({ at: Date.now(), method: request.method, url });
343365
injectRequestHeaders(request);
344366
if (/\balicdn\.com/i.test(url)) {
345367
let body = skipPeCache(url) ? null : getCachedBody(url);
@@ -414,7 +436,7 @@ function makeInterceptor(bypassPeCache = false) {
414436
},
415437
beforeSyncRequest({ request, window: w }) {
416438
const url = request.url;
417-
_requestLog.push({ at: Date.now(), method: request.method, url, sync: true });
439+
noteRequest({ at: Date.now(), method: request.method, url, sync: true });
418440
injectRequestHeaders(request);
419441
let body = null;
420442
if (/\balicdn\.com/i.test(url)) {
@@ -2174,6 +2196,31 @@ export function removeGlobalWindowAlias(g, w) {
21742196
} catch (_) {}
21752197
}
21762198

2199+
// ── Heap reclaim at window-generation turnover ─────────────────────────────
2200+
// A destroyed window leaves a large dead object graph behind (SDK instances,
2201+
// pe VM, intervals, XHR buffers). JSC only hands pages back to the OS on a
2202+
// FULL synchronous collection, so without this the serve process ratchets:
2203+
// every window generation's allocation peak becomes the permanent RSS floor
2204+
// (issue #50: 2.9 days → 9.95GB resident). Throttled because pe-storm retry
2205+
// ladders destroy several windows back-to-back and Bun.gc(true) is a
2206+
// stop-the-world pass that gets heavier at large heaps.
2207+
const _gcStats = { calls: 0, lastAt: 0 };
2208+
function reclaimCaptchaHeap() {
2209+
try {
2210+
const gc =
2211+
(typeof Bun !== "undefined" && typeof Bun.gc === "function" && Bun.gc) ||
2212+
(typeof globalThis.gc === "function" && globalThis.gc); // Node --expose-gc (tests)
2213+
if (!gc) return; // plain Node runtime (Android bundle): no exposed gc, skip
2214+
const raw = Number(process.env.CAPTCHA_GC_MIN_INTERVAL_MS);
2215+
const minMs = Number.isFinite(raw) && raw >= 0 ? raw : 5_000;
2216+
const now = Date.now();
2217+
if (now - _gcStats.lastAt < minMs) return;
2218+
_gcStats.lastAt = now;
2219+
_gcStats.calls += 1;
2220+
gc(true);
2221+
} catch (_) {}
2222+
}
2223+
21772224
function destroyDom(win) {
21782225
try {
21792226
const cap = win.document.getElementById("cap");
@@ -2192,6 +2239,7 @@ function destroyDom(win) {
21922239
// host constructor (harmless: the window registry is already cleared).
21932240
removeGuestScope(win);
21942241
try { shutdownSyncFetchWorker(); } catch (_) {}
2242+
reclaimCaptchaHeap();
21952243
}
21962244

21972245
function extractVerifyParam(param) {
@@ -2247,10 +2295,13 @@ function handleCaptchaResult(result) {
22472295
// 815ms per solve) by amortizing the DOM boot + SDK script load. On by
22482296
// default; opt out with CAPTCHA_WINDOW_REUSE=0 (or per-call
22492297
// solveTraceless({reuseWindow:false})). The window
2250-
// is discarded after `maxSolves` (memory growth), after any stall/failure
2251-
// (fresh InitCaptchaV3 rolls a new pe version), or after `maxIdleMs` idle.
2298+
// is discarded after `maxSolves` (each solve leaves SDK instance graphs
2299+
// resident in the window — issue #50 measured tens of MB per solve), after
2300+
// any stall/failure (fresh InitCaptchaV3 rolls a new pe version), or after
2301+
// `maxIdleMs` idle. 8 keeps most of the amortization win while capping a
2302+
// generation's retention peak at ~1/3 of the old 25-solve default.
22522303
const _reusePool = { window: null, browserFrame: null, solves: 0, lastUsedAt: 0 };
2253-
const REUSE_MAX_SOLVES = Number(process.env.CAPTCHA_REUSE_MAX_SOLVES || 25);
2304+
const REUSE_MAX_SOLVES = Number(process.env.CAPTCHA_REUSE_MAX_SOLVES || 8);
22542305
const REUSE_MAX_IDLE_MS = Number(process.env.CAPTCHA_REUSE_MAX_IDLE_MS || 120_000);
22552306

22562307
function takeReusableWindow() {
@@ -2448,4 +2499,17 @@ async function solveTraceless(opts) {
24482499
}
24492500
}
24502501

2451-
export { solveTraceless, createDom, destroyDom };
2502+
/** Test/debug observability into the memory guards (issue #50). */
2503+
export function __captchaMemStats() {
2504+
return {
2505+
requestLogLength: _requestLog.length,
2506+
requestLogCap: REQUEST_LOG_CAP,
2507+
cdnCacheSize: _memCdnCache.size,
2508+
cdnCacheCap: MEM_CDN_CACHE_CAP,
2509+
reuseMaxSolves: REUSE_MAX_SOLVES,
2510+
gcCalls: _gcStats.calls,
2511+
gcLastAt: _gcStats.lastAt,
2512+
};
2513+
}
2514+
2515+
export { solveTraceless, createDom, destroyDom, noteRequest, rememberCdnBody, reclaimCaptchaHeap };

0 commit comments

Comments
 (0)