Skip to content

fix: raise a clear error when subscribe can't connect to NATS - #8

Merged
sujal-maheshwari2004 merged 1 commit into
mainfrom
fix/4-nats-auth-error
Oct 18, 2026
Merged

sujal-maheshwari2004 merged 1 commit into
mainfrom
fix/4-nats-auth-error

Conversation

@sujal-maheshwari2004

Copy link
Copy Markdown
Contributor

What was wrong

subscribe() ran nats.connect on a background thread and returned right away. With a wrong broker token, nats-py logged a full Authorization Violation traceback on every retry (for minutes, by default), and the caller held a live thread that would never receive an event.

Fix

  • subscribe() now waits until the connection is up and the subscription is registered.
  • If the first connect fails, subscribe() raises ConnectionError with the cause, e.g. subscribe: cannot connect to NATS at 127.0.0.1:49422: nats: 'Authorization Violation'. The token in nats_url is not echoed. It fails fast instead of retrying: an error_cb re-raises before the first successful connect, which aborts nats.connect.
  • After it connects, broker errors are logged as one warning line on the priompt_sdk logger, not a traceback. nats-py still reconnects by itself.
  • Side effect: because subscribe() now returns only after the subscription is registered, an event published right after it returns is no longer missed.

Tests

test_subscribe_raises_when_broker_refuses uses a fake nats module that refuses the connection the way nats-py does.

before the fix: FAILED tests/test_client.py::test_subscribe_raises_when_broker_refuses - Failed: DID NOT RAISE
                1 failed, 14 passed
after the fix:  15 passed in 0.25s

Real run against priompt v0.7.2 serve -nats-token natstok, subscribing with nats://WRONG@127.0.0.1:49422:

Before: subscribe() returned <Thread(Thread-1 (run), started daemon ...)>, thread alive after 4s: True, and 6 tracebacks on stderr (nats: encountered error ... nats.errors.Error: nats: 'Authorization Violation', plus ConnectionResetError: [WinError 10054]). No events ever arrived.

After:

subscribe() raised: ConnectionError subscribe: cannot connect to NATS at 127.0.0.1:49422: nats: 'Authorization Violation'

(in 0.5 s, no traceback). With the right token, subscribe() returns the thread as before. With nothing listening on the port, it raises ... at 127.0.0.1:49499: TimeoutError after about 2.6 s.

Expected conflict

This PR and #7 (fix for #3) both change subscribe() in priompt_sdk/client.py and add tests at the end of tests/test_client.py. Each is based on main. Whichever merges second needs a rebase. The two changes are independent: #7 changes the message handler, and this PR changes the connect, wait, and error path. Keep both.

Fixes #4

🤖 Generated with Claude Code

With a wrong broker token, nats.connect ran in the background thread,
logged an 'Authorization Violation' traceback on every retry, and
subscribe() returned a thread that never received events.

subscribe() now waits for the connection and the subscription. If the
first connect fails, it raises ConnectionError with the cause (the token
in nats_url is not echoed) instead of retrying for minutes. Once
connected, broker errors are logged as one warning line, not a traceback;
nats-py still reconnects on its own. Returning only after the
subscription is registered also means an event published right after
subscribe() returns is no longer missed.

Fixes #4

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sujal-maheshwari2004
sujal-maheshwari2004 merged commit 7bb6662 into main Oct 18, 2026
1 check passed
@sujal-maheshwari2004
sujal-maheshwari2004 deleted the fix/4-nats-auth-error branch October 18, 2026 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong NATS token: logs tracebacks and keeps running with no events

1 participant