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

changed send_json and its dependent functions to be non-blocking. #15

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

killown
Copy link
Contributor

@killown killown commented Sep 5, 2024

No description provided.

@killown
Copy link
Contributor Author

killown commented Sep 5, 2024

now send_json will timeout with incomplete data, fix #14

@killown killown requested a review from ammen99 September 5, 2024 03:50
wayfire/ipc.py Outdated
return response
else:
# The connection is closed due to a timeout
self.connect_client(self.socket_name)
Copy link
Member

Choose a reason for hiding this comment

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

this reconnect here seems very weird to me, we're raising the exception, the user should decide what to do

Copy link
Contributor Author

@killown killown Sep 5, 2024

Choose a reason for hiding this comment

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

We have two options:

The worse option, which will lead to issues later:

Exception("Response timeout, client connection closed")

In this case, we would remove the line for connecting the client, which is not ideal. Every time a timeout occurs, the client-side code would need to handle socket = WayfireSocket() again.
The issue here is the excessive checking to see if the client is still connected, all due to a send_json timeout.
It's not part of the user side to keep the client connected after socket = WayfireSocket()

The better option:

Set self.reconnect_on_client_timeout = True in init

Exception("Response timeout, reconnecting client")
if self.reconnect_on_client_timeout:
    self.connect_client(self.socket_name)

This way, the user can set socket.reconnect_on_client_timeout = False if they don't want the client to automatically reconnect.

it's not strange btw, the message 'Response timeout' indicates that the request didn't happen

@killown
Copy link
Contributor Author

killown commented Sep 5, 2024

This will reproduce the issue which happens randomly, a quick way to get the error is by using fuzzy tests while this code watch for events...

import asyncio
from wayfire import WayfireSocket
import sys

sock = WayfireSocket()
sock.watch()

async def events_one():
    while True:
        try:
            event = sock.read_next_event()
            print(f"event: {event}")
        except Exception as e:
            print(f"Failed to get the event: {e}")
            sys.exit(0)
        await asyncio.sleep(0.04)  # Add delay to prevent overwhelming the system

async def events_two():
    while True:
        try:
            event = sock.read_next_event()
            print(f"event: {event}")
        except Exception as e:
            print(f"Failed to get the event: {e}")
            sys.exit(0)
        await asyncio.sleep(0.04)  # Add delay to prevent overwhelming the system

async def events_three():
    while True:
        try:
            event = sock.read_next_event()
            print(f"event: {event}")
        except Exception as e:
            print(f"Failed to get the event: {e}")
            sys.exit(0)
        await asyncio.sleep(0.04)  # Add delay to prevent overwhelming the system

async def events_four():
    while True:
        try:
            event = sock.read_next_event()
            print(f"event: {event}")
        except Exception as e:
            print(f"Failed to get the event: {e}")
            sys.exit(0)
        await asyncio.sleep(0.04)  # Add delay to prevent overwhelming the system



async def main():
    await asyncio.gather(
        events_one(),
        events_two(),
        events_three(),
        events_four()
    )

asyncio.run(main())****

this will return randomly:

80, 'width': 1920, 'x': 0, 'y': 0}, 'workspace': {'grid_height': 3, 'grid_width': 3, 'x': 1, 'y': 0}, 'wset-index': 2}}
Failed to get the event: JSON decoding error: unexpected character: line 1 column 1 (char 0)
Task exception was never retrieved
future: <Task finished name='Task-1' coro=<main() done, defined at /home/neo/test-broken-pywayfire.py:52> exception=SystemExit(0)>
Traceback (most recent call last):
  File "/home/neo/.local/lib/python3.12/site-packages/wayfire/ipc.py", line 56, in read_message
    response = js.loads(response_message)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
orjson.JSONDecodeError: unexpected character: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/neo/test-broken-pywayfire.py", line 23, in events_two
    event = sock.read_next_event()
            ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/neo/.local/lib/python3.12/site-packages/wayfire/ipc.py", line 115, in read_next_event
    return self.read_message()
           ^^^^^^^^^^^^^^^^^^^
  File "/home/neo/.local/lib/python3.12/site-packages/wayfire/ipc.py", line 58, in read_message
    raise Exception(f"JSON decoding error: {e}")
Exception: JSON decoding error: unexpected character: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/neo/test-broken-pywayfire.py", line 60, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.12/asyncio/runners.py", line 193, in run
    with Runner(debug=debug, loop_factory=loop_factory) as runner:
  File "/usr/lib/python3.12/asyncio/runners.py", line 62, in __exit__
    self.close()
  File "/usr/lib/python3.12/asyncio/runners.py", line 70, in close
    _cancel_all_tasks(loop)
  File "/usr/lib/python3.12/asyncio/runners.py", line 205, in _cancel_all_tasks
    loop.run_until_complete(tasks.gather(*to_cancel, return_exceptions=True))
  File "/usr/lib/python3.12/asyncio/base_events.py", line 674, in run_until_complete
    self.run_forever()
  File "/usr/lib/python3.12/asyncio/base_events.py", line 641, in run_forever
    self._run_once()
  File "/usr/lib/python3.12/asyncio/base_events.py", line 1986, in _run_once
    handle._run()
  File "/usr/lib/python3.12/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/home/neo/test-broken-pywayfire.py", line 53, in main
    await asyncio.gather(
  File "/usr/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 674, in run_until_complete
    self.run_forever()
  File "/usr/lib/python3.12/asyncio/base_events.py", line 641, in run_forever
    self._run_once()
  File "/usr/lib/python3.12/asyncio/base_events.py", line 1986, in _run_once
    handle._run()
  File "/usr/lib/python3.12/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/home/neo/test-broken-pywayfire.py", line 27, in events_two
    sys.exit(0)
SystemExit: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants