Skip to content

Fix messages are one-behind when using secure sockets (wss) #298

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ws4py/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,22 @@ def stop(self):
self.websockets.clear()
self.poller.release()

def get_fds_with_pending_data( self ):
"""
Returns a list of file descriptors that have pending data.

This is a workaround for a bug where polling returns no file descriptor,
even though we have sockets with pending data.
"""
fds = []
with self.lock:
for ws in ( ws for ws in self.websockets.values() if ws._is_secure ):
if hasattr( ws.sock, 'pending' ) and ws.sock.pending():
fds.append( ws.sock.fileno() )
# set special handling flag
ws._force_process_buffer = True
return fds

def run(self):
"""
Manager's mainloop executed from within a thread.
Expand All @@ -309,6 +325,10 @@ def run(self):
if not self.running:
break

if not polled:
# workaround WSS bug
polled.extend( self.get_fds_with_pending_data() )

for fd in polled:
if not self.running:
break
Expand Down
16 changes: 15 additions & 1 deletion ws4py/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ def __init__(self, sock, protocols=None, extensions=None, environ=None, heartbea
Tell us if the socket is secure or not.
"""

self._force_process_buffer = False
"""
Indicates special handling of the buffer, only relevant for secure sockets.
"""

self.client_terminated = False
"""
Indicates if the client has been marked as terminated.
Expand Down Expand Up @@ -241,7 +246,7 @@ def close_connection(self):
except:
pass
self.sock = None


def ping(self, message):
"""
Expand Down Expand Up @@ -424,6 +429,15 @@ def once(self):
if not self.process(self.buf[:requested]):
return False
self.buf = self.buf[requested:]
if self.buf and self._force_process_buffer:
Copy link
Preview

Copilot AI May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The while loop for processing the buffer under special handling mode duplicates some logic found earlier in the method. Consider refactoring this loop into a separate helper method to improve readability and maintainability.

Copilot uses AI. Check for mistakes.

self._force_process_buffer = False
# if we are in special handling mode, we need to process
# the buffer until it is empty
while self.buf:
requested = self.reading_buffer_size
if not self.process(self.buf[:requested]):
return False
self.buf = self.buf[requested:]

return True

Expand Down
Loading