Skip to content

SessionFileHandler: no TTL or cleanup — session files accumulate indefinitely #36

@justin-k-bruce

Description

@justin-k-bruce

Problem

When using SessionFileHandler (the default), session files in the sessions/ directory are never cleaned up. The only way to delete a session file is by explicitly calling session.close(), but the framework's ASGI handler (__init__.py:422) never calls it after request processing.

This is especially problematic for WebSocket endpoints: since WebSocket clients don't send cookies, every single WebSocket connection creates a new orphaned session file that is never read or deleted.

In production, this led to 24,800+ orphaned session files accumulating on disk.

Root Cause

In __init__.py lines 419-422:

if os.getenv("TINA4_SESSION", "PY_SESS") in webserver.cookies:
    webserver.session.load(webserver.cookies[...])
else:
    self.cookies["PY_SESS"] = webserver.session.start()  # creates file, never cleaned up

session.start() calls save() which writes a file to disk. After the response is sent, the session is never closed or cleaned up.

Suggested Fix

Add a TTL-based cleanup mechanism to SessionFileHandler:

  1. Option A: Add a configurable TINA4_SESSION_TTL (e.g., default 3600s). Run periodic cleanup of files older than the TTL.
  2. Option B: Skip session creation entirely for WebSocket upgrade requests (message["type"] == "websocket").
  3. Option C: Call session.close() automatically after each non-WebSocket response if the session was auto-created and unused.

Environment

  • tina4-python 0.2.170
  • WebSocket endpoint using simple_websocket
  • Linux (systemd service)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions