Skip to content

Pending nested derived snapshots can prevent the scheduler from stabilizing #1735

Closed

Description

@domgan-oai

Summary

Activating a component-local query$ can leave the Octane scheduler unable to stabilize when an acyclic chain of derived$ values reads a pending nested snapshot. The request in the reproduction resolves immediately, but act() reaches its 100-iteration drain limit before the rendered result can settle.

Reproduction

Tested against the tree of octanejs/octane main at commit 7a92ad2a158944cec109ba35e8caa5561cfd7041, with Node 22.23.0, Vitest 4.1.10 and jsdom. Install the repository dependencies, then add these two files at the repository root paths shown below.

packages/octane/tests/signal-pending-issue-repro.test.ts:

import { describe, expect, it } from 'vitest';
import { act } from 'octane';
import * as signals from 'octane/signals';
import { mount } from './_helpers.js';
import { loadCompiledFixtureSource } from './_server-fixture.js';

const { App } = loadCompiledFixtureSource<any>(
  `import { derived$, query$, signal$, skip } from 'octane/signals';
export function App(props) @{
  const active$ = signal$(false);
  const eligible$ = derived$(() => props.enabled);
  const primary$ = query$(() => active$.get() && eligible$.get() ? props.key : skip, props.load);
  const latest$ = derived$(() => primary$.latest(undefined));
  const usePrimary$ = derived$(() => primary$.snapshot().status !== 'error' || latest$.get() !== undefined);
  const selected$ = derived$(() => {
    if (!active$.get() || !eligible$.get()) return null;
    if (usePrimary$.get()) return latest$.get() ?? primary$.get();
    return null;
  });
  const available$ = derived$(() => {
    const snapshot = selected$.snapshot();
    return snapshot.status === 'ready' && snapshot.value !== null;
  });
  const fallback$ = derived$(() => selected$.snapshot().status !== 'ready');
  const visible$ = derived$(() => String(available$.get()) + ':' + String(fallback$.get()));
  <main>
    <button onClick={() => active$.set(true)}>{'load'}</button>
    <p>{fallback$.get() ? 'pending' : 'settled'}</p>
    <span>{visible$.get() as string}</span>
  </main>
}`,
  {
    id: '/signal-pending-issue-repro.tsrx',
    mode: 'client',
    compileOptions: { dev: true, strong: false, hmr: false },
    runtimeModules: { 'octane/signals': signals },
  },
);

describe('pending derived snapshot', () => {
  it('stabilizes after a query becomes ready', async () => {
    const calls: string[] = [];
    const load = (key: string) => {
      calls.push(key);
      return Promise.resolve('value');
    };
    const root = mount(App, { key: 'a', enabled: true, load });
    try {
      expect(root.find('span').textContent).toBe('false:false');
      await act(() => root.click('button'));
      expect(calls).toEqual(['a']);
      expect(root.find('span').textContent).toBe('true:false');
    } finally {
      root.unmount();
    }
  });
});

vitest.signal-upstream-issue.config.mjs:

import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { defineConfig } from 'vitest/config';
import { octane } from './packages/octane/src/compiler/vite.js';

const root = resolve(import.meta.dirname, 'packages/octane');
const pkg = JSON.parse(readFileSync(resolve(root, 'package.json'), 'utf8'));
const alias = Object.entries(pkg.exports).map(([name, target]) => ({
  find: name === '.' ? 'octane' : 'octane' + name.slice(1),
  replacement: resolve(root, typeof target === 'string' ? target : target.default),
})).sort((a, b) => b.find.length - a.find.length);
export default defineConfig({
  resolve: { alias },
  plugins: [octane({ hmr: false })],
  test: {
    environment: 'jsdom',
    setupFiles: ['packages/octane/tests/_per-test-setup.ts'],
    include: ['packages/octane/tests/signal-pending-issue-repro.test.ts'],
  },
});

Run:

./node_modules/.bin/vitest run -c vitest.signal-upstream-issue.config.mjs --reporter=verbose

Expected: The click starts a single request and the visible result eventually becomes true:false.

