Skip to content

@solid-primitives/form CLEANUP_IN_FORBIDDEN_SCOPE #1143

Description

@akawahuynh

Describe the bug

The usage documented in the README:

import { createForm } from "@solid-primitives/form";


export default function Form() {
  const form = createForm({ fields: { email: { initial: "" } } });

  return (
    <form ref={form.ref}>
      <input ref={form.bind("email")} />
      <button type="submit">Submit</button>
    </form>
  );
}

Expected
Refs attach, the submit listener intercepts the native submit, and the form hydrates normally (as in Solid 1.x).
Actual
Dev build throws from the ref callback:

Error in a Solid app

Error: [CLEANUP_IN_FORBIDDEN_SCOPE] Cannot use onCleanup inside a ref callback; register it in the factory that returns the ref (the component body)
  • Page: /register

Stack

  • … 9 frames in dependencies: @solidjs/signals, @solid-primitives/utils, @solid-primitives/event-listener, @solid-primitives/form, @solidjs/web
  • ref={form.ref} at /src/components/auth-form.tsx:99:43

Source

/src/components/auth-form.tsx:99:43

   96 |           </p>
   97 |         </div>
   98 |
>  99 |         <form class="flex flex-col gap-4" ref={form.ref}>
      |                                           ^
  100 |           <Show when={!isLogin()}>
  101 |             <Field data-invalid={form.fields.name.error() ? "true" : undefined}>
  102 |               <FieldLabel for="name">Name</FieldLabel>

The throw aborts hydration of the rest of the tree. In an SSR + hydrate app the form's submit handler is never attached, so pressing submit performs a native GET submission — the fields, including the password, end up in the URL query string. The console also reports Hydration completed with N unclaimed server-rendered node(s) for the form's elements.
Root cause
packages/form/src/form.ts:

  • ref calls makeEventListener(el, "submit", ...)
  • bind calls makeEventListener(el, "input"|"change", ...) and makeEventListener(el, "blur", ...)
    makeEventListener registers its removal with tryOnCleanup → onCleanup synchronously inside the ref callback. Solid 2 forbids cleanup registration there by design (dev throws; production strips the throw but the cleanup has no owner and never runs, leaking listeners). The Solid 2 contract is that a ref callback may only return its cleanup.

Minimal Reproduction Link

https://playground.solidjs.com/anonymous/1245aa5d-4c85-4e46-b234-e91fba5eeab1

Activity

  1. changed the title [-]form.ref / form.bind throw CLEANUP_IN_FORBIDDEN_SCOPE on Solid 2 and break form hydration[/-] [+]@solid-primitives/form CLEANUP_IN_FORBIDDEN_SCOPE[/+] on Nov 27, 2026
  2. added
    bug
    on Nov 27, 2026
  3. added theissue type on Nov 27, 2026
  4. davedbase commented on Nov 27, 2026

    @davedbase
    Member

    @akawahuynh let me know if the PR fully resolves the issue for you. I'll merge it as soon as you do.

  5. akawahuynh commented on Nov 27, 2026

    @akawahuynh
    Author

    All functions related to makeEventListener and tryOnCleanup for ref encounter the same issue—for example, focus():

    export function focus(callback: (isActive: boolean) => void): (target: Element) => void {

    Or:

  6. davedbase commented on Nov 27, 2026

    @davedbase
    Member

    @akawahuynh good point I didn't think of it. I had an agent go through and identify the other call sites that are impacted (mediastream, lifecycle, active-element, cursor, mutation-observer and event-listener).

  7. added a commit that references this issue on Nov 28, 2026
    2f75087
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

    bugsolid-2.0

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions