Skip to content

onCleanup not always called on the server #1599

@marbemac

Description

@marbemac

Describe the bug

When a resource is created outside of the <Suspense> boundary that wraps its data access, the first such resource will not have related onCleanup functions triggered on the server (only the first resource created this way).

Kind of nuanced, and there seems to be some other strange but related behavior at play here as well. Easier to show with some examples.

Here's our resource helper - it just creates and returns a new resource, and registers an onCleanup function:

https://github.com/marbemac/solid-oncleanup-ssr-bug/blob/ef6a572b581791ccabaf6439061b70d6e0c59e91/src/routes/index.tsx#L25-L39

A DataComp component that does not include it's own suspense boundary:

https://github.com/marbemac/solid-oncleanup-ssr-bug/blob/ef6a572b581791ccabaf6439061b70d6e0c59e91/src/routes/index.tsx#L41-L51

A DataCompWithInnerSuspense component that DOES wrap access to the resource data in its own suspense boundary:

https://github.com/marbemac/solid-oncleanup-ssr-bug/blob/ef6a572b581791ccabaf6439061b70d6e0c59e91/src/routes/index.tsx#L53-L67

Scenarios

Render a single DataCompWithInnerSuspense

<DataCompWithInnerSuspense name="DataCompWithInnerSuspense-1" />

Resulting server logs

Note no onCleanup.

call:DataCompWithInnerSuspense-1
call:DataCompWithInnerSuspense-1.resource

Render more than one DataCompWithInnerSuspense

<DataCompWithInnerSuspense name="DataCompWithInnerSuspense-1" />
<DataCompWithInnerSuspense name="DataCompWithInnerSuspense-2" />

Resulting server logs

Note the second comp did have it's onCleanup triggered.

call:DataCompWithInnerSuspense-1
call:DataCompWithInnerSuspense-1.resource
call:DataCompWithInnerSuspense-2
call:DataCompWithInnerSuspense-2.resource
onCleanup:DataCompWithInnerSuspense-2.resource
onCleanup:DataCompWithInnerSuspense-2

Render DataComp + one or more DataCompWithInnerSuspense

<Suspense>
  <DataComp name="DataComp-1" />
</Suspense>

<DataCompWithInnerSuspense name="DataCompWithInnerSuspense-1" />

Resulting server logs

In this case onCleanup IS called for DataCompWithInnerSuspense-1.

Note however that there might be some other weirdness, not sure why the the DataComp logs are triggered again at the end (last 4 lines).

call:DataComp-1
call:DataComp-1.resource
call:DataCompWithInnerSuspense-1
call:DataCompWithInnerSuspense-1.resource
onCleanup:DataComp-1.resource
onCleanup:DataComp-1
onCleanup:DataCompWithInnerSuspense-1.resource
onCleanup:DataCompWithInnerSuspense-1
call:DataComp-1
call:DataComp-1.resource
onCleanup:DataComp-1.resource
onCleanup:DataComp-1

Your Example Website or App

https://github.com/marbemac/solid-oncleanup-ssr-bug

Steps to Reproduce the Bug or Issue

  1. Clone https://github.com/marbemac/solid-oncleanup-ssr-bug.
  2. pnpm install
  3. pnpm dev
  4. Load http://127.0.0.1:3001
  5. Adjust the components rendered to mess around with the various scenarios described in this issue (https://github.com/marbemac/solid-oncleanup-ssr-bug/blob/main/src/routes/index.tsx#L11-L18)
  6. Reload the page, view server-side logs in terminal

Expected behavior

onCleanup is called consistently.

Screenshots or Videos

No response

Platform

  • OS: na
  • Browser: na
  • Version: 1.6.12

Additional context

Originally ran into this behavior while trying to debug an issue in @tanstack/solid-query. onCleanup is used there to unsubscribe various observables, and the fact that it is not called consistently when on the server is causing problems.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions