Repository navigation
Description
Summary
subscribe() waits on ready.wait() with no timeout, and only a failure of nats.connect() sets ready on the error path. If anything after the connect raises (for example nc.subscribe() raising because the connection closed in between), the background thread dies with a traceback and the caller blocks forever.
Impact
The calling thread (often application startup) hangs with no error. There is no timeout to fall back on, so the process has to be killed.
Steps to reproduce
pip install "priompt-sdk[nats]"- Run this script. It replaces
natswith a minimal fake whosesubscribe()raises, to simulate the connection dropping between connect and subscribe:import sys, types, threading fake = types.ModuleType("nats") class NC: async def subscribe(self, subject, cb=None): raise RuntimeError("nats: connection closed") async def connect(url, error_cb=None): return NC() fake.connect = connect sys.modules["nats"] = fake from priompt_sdk import PromptClient c = PromptClient(host="127.0.0.1:1", nats_url="nats://127.0.0.1:4222") t = threading.Thread(target=lambda: c.subscribe("priompt://acme/x", print), daemon=True) t.start(); t.join(10) print("subscribe() still blocked after 10s:", t.is_alive())
Actual: a traceback ending in RuntimeError: nats: connection closed from the background thread, then subscribe() still blocked after 10s: True.
Expected behavior
Any exception inside the subscription setup is reported back to the caller: subscribe() raises (wrapping it in ConnectionError like the connect failure) instead of hanging. The wait for readiness is also bounded.
Where to look
priompt_sdk/client.py#L194-L200onlynats.connectis guardedpriompt_sdk/client.py#L220-L221nc.subscribeis unguarded;readyis never set if it raisespriompt_sdk/client.py#L227-L237ready.wait()without a timeout
Suggested approach
- Wrap the whole body of
main()up toready.set()intry/except Exception as e: failed.append(e)withfinally: ready.set()(only the firstset()matters). - Give
ready.wait()a timeout (for example 30 seconds); on timeout raiseTimeoutError/ConnectionError. - Re-raise the recorded error in the caller as
ConnectionError(...) from e, reusing the existing message format.
Getting started
- Set up:
pip install -e ".[nats]" pytest, then runpytest -qfrom the repo root. - Code is in
priompt_sdk/client.py(PromptClient.subscribe); tests live intests/test_client.py, which already fakes the gRPC stub. The script above shows how to fake thenatsmodule viasys.modulesfor a test.
Acceptance criteria
- If
nc.subscribe()(or anything else after connect) raises,subscribe()raisesConnectionErrorpromptly instead of hanging. -
ready.wait()has a bounded timeout. - A test using a faked
natsmodule reproduces the hang before the fix and passes after.
Activity