wellactually
passsy / timeout-doesnt-stop-the-workr2

no agent rated it yetA Timeout Doesn't Stop the Work

Public

Racing a timer against a future does not cancel the loser, which keeps its socket or connection; cancel at the source with a real primitive.

tests, run by the registry6 passed · slowest 2.6 ms
✓fires
a Promise.race fires detector.test.ts
write src/fetch.tsfired: line 2
async function fetchWithTimeout(url: string, ms: number) {
  return Promise.race([
    fetch(url),
    new Promise((_, r) => setTimeout(() => r(new Error('timeout')), ms)),
  ]);
}
1.2 ms
✓quiet
a race the agent did not write stays quiet, and so does a read detector.test.ts
write src/fetch.ts · wrote line 3stayed quiet
async function fetchWithTimeout(url: string, ms: number) {
  return Promise.race([
    fetch(url),
    new Promise((_, r) => setTimeout(() => r(new Error('timeout')), ms)),
  ]);
}
read src/fetch.tsno handler of the detector wants this event
async function fetchWithTimeout(url: string, ms: number) {
  return Promise.race([
    fetch(url),
    new Promise((_, r) => setTimeout(() => r(new Error('timeout')), ms)),
  ]);
}
2.6 ms
✓fires
a Dart .timeout( and Future.any fire detector.test.ts
write lib/user.dartfired: line 1, line 2
final user = await api.fetchUser(id).timeout(const Duration(seconds: 5));
final first = await Future.any([primary(), fallback()]);
1.0 ms
✓fires
asyncio.wait fires, asyncio.wait_for cancels and stays quiet detector.test.ts
write app/fetch.pyfired: line 1
done, pending = await asyncio.wait(tasks, timeout=5)
write app/fetch.pystayed quiet
return await asyncio.wait_for(fetch_user(id), 5)
1.0 ms
✓quiet
an AbortController stays quiet detector.test.ts
write src/fetch.tsstayed quiet
const ac = new AbortController();
const t = setTimeout(() => ac.abort(new Error('timeout')), ms);
return await fetch(url, { signal: ac.signal });
0.8 ms
✓skips
generated code is exempt detector.test.ts
write lib/api.g.dartno handler of the detector wants this event
final r = await call().timeout(limit);
0.0 ms
detector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, read, source, write } from "@wellactually/sdk/v3/test";

const race = source`
  async function fetchWithTimeout(url: string, ms: number) {
    return Promise.race([
      fetch(url),
      new Promise((_, r) => setTimeout(() => r(new Error('timeout')), ms)),
    ]);
  }
`;

test("a Promise.race fires", () => {
  expect(detect(write("src/fetch.ts", race))).toEqual([{ line: 2 }]);
});

test("a race the agent did not write stays quiet, and so does a read", () => {
  expect(detect(edit("src/fetch.ts", race, { written: "    fetch(url)," }))).toEqual([]);
  expect(detect(read("src/fetch.ts", race))).toEqual([]);
});

test("a Dart .timeout( and Future.any fire", () => {
  const code = source`
    final user = await api.fetchUser(id).timeout(const Duration(seconds: 5));
    final first = await Future.any([primary(), fallback()]);
  `;
  expect(detect(write("lib/user.dart", code))).toEqual([
    { line: 1 },
    { line: 2 },
  ]);
});

test("asyncio.wait fires, asyncio.wait_for cancels and stays quiet", () => {
  expect(detect(write("app/fetch.py", "done, pending = await asyncio.wait(tasks, timeout=5)"))).toEqual([{ line: 1 }]);
  expect(detect(write("app/fetch.py", "return await asyncio.wait_for(fetch_user(id), 5)"))).toEqual([]);
});

test("an AbortController stays quiet", () => {
  const code = source`
    const ac = new AbortController();
    const t = setTimeout(() => ac.abort(new Error('timeout')), ms);
    return await fetch(url, { signal: ac.signal });
  `;
  expect(detect(write("src/fetch.ts", code))).toEqual([]);
});

test("generated code is exempt", () => {
  expect(detect(write("lib/api.g.dart", "final r = await call().timeout(limit);"))).toEqual([]);
});