Skip to content

APIClient singleton holds a closed Project after Project.create #70

Open

Description

@elainethale

Problem

APIClient is a process-wide singleton (APIClient.__new__ returns cls._instance). Calling APIClient(project) with a project at a different path switches the shared instance to that project. Calling it with a project at the same path keeps the existing Project object, even if that object has since been closed.

Project.create triggers this. populate_palette_metrics (src/stride/project.py) calls APIClient(self) with the project being created. create then closes that project and returns a freshly loaded one at the same path. The singleton still holds the closed project:

from stride import Project
from stride.api import APIClient

p = Project.create("cfg.json5", dataset="global-test")
APIClient(p).scenarios  # ConnectionException: Connection already closed!

Reproduced on bug-68/time-zone-alignment with the global-test dataset. The behavior predates that branch.

Other effects

  • Any code that creates or refreshes a project's palette switches the shared client to that project. That includes Project.create, populate_palette_metrics, and the CLI palette command (src/stride/cli/stride.py). Any other holder of the client, such as a notebook or the test suite's session api_client fixture, then silently queries a different project. PR Align temporal data in local standard time (fixes #68) #69 had to work around this in tests/test_time_alignment.py by saving and restoring APIClient._instance around Project.create.
  • The viewer (src/stride/ui/app.py) depends on the singleton on purpose. It calls APIClient(cached_project) in several places to "update the singleton" when switching between cached projects. Any fix has to keep project switching in the viewer working.

Possible directions

  • Narrow: have populate_palette_metrics (and the CLI palette command) query sectors and end uses directly through self.con, instead of going through APIClient.
  • Broader: make APIClient an ordinary object owned by its caller. The viewer would hold the current client in its project cache, alongside the Project. That removes the hidden global state, and with it the stale-project and project-switching side effects.
  • Either way: when the path matches but the stored Project is a different object (or closed), APIClient.__init__ should replace it rather than keep it.

🤖 Generated with Claude Code

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions