Skip to content

close() leaves subscriptions running and there is no way to unsubscribe #16

Open

Description

@sujal-maheshwari2004

Summary

Each subscribe() call starts a daemon thread with its own asyncio loop and NATS connection that runs while True: await asyncio.sleep(3600). The client keeps no reference to it, close() only closes the gRPC channel, and the return value is a bare threading.Thread, so a subscription cannot be stopped.

Impact

  • After client.close(), on_change callbacks keep firing (and an accepted reload calls get() on the closed channel).
  • Code that subscribes and closes repeatedly (tests, per-request clients, hot-reloading workers) leaks one thread and one broker connection each time.
  • There is no context-manager support, so with PromptClient(...) as c: is not available.

Steps to reproduce

  1. pip install "priompt-sdk[nats]"
  2. Start a priompt server from priompt/priompt with its embedded broker: priompt serve (NATS listens on 127.0.0.1:4222 by default and the demo prompt priompt://acme/onboarding/welcome is seeded).
  3. Run:
    import time
    from priompt_sdk import PromptClient
    c = PromptClient(host="localhost:8443", nats_url="nats://127.0.0.1:4222")
    c.subscribe("priompt://acme/onboarding/welcome",
                lambda v, k: print("still delivered:", v, k))
    c.close()
    time.sleep(60)
  4. While it sleeps, publish a new version through the server:
    echo "Hello {name}, welcome to {org}!" | priompt publish -addr localhost:8443 \
      -uri priompt://acme/onboarding/welcome -slot name -slot org

Actual: still delivered: <hash> <verdict> is printed after close(). The NATS connection stays open until the process exits.

Expected behavior

  • subscribe() returns a handle whose close()/unsubscribe() drains the NATS connection, stops the loop and joins the thread.
  • client.close() stops every subscription the client started, then closes the gRPC channel.
  • PromptClient supports with (__exit__ calls close()).

Where to look

Suggested approach

  • Introduce a small Subscription object holding the thread, its loop, the NATS connection and a stop asyncio.Event; replace the sleep loop with await stop.wait() followed by await nc.drain().
  • Subscription.close() sets the event with loop.call_soon_threadsafe and joins the thread with a timeout.
  • Keep a list of live subscriptions on the client; close() closes them all before the channel, and is idempotent.
  • Add __enter__/__exit__.
  • Changing the return type of subscribe() is visible to callers; note it in the changelog (or keep join()/is_alive() on the handle for compatibility).

Acceptance criteria

  • subscribe() returns a handle with close()/unsubscribe(); after calling it no callbacks are delivered and the thread has exited.
  • client.close() stops all subscriptions it started; a test with a faked nats module shows the handler is not invoked after close and the thread is no longer alive.
  • with PromptClient(...) as c: works and closes everything on exit.
  • README documents how to stop a subscription.

Activity

  1. added
    bug
    help wanted
    on Nov 14, 2026
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

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions