question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Asyncio based websocket client leaking memory

See original GitHub issue

Hey,

I use the asyncio based websocket client on windows/python3.6 for messaging between my client and the server.

Whenever the connection is closed, I instruct my client to reconnect. When that happens, I get one extra thread and memory usage. Threads stop expanding after a while (I think it’s just windows not properly reporting the threads), but memory consumption goes way up.

To reproduce the problem is pretty easy: set up an asyncio based websocket client, and tell it to reconnect with loop.create_connection in its protocol onClose method.

It happens when I get the following reason in the onClose method: connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)

Please investigate. I have played around with the garbage collector and tracemalloc, it shows that the most memory consumption happens at protocol init where there’s self.is_closed = txaio.create_future(), and after that I gave up tracking the memory leak further.

What it seems to me, is that the protocol class itself is not freed after connection close, but I can’t really tell why. I don’t keep references to the protocol class.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
karolyicommented, Jun 12, 2019

@meejah If my opinion counts, I’d raise that exception at the code I pointed out. The same happens with normal sockets, you get an exception when you try to send over a closed socket.

1reaction
meejahcommented, Jun 11, 2019

On the WAMP side, we do raise TransportLost if you try to send a message on a WAMP session that is not “open” … https://github.com/crossbario/autobahn-python/blob/master/autobahn/wamp/websocket.py#L126

@oberstet what about changing pure WebSockets to do this (possibly “instead?” i.e. just move this to the lower layer). This would be an API change, really (i.e. “raise instead of just returning None”) not sure how to handle that properly 😕 or maybe since “it might raise an exception anyway” that’s fine?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do my asyncio network streams cause memory leaks on ...
I am currently trying to create an IRC client for personal uses. My problem is the handling of connection losses.
Read more >
Memory leak in tkinter/asyncio/websockets application - Reddit
I'm in the process of building a POS system for a local restaurant. The GUI is pretty much complete. Everything works according to...
Read more >
How to fix ws and socket.io memory leak? - Server Fault
1 Answer 1 · the ws node. · the zlib module has had a problem in the past relating to memory fragmentation, which...
Read more >
Issue 34745: asyncio ssl memory leak - Python tracker
If the master branch has the leak it might mean that the root cause is indeed in either the ssl module or openssl...
Read more >
python-websockets(1) - Arch manual pages
Built on top of asyncio, Python's standard asynchronous I/O framework, it provides an elegant coroutine-based API. Here's how a client sends and receives ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found