Repository navigation
Open
Labels
Description
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_changecallbacks keep firing (and an accepted reload callsget()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
pip install "priompt-sdk[nats]"- Start a priompt server from priompt/priompt with its embedded broker:
priompt serve(NATS listens on127.0.0.1:4222by default and the demo promptpriompt://acme/onboarding/welcomeis seeded). - 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)
- 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 whoseclose()/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.PromptClientsupportswith(__exit__callsclose()).
Where to look
priompt_sdk/client.py#L193-L237thread + loop +nats.connect,while True: sleep(3600), returns theThreadpriompt_sdk/client.py#L239-L240close()only closesself._chanREADME.md#L151-L159API list (no way to unsubscribe)
Suggested approach
- Introduce a small
Subscriptionobject holding the thread, its loop, the NATS connection and a stopasyncio.Event; replace the sleep loop withawait stop.wait()followed byawait nc.drain(). Subscription.close()sets the event withloop.call_soon_threadsafeand 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 keepjoin()/is_alive()on the handle for compatibility).
Acceptance criteria
-
subscribe()returns a handle withclose()/unsubscribe(); after calling it no callbacks are delivered and the thread has exited. -
client.close()stops all subscriptions it started; a test with a fakednatsmodule 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