Repository navigation
fix: raise a clear error when subscribe can't connect to NATS - #8
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
subscribe()rannats.connecton a background thread and returned right away. With a wrong broker token, nats-py logged a fullAuthorization Violationtraceback 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.subscribe()raisesConnectionErrorwith the cause, e.g.subscribe: cannot connect to NATS at 127.0.0.1:49422: nats: 'Authorization Violation'. The token innats_urlis not echoed. It fails fast instead of retrying: anerror_cbre-raises before the first successful connect, which abortsnats.connect.priompt_sdklogger, not a traceback. nats-py still reconnects by itself.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_refusesuses a fakenatsmodule that refuses the connection the way nats-py does.Real run against
priompt v0.7.2 serve -nats-token natstok, subscribing withnats://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', plusConnectionResetError: [WinError 10054]). No events ever arrived.After:
(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: TimeoutErrorafter about 2.6 s.Expected conflict
This PR and #7 (fix for #3) both change
subscribe()inpriompt_sdk/client.pyand add tests at the end oftests/test_client.py. Each is based onmain. 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