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

httpx.ResponseNotRead: Attempted to access streaming response content, without having called read() #8

Closed
AveZorgen opened this issue Apr 20, 2024 · 7 comments

Comments

@AveZorgen
Copy link

Hello, I've got an error while using tonapi.sse.subscribe_to_transactions when "HTTP/1.1 502 Bad Gateway" occurs.

My code snippet:

async def handler(event: TransactionEventData, tonapi: AsyncTonapi) -> None:
    pass

async def main() -> None:
    tonapi = AsyncTonapi(api_key=API_KEY, timeout=60)
    await tonapi.sse.subscribe_to_transactions(
        accounts=ACCOUNTS, handler=handler, args=(tonapi,)
    )
    

if __name__ == "__main__":
    asyncio.run(main())

I've also found a possible solution here: https://github.com/encode/httpx/discussions/1856

Stacktrace:

Traceback (most recent call last):
  File "D:\Projects\WhaleWatch\oapi.py", line 112, in main
    await tonapi.sse.subscribe_to_transactions(
  File "D:\Projects\WhaleWatch\venv\lib\site-packages\pytonapi\async_tonapi\methods\sse.py", line 25, in subscribe_to_transactions
    async for data in self._subscribe(method=method, params=params):
  File "D:\Projects\WhaleWatch\venv\lib\site-packages\pytonapi\async_tonapi\client.py", line 118, in _subscribe
    await self.__process_response(response)
  File "D:\Projects\WhaleWatch\venv\lib\site-packages\pytonapi\async_tonapi\client.py", line 80, in __process_response
    content = await self.__read_content(response)
  File "D:\Projects\WhaleWatch\venv\lib\site-packages\pytonapi\async_tonapi\client.py", line 66, in __read_content
    content = {"error": response.text}
  File "D:\Projects\WhaleWatch\venv\lib\site-packages\httpx\_models.py", line 576, in text
    content = self.content
  File "D:\Projects\WhaleWatch\venv\lib\site-packages\httpx\_models.py", line 570, in content
    raise ResponseNotRead()
httpx.ResponseNotRead: Attempted to access streaming response content, without having called `read()`.
@nessshon
Copy link
Collaborator

@AveZorgen, hello!

I just ran the tests and didn't find any errors. Could you send me a list of ACCOUNTS? Tell me, how often does this error occur?

I have implemented the solution you found in version 0.2.4b1. Try installing it (pip install pytonapi==0.2.4b1) and see if the error occurs again. If this doesn't happen, I will implement the solution in the next update.

@AveZorgen
Copy link
Author

One of the accounts: UQD0g_SvxCFDL6siKlAbCGuunZY8R7L6imTN0mIaxIGWGVQf
Errors occurred with a frequency ranging from every half an hour to 5 hours.
After transitioning to version 0.2.4b1, this error has been resolved.
But now I'm getting a different error:

Traceback (most recent call last):
  File "/root/WhaleWatch/main.py", line 115, in main
    await tonapi.sse.subscribe_to_transactions(
  File "/root/WhaleWatch/venv/lib/python3.10/site-packages/pytonapi/async_tonapi/methods/sse.py", line 25, in subscribe_to_transactions
    async for data in self._subscribe(method=method, params=params):
  File "/root/WhaleWatch/venv/lib/python3.10/site-packages/pytonapi/async_tonapi/client.py", line 122, in _subscribe
    await self.__process_response(response)
  File "/root/WhaleWatch/venv/lib/python3.10/site-packages/pytonapi/async_tonapi/client.py", line 97, in __process_response
    error = content.get("error", content)
AttributeError: 'str' object has no attribute 'get'

I'm guessing it's because of these lines at async_tonapi/client.py:69

except httpx.ResponseNotRead:
    content = response.text

My python is 3.10.12 and freeze:

annotated-types==0.6.0
anyio==4.3.0
certifi==2024.2.2
exceptiongroup==1.2.1
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
idna==3.7
pydantic==2.7.0
pydantic_core==2.18.1
python-dotenv==1.0.1
python-telegram-bot==21.1.1
pytonapi==0.2.4b1
sniffio==1.3.1
typing_extensions==4.11.0
websockets==12.0

@nessshon
Copy link
Collaborator

@AveZorgen, thanks for the detailed tips!

Try installing it pytonapi==0.2.4b2 and see if the error occurs again..

@Asidert
Copy link

Asidert commented Apr 22, 2024

@nessshon Hello, I have similar exception:

Traceback (most recent call last):
  File "/root/TON_Taxi/ton/api/tonapi.py", line 28, in listen
    await self.api.sse.subscribe_to_transactions(accounts=[self.debug, self.team_address], handler=self.on_new_transaction, args=(self.api,))
  File "/root/TON_Taxi/venv/lib/python3.10/site-packages/pytonapi/async_tonapi/methods/sse.py", line 25, in subscribe_to_transactions
    async for data in self._subscribe(method=method, params=params):
  File "/root/TON_Taxi/venv/lib/python3.10/site-packages/pytonapi/async_tonapi/client.py", line 126, in _subscribe
    async for line in response.aiter_lines():
  File "/root/TON_Taxi/venv/lib/python3.10/site-packages/httpx/_models.py", line 963, in aiter_lines
    async for text in self.aiter_text():
  File "/root/TON_Taxi/venv/lib/python3.10/site-packages/httpx/_models.py", line 950, in aiter_text
    async for byte_content in self.aiter_bytes():
  File "/root/TON_Taxi/venv/lib/python3.10/site-packages/httpx/_models.py", line 929, in aiter_bytes
    async for raw_bytes in self.aiter_raw():
  File "/root/TON_Taxi/venv/lib/python3.10/site-packages/httpx/_models.py", line 987, in aiter_raw
    async for raw_stream_bytes in self.stream:
  File "/root/TON_Taxi/venv/lib/python3.10/site-packages/httpx/_client.py", line 149, in __aiter__
    async for chunk in self._stream:
  File "/root/TON_Taxi/venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 253, in __aiter__
    with map_httpcore_exceptions():
  File "/usr/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/root/TON_Taxi/venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 86, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ReadError

Now I’ll install the version you specified and check it. I'll let you know tomorrow morning, was there an exception again or is everything stable

@Asidert
Copy link

Asidert commented Apr 22, 2024

@nessshon still throws an exception. After about 1-2 hours of work.

image

@nessshon
Copy link
Collaborator

@Asidert, @AveZorgen, Have you encountered any more errors lately? If so, I'm considering implementing a strategy to ignore all 5xx errors so that the SSE method continues to function regardless. What are your thoughts on this approach?

@Asidert
Copy link

Asidert commented Apr 25, 2024

@nessshon There were errors, but now I catch them and try to reconnect

image

@nessshon nessshon closed this as not planned Won't fix, can't repro, duplicate, stale Jun 25, 2024
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

No branches or pull requests

3 participants