Skip to content

Pending query selector can hang the browser in development mode #1736

Closed

Description

@domgan-oai

Summary

In Octane 0.9.1, a component-local query$ whose selector reads another pending query can leave the development server's browser tab unresponsive. The upstream loader starts, but the event loop does not reach the next test step while its promise remains pending.

Environment

  • Octane 0.9.1, Node.js 22.23.0, Linux, Vitest 4.1.11, Playwright 1.62.1, Chromium.
  • Vite's serve command with the default Octane compiler options. The transformed fixture contains import.meta.hot.

Reproduction

Create the following files in an empty directory:

package.json:

{
  "name": "octane-pending-derived-repro",
  "private": true,
  "type": "module",
  "scripts": {
    "test:pending": "vitest run --config vite.config.mts -t \"control=false\"",
    "test:control": "vitest run --config vite.config.mts -t \"control=true\""
  },
  "dependencies": {
    "@tsrx/core": "0.4.0",
    "octane": "0.9.1"
  },
  "devDependencies": {
    "@vitest/browser-playwright": "4.1.11",
    "playwright": "1.62.1",
    "typescript": "5.9.3",
    "vite": "8.1.5",
    "vitest": "4.1.11"
  }
}

vite.config.mts:

import { octane } from "octane/compiler/vite";
import { playwright } from "@vitest/browser-playwright";
import { defineConfig } from "vitest/config";
import { writeFileSync } from "node:fs";
const provider = playwright();
export default defineConfig({
  root: import.meta.dirname,
  plugins: [
    { name: "phase-trace", configureServer(server) {
      server.middlewares.use("/__phase", (request, response) => {
        const chunks: Buffer[] = [];
        request.on("data", (chunk: Buffer) => chunks.push(chunk));
        request.on("end", () => {
          process.stdout.write(`[phase] ${Buffer.concat(chunks).toString()}\n`);
          response.statusCode = 204;
          response.end();
        });
      });
    } },
    octane(process.env.OCTANE_HMR === "0" ? { hmr: false } : {}),
    { name: "audit-compiled-fixture", enforce: "post", configResolved(config) {
      process.stdout.write(`[config] command=${config.command} explicitHmrOff=${process.env.OCTANE_HMR === "0"}\n`);
    }, transform(code, id) {
      if (id.includes("query-selector.tsrx")) {
        writeFileSync("compiled-fixture.js", code);
        process.stdout.write(`[fixture] importMetaHot=${code.includes("import.meta.hot")}\n`);
      }
    } },
  ],
  resolve: { dedupe: ["octane"] },
  optimizeDeps: { noDiscovery: true, include: [], exclude: ["octane", "octane/signals"] },
  test: {
    include: ["*.browser.test.ts"],
    browser: { enabled: true, headless: true, provider, instances: [{ browser: "chromium", provider }] },
  },
});

query-selector.tsrx:

import { useEffectEvent, useLayoutEffect, useState } from "octane";
import { captureSignalOwner, currentSignalOwner, derived$, query$, signal$, skip } from "octane/signals";

export type Props = {
  skipWhilePending: boolean;
  loadSource: (key: string) => Promise<string>;
  loadDependent: (key: string) => Promise<string>;
};

export function Example(props: Props) @{
  const [tick, setTick] = useState(0);
  const active$ = signal$(false);
  const source$ = query$(() => active$.get() ? "source" : skip, props.loadSource);
  const dependent$ = query$(() => {
    if (!active$.get()) return skip;
    if (props.skipWhilePending) {
      const state = source$.snapshot();
      if (state.status !== "ready") return skip;
      return `${tick}:${state.value}`;
    }
    return `${tick}:${source$.get()}`;
  }, props.loadDependent);
  const a$ = derived$(() => dependent$.snapshot().status);
  const b$ = derived$(() => dependent$.snapshot().status);
  const run = captureSignalOwner(currentSignalOwner()!);
  const activate = useEffectEvent(() => run(() => active$.set(true)));
  useLayoutEffect(() => {
    let cancelled = false;
    queueMicrotask(() => { if (!cancelled) activate(); });
    return () => { cancelled = true; };
  }, []);
  <section>
    <button id="next" onClick={() => setTick(tick + 1)}>{"Next"}</button>
    <output id="status">{`${tick}:${a$.get()}:${b$.get()}`}</output>
  </section>
}

query-selector.browser.test.ts:

import { createRoot, delegateEvents, flushSync } from "octane";
import { expect, it, vi } from "vitest";
import { userEvent } from "vitest/browser";
import { Example } from "./query-selector.tsrx";

const phase = (value: string) => navigator.sendBeacon("/__phase", value);

for (const skipWhilePending of [false, true]) {
  it(`pending selector control=${skipWhilePending}`, async () => {
    phase("case:start");
    let resolve!: (value: string) => void;
    const gate = new Promise<string>((done) => { resolve = done; });
    let resumed = false;
    const loadSource = vi.fn(async () => {
      phase("loader:start");
      const value = await gate;
      resumed = true;
      phase("loader:resumed");
      return value;
    });
    const loadDependent = vi.fn(async (key: string) => key);
    const host = document.createElement("div");
    document.body.append(host);
    delegateEvents(["click"]);
    const root = createRoot(host);
    try {
      root.render(Example, { skipWhilePending, loadSource, loadDependent });
      flushSync(() => {});
      phase("after:flush");
      await vi.waitFor(() => expect(loadSource).toHaveBeenCalledOnce());
      phase("loader:observed");
      expect(resumed).toBe(false);
      expect(loadDependent).not.toHaveBeenCalled();
      await userEvent.click(host.querySelector<HTMLButtonElement>("#next")!);
      phase("after:click");
      expect(host.querySelector("#status")?.textContent).toContain("1:");
      resolve("value");
      await vi.waitFor(() => expect(host.querySelector("#status")?.textContent).toBe("1:ready:ready"));
      phase("result:observed");
      expect(loadDependent).toHaveBeenCalledWith("1:value", expect.anything());
    } finally {
      resolve("value");
      root.unmount();
      host.remove();
    }
  }, 15_000);
}

Install and run:

npm install --ignore-scripts
npx playwright install chromium
npm run test:control
npm run test:pending

On Linux, an external watchdog such as timeout 30s npm run test:pending can terminate the last command if the browser stops responding. The test's own 15-second timeout did not fire during the observed hang.

Expected: Both tests finish. While the first loader is pending, an unrelated button click updates the UI. Resolving the loader starts the dependent loader with 1:value, and the output becomes 1:ready:ready.

Actual: test:control, which uses snapshot() and skip while pending, passes. test:pending, which uses get() in the dependent selector, logs case:start, after:flush, and loader:start, then does not reach loader:observed before the 30-second watchdog exits with code 124. The test does not reach its assertion about the downstream loader.

As a control, OCTANE_HMR=0 npm run test:pending passes and the transformed fixture does not contain import.meta.hot. The plugin's hmr: false option also selects its non-development compiler path, so this comparison does not isolate the HMR wrapper as the cause. The reproduction does not establish whether a production build is affected.

Activity

  1. added a commit that references this issue on Oct 5, 2026
    21e0575
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions