Skip to content

[Python] Add automatic connection cleanup #170

@gadenbuie

Description

@gadenbuie

Summary

This issue tracks the implementation of automatic connection cleanup in QueryChat for Python, building on the R implementation discussed in PR #164.

Background

PR #164 adds a cleanup parameter to QueryChat's R implementation with automatic cleanup behavior when:

  1. A connection is created by QueryChat (e.g., for a data frame)
  2. The QueryChat object is used within a Shiny app session via on_stop()/onStop() callbacks

Problem Statement

Python's Shiny framework does not yet have an equivalent of R's shiny::onStop() callback mechanism that allows for application-level cleanup. This prevents us from:

  • Automatically cleaning up connections when QueryChat exits a Shiny app
  • Keeping the R/Python cleanup logic in sync
  • Providing consistent behavior across both implementations

Key Considerations

  1. Connection Ownership: Only clean up connections that QueryChat created or explicitly manages, not connections provided by users, unless the QueryChat object is created inside a Shiny app context
  2. Safety First: Avoid destructive actions when a connection is used outside of a Shiny app context
  3. Predictable Behavior: Simple, clear rules about when cleanup happens (not relying on interactive() or other implicit signals)
  4. Consistency: R and Python implementations should follow similar logic

Next Steps

  1. Implement on_stop() in Shiny for Python (equivalent to R's shiny::onStop())
  2. Once available, add Python support for automatic cleanup with similar logic to the R implementation
  3. Document when and how cleanup occurs for users

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions