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

del Client.instances[self.id] Key error #2915

Closed
CVxTz opened this issue Apr 18, 2024 · 4 comments
Closed

del Client.instances[self.id] Key error #2915

CVxTz opened this issue Apr 18, 2024 · 4 comments
Labels
bug Something isn't working help wanted Extra attention is needed
Milestone

Comments

@CVxTz
Copy link
Contributor

CVxTz commented Apr 18, 2024

Description

Hello !

I had this error:

 Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/nicegui/background_tasks.py", line 52, in _handle_task_result
    task.result()
  File "/usr/local/lib/python3.10/site-packages/nicegui/client.py", line 259, in handle_disconnect
    self.delete()
  File "/usr/local/lib/python3.10/site-packages/nicegui/client.py", line 318, in delete
    del Client.instances[self.id]
KeyError: '51d921eb-0045-4f76-9b2e-ec8e3021e24c' 

This was also mentioned in #1826

@falkoschindler mentioned the absence of environ as the reason why prune_clients would not delete the same clients as handle_disconnect. But clients that connected can still get environ as None (for whatever reason) as in the socker-io library we have this function def:

    def get_environ(self, sid, namespace=None):
        """Return the WSGI environ dictionary for a client.

        :param sid: The session of the client.
        :param namespace: The Socket.IO namespace. If this argument is omitted
                          the default namespace is used.
        """
        eio_sid = self.manager.eio_sid_from_sid(sid, namespace or '/')
        return self.environ.get(eio_sid)

Which can return None.

I see multiple options:

  • On client connect, verify that environ does not end up being None anyway:
    async def _on_handshake(sid: str, data: Dict[str, str]) -> bool:
       client = Client.instances.get(data['client_id'])
       if not client:
           return False
       client.environ = sio.get_environ(sid)  # Add some check or default non null value here
       client.tab_id = data['tab_id']
       await sio.enter_room(sid, client.id)
       client.handle_handshake()
       return True
  • On .delete(), verify that the id exists and was not already deleted by prune_clients
  • Understand why sio.get_environ might return None and fix it.

I can't really reproduce the issue so I am not sure of what I am saying but I think its worth looking into this.

@falkoschindler
Copy link
Contributor

Thanks for the new input on this matter, @CVxTz!

The fact that sio.get_environ might return None could indeed cause some false assumption, even though I'm still struggling to get my head around the exact scenario where this could happen. Maybe there was a problem creating the socket connection, or it has been closed already. Adding an early return False for this case might be a possible solution. But first I'd like to find some kind of reproduction in order to verify the solution.

@falkoschindler falkoschindler added bug Something isn't working help wanted Extra attention is needed labels Apr 23, 2024
@rodja
Copy link
Member

rodja commented Apr 24, 2024

I wonder if we could get rid of relying on sio.get_environ so much:

While digging through the code I found a possible scenario where client.environ is None: if a disconnect happens before the handshake could be completed. Does anyone has an idea on how to provoke this for verification?

@ZAn-next
Copy link

ZAn-next commented May 1, 2024

I have got the same error when I use storage.client. I am not sure if there is any memory leakage as well.
Just be curious: Do we consider prioritizing merging the change to v1.x, instead of v2.0?

@falkoschindler
Copy link
Contributor

@ZAn-next We just had the idea of replacing environ with tab_id for checking if a client has_socket_connection: 200dea9. This might have solved this issue. Oh and GitHub already closed it because of my commit message. Let's see if the problem is gone and re-open it otherwise.

@falkoschindler falkoschindler added this to the 1.4.24 milestone May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants