WebSocket-for-Python icon indicating copy to clipboard operation
WebSocket-for-Python copied to clipboard

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

Open mpf82 opened this issue 8 months ago • 6 comments

This fix tries to query the sockets directly for pending data if poller.poll() returns no file descriptors.

In case of pending data, a special flag is set on the websocket, to ensure the whole buffer is processed in one go.

Fixes https://github.com/Lawouach/WebSocket-for-Python/issues/270

mpf82 avatar May 07 '25 09:05 mpf82

can you please add unit tests to verify the changes?

Sure. I've already prepared an integration test in pytest for our system.

I will refactor the test to use unittest.TestCase.

mpf82 avatar May 07 '25 16:05 mpf82

thank you. will be looking forward to it

auvipy avatar May 12 '25 07:05 auvipy

You might want to replace my server.crt and server.key files.

They have been created on a clean Ubuntu container, so I don't mind if they are available on github, but still.

Btw, the server.crt and server.key files in the example folders seem to be too old and will raise a [SSL: EE_KEY_TOO_SMALL] when used in my environment.

mpf82 avatar May 13 '25 15:05 mpf82

Seems that tox is using pytest, not unittest.

Anyway, when change my local setup to run pytest, I do not get an error:

Running pytest with args: ['-p', 'vscode_pytest', '-vvv', '--rootdir=\mpf82\WebSocket-for-Python', '\mpf82\WebSocket-for-Python\test\test_cherrypy_ssl.py::CherryPySSLTest::test_ssl']

test/test_cherrypy_ssl.py::CherryPySSLTest::test_ssl PASSED [100%]

All I can see in the failing job is

test/test_cherrypy_ssl.py py311: exit 70 (1.05 seconds) /home/runner/work/WebSocket-for-Python/WebSocket-for-Python> pytest pid=1888 py311: FAIL code 70 (11.27=setup[10.22]+cmd[1.05] seconds) evaluation failed :( (11.38 seconds)

No idea, what FAIL code 70 is supposed to mean.

mpf82 avatar May 14 '25 14:05 mpf82

After some more testing, I can reproduce the error.

The issue is with running multiple CherryPy tests in parallel.

Moving the tests of test_cherrypy.py to the end of the test cases (via conftest.py) will result in all tests PASSing, but the pytest process will still fail.

Removing test_cherrypy.py completely (and keeping only the new test_cherrypy_ssl.py) fixes the issue, but that's obviously not an option.

I have currently no idea how to fix this.

I've looked into CherryPy's tests and it seems they are relying on some sort of helper magic:

https://github.com/cherrypy/cherrypy/tree/main/cherrypy/test

Help is appreciated.

mpf82 avatar May 14 '25 15:05 mpf82

Converted to draft, because I just cannot get the unittests to pass.

I have refactored the two CherryPy tests to use the unittest wrapper class, but no success.

I've tried to narrow down the issue and it seems that starting the websocket manager (Thread) causes the tests to fail once multiple CherryPy tests are run in the same (pytest) process space.

Not starting the manager will result in the tests passing (as long as they do not rely on the manager).

Refactoring the two CherryPy test modules into one also failed, because of all the mocking in the existing test.

I have no idea how to tell the manager thread to run in "CherryPy's unittest class wrapper process space" so I give up.

mpf82 avatar May 20 '25 07:05 mpf82