serverpod-sessions

Guide safe Serverpod session lifecycle and closure rules.

3.2k|369|Updated May 22, 2021
One-click install
npx skills add https://github.com/serverpod/serverpod --skill serverpod-sessions
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: serverpod-sessions
Source: https://github.com/serverpod/serverpod/tree/main/packages/serverpod/skills/serverpod-sessions
Command: npx skills add https://github.com/serverpod/serverpod --skill serverpod-sessions

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Prevents session-related bugs by teaching how Serverpod sessions are created, used, and closed so you avoid leaked resources and “session closed” errors.

Core Features & Use Cases

  • Session types and lifetimes: Understand which session is created for method calls, web routes, streams, WebSockets, future calls, and manual internal sessions.
  • Safe manual session management: Learn how to close InternalSession reliably (including why unclosed sessions leak memory and logs).
  • Cleanup callbacks and correct timing: Use addWillCloseListener correctly and avoid capturing request sessions for work that runs after the endpoint returns.
  • Use case: When debugging a “StateError: session already closed” caused by a delayed callback (like a timer), apply the guidance to refactor the work into a future call or a fresh internal session.

Quick Start

Ask the assistant to explain which session type you’re using and how to refactor your code so it never uses a session after the endpoint returns.

Frequently Asked Questions about serverpod-sessions

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why am I getting a "StateError: session already closed" in my Dart Serverpod endpoint?

A "StateError: session already closed" occurs when a delayed callback like a timer captures a request-scoped session and runs after the endpoint returns. Refactor the delayed work into a future call or a fresh internal session.

How do I safely manage InternalSession lifecycle in Serverpod for long-running tasks?

Safely manage InternalSession lifecycle by creating and closing the session inside a finally block. This ensures reliable async cleanup and prevents leaked memory and logs if the long-running task throws an exception.

When should I use will-close cleanup listeners in Serverpod WebSocket handling?

Use will-close cleanup listeners in Serverpod WebSocket handling to execute async cleanup callbacks correctly before the stream terminates. This prevents capturing request sessions for work that runs beyond the stream's lifetime.

What is the correct session lifetime for Serverpod future calls and web routes?

The correct session lifetime for Serverpod future calls and web routes is strictly bound to the request or stream scope. Avoid capturing these request-scoped sessions for any background work that outlives the endpoint execution.

How do I prevent Serverpod session leaks when handling async streams?

Prevent Serverpod session leaks by avoiding the capture of request-scoped sessions beyond the stream lifetime. Use addWillCloseListener correctly and ensure all async cleanup logic executes before the stream closes.

Do I need to manually close InternalSession for every Serverpod endpoint method?

You only need to manually create and close InternalSession for longer-running work outside standard endpoint methods. Standard method calls, web routes, and streams handle request-scoped session lifetimes automatically upon return.