-
-
Notifications
You must be signed in to change notification settings - Fork 100
improve reconnect and trigger boot notification #341
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a finally:
before line 821?
I think you also need to set |
yes, something like that is needed. But with return_exceptions is True gather will wait until all awaitables have been completed, which is not what we want. If any awaitable raises an exception, all should be cancelled. Once the exception is raised, gather completes and cancelling gather will not cancel the subtasks. There's two possible solutions:
try:
....
except Exception:
self.gather.cancel() I prefer option 2. |
My understanding is if return_exceptions is True then cancelling gather will cancel the other tasks, but I haven't confirmed with a piece of test code. The difference being you have to test the result for an Exception. https://hynek.me/articles/waiting-in-asyncio/ |
Ignore this, you are right it will only work for finite tasks as the result from gather is only returned after all tasks have completed successfully or thrown an exception (which may happen but there is no guarantee given we are running two indefinite loops). I agree option 2 looks cleaner. |
Another option worth considering is removing the closing from the monitor_connection function, and simply propagate the error back to the calling try block (start and reconnect), after which the connection will close in the finally statement. The logging and closing can then be handled there eg In monitor_connection:
In start/reconnect add:
The |
got something that seems to work now, please review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm getting the following error:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/websockets/legacy/server.py", line 293, in handler
await self.ws_handler(self, path)
File "/config/custom_components/ocpp/api.py", line 203, in on_connect
f"Charger {cp_id} disconnected from {self.host}:{self.port} websocket={websocket.id}."
AttributeError: 'WebSocketServerProtocol' object has no attribute 'id'
Strange, works for me. What is your websockets version? |
i'll release as beta now, so others can check if it works |
That's odd somehow HA reverted to websockets 9.1 although I have been on 10.1 for sometime... I had to go into bash and upgrade to get 10.1 back again. This may be the reason:
|
after adding trigger_boot_notification to post_connect, we could get into an infinite loop because the boot notification handler would create a new post_connect. This makes sense in case of a spontaneous reboot, but not if the boot notification was triggered. So we check for this, and only only re-do post connect if the boot notification was not triggered.