Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONDecodeError after a valid JetStream publish() call #639

Open
pisymbol opened this issue Dec 3, 2024 · 0 comments
Open

JSONDecodeError after a valid JetStream publish() call #639

pisymbol opened this issue Dec 3, 2024 · 0 comments
Labels
defect Suspected defect such as a bug or regression

Comments

@pisymbol
Copy link

pisymbol commented Dec 3, 2024

Observed behavior

While publishing valid JSON to a stream, the nats client throws an exception:

await self._js.publish(pub_subject, json.dumps(js).encode())
  File "/usr/local/lib/python3.11/site-packages/nats/js/client.py", line 191, in publish
    resp = json.loads(msg.data)
           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Expected behavior

I expect the message to be sent to the stream and delivered to the correct consumer.

Server and client version

# nats --version
0.1.4

/ # nats-server -v
nats-server: v2.9.20

Host environment

Linux nats-0 5.10.213-201.855.amzn2.aarch64 #1 SMP Mon Mar 25 18:16:08 UTC 2024 aarch64 Linux

Three replica cluster on k8s.

Steps to reproduce

This is harder to really specify since it happens randomly. But the root of the cause is the byte sequence returned in msg.data within the async handle callback function tries to load the payload as JSON and fails because it gets back from the server b'' instead of valid JSON:

150         try:
151             resp = json.loads(msg.data)
152             if 'error' in resp:
153                 err = nats.js.errors.APIError.from_error(resp['error'])
154                 future.set_exception(err)
155                 return
156
157             ack = api.PubAck.from_response(resp)
158             future.set_result(ack)
159         except (asyncio.CancelledError, asyncio.InvalidStateError):
160             pass```


So what's interesting is the assumption that `json.loads()` here will always succeed since `json.decoder.JSONDecodeError` could be thrown but uncaught (at least here).
@pisymbol pisymbol added the defect Suspected defect such as a bug or regression label Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect Suspected defect such as a bug or regression
Projects
None yet
Development

No branches or pull requests

1 participant