Skip to content

subscribe() hangs forever if anything fails after connecting to NATS #19

Open

Description

@sujal-maheshwari2004

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

  1. pip install "priompt-sdk[nats]"
  2. Run this script. It replaces nats with a minimal fake whose subscribe() 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

Suggested approach

  • Wrap the whole body of main() up to ready.set() in try/except Exception as e: failed.append(e) with finally: ready.set() (only the first set() matters).
  • Give ready.wait() a timeout (for example 30 seconds); on timeout raise TimeoutError/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 run pytest -q from the repo root.
  • Code is in priompt_sdk/client.py (PromptClient.subscribe); tests live in tests/test_client.py, which already fakes the gRPC stub. The script above shows how to fake the nats module via sys.modules for a test.

Acceptance criteria

  • If nc.subscribe() (or anything else after connect) raises, subscribe() raises ConnectionError promptly instead of hanging.
  • ready.wait() has a bounded timeout.
  • A test using a faked nats module reproduces the hang before the fix and passes after.

Activity

  1. added
    bug
    good first issue
    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

    buggood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions