nats.py
nats.py copied to clipboard
with Jetstream, get "nats: timeout" when Add Stream or pull_subscribe to an existing stream
Hi,
I'm using nats.py version 2.2.0
, latest stable release.
I was using simple NATS pub/sub without JetStream. To use Jetstream, recently I started making the change..
When I tried implementing Pull Subscriber model in Python, my pull_subscribe(..)
call to an existing Stream started failing with timeout
.
So, I created a simple independent script as following based on NATS doc examples.. which gave similar timeout example even at an earlier stage when trying to add a new stream.
I'm running NATS server with Jetstream nats-server -js -p 4222 -DV
.
Code & Error
import asyncio
import nats
from nats.errors import TimeoutError
nats_url = "nats://127.0.0.1:4222"
async def main():
nc = await nats.connect(nats_url)
js = nc.jetstream()
await js.add_stream(
name="test.x",
subjects=["test.x.todo", "test.x.done"],
)
ack = await js.publish("test.x.todo", f"hello world".encode())
print(ack)
psub = await js.pull_subscribe("test.x.todo", "test.psub")
msgs = await psub.fetch(1)
for msg in msgs:
print(msg)
await nc.close()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
- Error
$ python sample.py
Traceback (most recent call last):
File "/usr/lib64/python3.11/asyncio/tasks.py", line 490, in wait_for
return fut.result()
^^^^^^^^^^^^
asyncio.exceptions.CancelledError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "$MY_APP_PATH/.venv/lib/python3.11/site-packages/nats/aio/client.py", line 999, in _request_new_style
msg = await asyncio.wait_for(future, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib64/python3.11/asyncio/tasks.py", line 492, in wait_for
raise exceptions.TimeoutError() from exc
TimeoutError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "$MY_APP_PATH/workers/sr-worker/sample.py", line 28, in <module>
loop.run_until_complete(main())
File "/usr/lib64/python3.11/asyncio/base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "$MY_APP_PATH/workers/sr-worker/sample.py", line 11, in main
await js.add_stream(
File "$MY_APP_PATH/.venv/lib/python3.11/site-packages/nats/js/manager.py", line 89, in add_stream
resp = await self._api_request(
^^^^^^^^^^^^^^^^^^^^^^^^
File "$MY_APP_PATH/.venv/lib/python3.11/site-packages/nats/js/manager.py", line 355, in _api_request
msg = await self._nc.request(req_subject, req, timeout=timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "$MY_APP_PATH/.venv/lib/python3.11/site-packages/nats/aio/client.py", line 963, in request
msg = await self._request_new_style(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "$MY_APP_PATH/.venv/lib/python3.11/site-packages/nats/aio/client.py", line 1012, in _request_new_style
raise errors.TimeoutError
nats.errors.TimeoutError: nats: timeout
I've got the same problem. I tried with older versions of the lib and it didn't work either.
It appears to be the latest version of NATS (2.9.15) that is causing this issue. When I reverted NATS to 2.9.14 (or 2.9.12), I was able to create a pull_subscribe and get messages from the consumer.
For us this was caused by putting dots in the durable name.
@ajlane ah thanks, good to know.. I think my subset had dots in durable name as well... I'll check a non-dot name and report back