vitest-plugin: a Durable Object aborted during a dynamic import makes later imports of that module hang

Open
#16,785 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, typescript, vite
Domain
backend, testing

Research direction

Start with the reproduction in test/hang.test.ts and run npx vitest run to confirm the second test hangs after the Durable Object aborts during import("./lazy"). Then trace the module runner's cachedModule handling of concurrentModuleNodePromises, which the issue identifies as the suspected cause. Done means later imports of the same module settle after an abort, while the control test still passes.

Written by the indexing model from the issue text.

Description

bug package:vitest
What versions & operating system are you using?

@cloudflare/vitest-plugin 1.4.11, vitest 4.1.12, wrangler 4.156.0. Seen on macOS and on Linux.

Please provide a link to a minimal reproduction

The whole reproduction is below, in place of a link. It has a control test, and it fails the same way on each run.

wrangler.jsonc

{
  "name": "repro",
  "main": "src/index.ts",
  "compatibility_date": "2026-10-01",
  "compatibility_flags": ["nodejs_compat"],
  "durable_objects": { "bindings": [{ "name": "VICTIM", "class_name": "Victim" }] },
  "migrations": [{ "tag": "v1", "new_sqlite_classes": ["Victim"] }]
}

vitest.config.ts

import { cloudflareTest } from "@cloudflare/vitest-plugin";
import { defineConfig } from "vitest/config";

export default defineConfig({
	plugins: [cloudflareTest({ wrangler: { configPath: "./wrangler.jsonc" } })],
});

src/lazy.ts

export const value = 1;

src/index.ts

import { DurableObject } from "cloudflare:workers";

export class Victim extends DurableObject {
	importAndAbort(abort: boolean) {
		void import("./lazy");
		if (abort) this.ctx.abort("stopped during an import");
	}
}

export default { fetch: () => new Response("ok") };

test/hang.test.ts

import { env } from "cloudflare:test";
import { expect, it } from "vitest";

const importLazy = () =>
	Promise.race([
		import("../src/lazy").then(() => "loaded"),
		scheduler.wait(3_000).then(() => "hung"),
	]);

it("control: no abort", async () => {
	await env.VICTIM.getByName("a").importAndAbort(false);
	await scheduler.wait(200);
	expect(await importLazy()).toBe("loaded");
});

it("abort during an import", async () => {
	await env.VICTIM.getByName("b").importAndAbort(true).catch(() => {});
	expect(await importLazy()).toBe("loaded");
}, 10_000);

Run npx vitest run. The control passes. The second test fails with expected 'hung' to be 'loaded'.

Describe the Bug

If ctx.abort() stops a Durable Object while a dynamic import() is in progress, each later import of that module in the same test file waits forever, also from the test itself.

The cause seems to be in Vite's module runner. ModuleRunner.cachedModule keeps each import in progress in concurrentModuleNodePromises, and removes the entry in a .finally(). It asks for the module through the transport on each import, also when the module has run already. When the Durable Object is aborted, the entry stays in the map, and later imports of that URL wait for a promise that does not settle.

This is hard to find, because it shows up as a timeout in tests that come later, and only when the abort falls inside the import. A library that imports modules lazily on each call makes this more likely: convex-test does this.

The expected behaviour is that an abort does not affect imports made by other contexts.

A workaround is to keep each module after its first load, so that later calls do not import. An abort during the first import of a module can still cause the hang.

Please provide any relevant error logs

There is no error. The later import does not settle.

Dominant language
TypeScript
Stars
4.7k
Forks
1.7k
Avg merge
1d 22h
Merged PRs (30d)
282

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudflare/workers-sdk

All issues in cloudflare/workers-sdk

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.