Actual: The click fails with act(): scheduler did not stabilize after 100 iterations — likely an infinite render loop, before the final assertion. The compiled fixture uses development mode with Strong disabled; the signal graph contains no intentional cycle.

Activity

  1. added
    bug
    agent-authored
    on Oct 5, 2026
  2. domgan-oai commented on Oct 5, 2026

    @domgan-oai
    ContributorAuthor

    Additional reproduction: a layout-effect microtask activates a deferred query. A derived value reads the query, and two other derived values consume its snapshot.

    On current main (7a92ad2), the jsdom test below fails at the first act with “scheduler did not stabilize after 100 iterations,” before reaching the click. A separate Chromium test using the published 0.9.1 package also stalls with a busy renderer while the query is pending; an instrumented run shows the click handler enters and exits, but the awaited interaction does not complete. Resolving the query before the interaction is a passing control.

    To reproduce in the repository, add the following fixture and test under the existing Octane test directories and run the signals test project. The test uses the existing test helpers.

    Fixture: packages/octane/tests/_fixtures/signal-layout-microtask-repro.tsrx

    /** @jsxImportSource octane */
    import { useEffectEvent, useLayoutEffect, useState } from "octane";
    import { captureSignalOwner, currentSignalOwner, derived$, query$, signal$, skip, type WritableSignal } from "octane/signals";
    
    export type Data = { values: readonly number[] };
    export type Fixture = { enabled$: WritableSignal<boolean>; load: (key: string) => Promise<Data> };
    const isEnabled$ = (fixture: Fixture) => fixture.enabled$.get();
    
    export function Example({ fixture }: { fixture: Fixture }) @{
      const [open, setOpen] = useState(false);
      const active$ = signal$(false);
      const eligible$ = derived$(() => isEnabled$(fixture));
      const request$ = query$(() => active$.get() && eligible$.get() ? "a" : skip, fixture.load);
      const selected$ = derived$(() => {
        if (!active$.get() || !eligible$.get()) return null;
        return request$.get();
      });
      const hasValues$ = derived$(() => {
        if (!active$.get() || !eligible$.get()) return false;
        const result = selected$.snapshot();
        return result.status === "ready" && (result.value?.values.length ?? 0) > 0;
      });
      const empty$ = derived$(() => {
        if (!active$.get() || !eligible$.get()) return false;
        const result = selected$.snapshot();
        return result.status === "error" || (result.status === "ready" && result.value?.values.length === 0);
      });
      const visible$ = derived$(() => hasValues$.get() || empty$.get());
      const run = captureSignalOwner(currentSignalOwner()!);
      const activate = useEffectEvent(() => run(() => active$.set(true)));
      useLayoutEffect(() => { queueMicrotask(activate); });
      <section>
        <button id="open" onClick={() => setOpen(true)}>{"Open"}</button>
        @if (open) { <div id="panel">{"Panel"}</div> }
        @if (visible$.get()) { <output id="result">{"Loaded"}</output> }
      </section>
    }

    Test: packages/octane/tests/signals-layout-microtask-repro.test.ts

    import { expect, it, vi } from 'vitest';
    import { createScope } from 'octane/signals';
    import { act, mount } from './_helpers.js';
    import { Example, type Data } from './_fixtures/signal-layout-microtask-repro.tsrx';
    
    it('handles an unrelated interaction while a layout-activated query is pending', async () => {
      const scope = createScope({ scopeKey: 'layout-query' });
      const enabled$ = scope.signal$('enabled', true);
      let resolve!: (value: Data) => void;
      const pending = new Promise<Data>((done) => { resolve = done; });
      const load = vi.fn(() => pending);
      const root = mount(Example, { fixture: { enabled$, load } });
      try {
        await act(() => {});
        expect(load).toHaveBeenCalledOnce();
        expect(root.container.querySelector('#result')).toBeNull();
        await act(() => root.click('#open'));
        expect(root.find('#panel').textContent).toBe('Panel');
        expect(root.container.querySelector('#result')).toBeNull();
        await act(() => resolve({ values: [] }));
        expect(root.find('#result').textContent).toBe('Loaded');
      } finally {
        resolve({ values: [] });
        root.unmount();
        scope.dispose();
      }
    });
  3. 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

    agent-authoredbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions