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

anyio.BusyResourceError: Another task is already writing to this resource #29

Open
ZzZombo opened this issue Apr 3, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@ZzZombo
Copy link

ZzZombo commented Apr 3, 2023

  File "~~~~.py", line 80, in resolve_url
    await self.ws.send_json(data)
  File "~~~~\Lib\site-packages\httpx_ws\_api.py", line 701, in send_json
    await self.send_text(serialized_data)
  File "~~~~\Lib\site-packages\httpx_ws\_api.py", line 659, in send_text
    await self.send(event)
  File "~~~~\Lib\site-packages\httpx_ws\_api.py", line 638, in send
    await self.stream.write(data)
  File "~~~~\Lib\site-packages\httpcore\backends\asyncio.py", line 51, in write
    await self._stream.send(item=buffer)
  File "~~~~\Lib\site-packages\anyio\_backends\_asyncio.py", line 1291, in send
    with self._send_guard:
  File "~~~~\Lib\site-packages\anyio\_core\_synchronization.py", line 584, in __enter__
    raise BusyResourceError(self.action)
anyio.BusyResourceError: Another task is already writing to this resource

I can't quite trace the conditions for the above error, because it only happens occasionally.

@ZzZombo ZzZombo added the bug Something isn't working label Apr 3, 2023
@frankie567
Copy link
Owner

Quite hard to tell what's going wrong from this 🤔 Do you try to send several messages concurrently in the same WebSocket?

@ZzZombo
Copy link
Author

ZzZombo commented Apr 3, 2023

From the output it doesn't seem so. It may appear once in a thousand calls within several hours or several times within a minute. All captured instances had at least ~10ms between calls though. Ironically my admittedly quite crude attempts at fixing it seemed only to severely increase the rate of the error occurring. But question: isn't WS supposed to handle simultaneous messages just fine?

T-256 added a commit to T-256/httpx-ws that referenced this issue Jul 28, 2023
@Emsu
Copy link

Emsu commented Dec 21, 2023

@frankie567 I get this too with audiostreams through websockets in the latest version still.

It looks like it's the background ping writing to the socket at the same time in my case. Sometimes the background ping throws the error and sometime's it's the audio stream data depending on who wrote first and which coroutine got the error.

Is there some way we can make the background ping not cause an error in the userland code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants