mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-03 01:23:31 +01:00
A user's backtracking term made each anonymize-form preview block the API's event loop for ~1.2 s and fail with a 500. One per keystroke, 45 of them froze every request for about a minute (12:59 UTC, 2026-11-30). - Previews run on their own worker pool (1 worker by default). A preview that runs past its deadline (3 s) terminates its worker and answers 422 anonymization_timeout; the form explains the term is too slow. - One preview per user: a newer edit, or the client leaving, stops the older preview (409 preview_superseded, logged at info). - Slow (>=500 ms) or failed previews log an anonymization_preview record with the project's repoId, input bytes and term count. - Heartbeats carry the interval's worst event-loop delay (loopMaxMs, loopP99Ms); a delay of 1 s or more logs event_loop_blocked at warn. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
54 lines
2.4 KiB
JavaScript
54 lines
2.4 KiB
JavaScript
const { expect } = require("chai");
|
|
require("ts-node/register/transpile-only");
|
|
const { monitorEventLoopDelay } = require("node:perf_hooks");
|
|
const { setTimeout } = require("node:timers");
|
|
const { anonymizePreview } = require("../src/core/preview-anonymizer");
|
|
|
|
const SLOW_TERM = "a+a+a+a+a+a+b(?=x)";
|
|
|
|
describe("anonymize-form preview off the main thread", function () {
|
|
this.timeout(20000);
|
|
|
|
it("anonymizes on a worker", async () => {
|
|
const [out] = await anonymizePreview("user-ok", ["Alice wrote this"], { terms: ["Alice"] });
|
|
expect(out).to.match(/^\S+ wrote this$/);
|
|
expect(out).to.not.include("Alice");
|
|
});
|
|
|
|
it("answers a backtracking term with a coded timeout and keeps the event loop free", async () => {
|
|
const loop = monitorEventLoopDelay({ resolution: 10 });
|
|
loop.enable();
|
|
let failure;
|
|
try { await anonymizePreview("user-slow", ["a".repeat(250)], { terms: [SLOW_TERM] }); }
|
|
catch (error) { failure = error; }
|
|
loop.disable();
|
|
expect(failure && failure.message).to.equal("anonymization_timeout");
|
|
expect(failure.httpStatus).to.equal(422);
|
|
// The regex ran for about a second on the worker; the main loop stayed responsive.
|
|
expect(loop.max / 1e6).to.be.lessThan(300);
|
|
// The pool recovers after the stalled worker is replaced.
|
|
const [out] = await anonymizePreview("user-slow", ["Bob"], { terms: ["Bob"] });
|
|
expect(out).to.not.include("Bob");
|
|
});
|
|
|
|
it("keeps one preview per user: a newer edit supersedes the older one", async () => {
|
|
const older = anonymizePreview("user-typing", ["a".repeat(250)], { terms: [SLOW_TERM] }).then(() => null, error => error);
|
|
const newer = anonymizePreview("user-typing", ["Carol"], { terms: ["Carol"] });
|
|
const [olderResult, [newerOut]] = await Promise.all([older, newer]);
|
|
expect(olderResult && olderResult.message).to.equal("preview_superseded");
|
|
expect(olderResult.httpStatus).to.equal(409);
|
|
expect(newerOut).to.not.include("Carol");
|
|
});
|
|
|
|
it("stops a preview whose client went away", async () => {
|
|
const controller = new AbortController();
|
|
const pending = anonymizePreview("user-left", ["a".repeat(250)], { terms: [SLOW_TERM] }, controller.signal)
|
|
.then(() => null, error => error);
|
|
setTimeout(() => controller.abort(), 50);
|
|
const started = Date.now();
|
|
const error = await pending;
|
|
expect(error && error.message).to.equal("preview_superseded");
|
|
expect(Date.now() - started).to.be.lessThan(800);
|
|
});
|
|
});
|