-
Notifications
You must be signed in to change notification settings - Fork 7
Update start/stop handling of credential_manager_shim #130
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
base: main
Are you sure you want to change the base?
Conversation
|
Stick to one framework, if we need uvloop, change the README.md file to pip install it. |
|
Actually |
|
Could you also motivate in the PR description why we need to quit gracefully instead of just terminating abruptly. Thanks. |
|
Hello! Thanks for your contribution! I copied in the text from your commit message into the PR description for visibility.
We only target Linux, so we don't need to worry about Windows. 👍 |
| try: | ||
| uvloop = True | ||
| from uvloop import run as asyncio_run | ||
| except ImportError: | ||
| uvloop = False | ||
| from asyncio import run as asyncio_run | ||
| from asyncio import Event |
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.
This is not a hot code path, so let's drop the uvloop implementation.
| signal(SIGTERM, lambda _, __ : quit.set()) | ||
| if uvloop: | ||
| logging.debug("using uvloop as async event loop") | ||
| asyncio_run(main()) |
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.
please import asyncio, and use asyncio.run to refer to this method.
|
@kalvdans At first I was creating a package on AUR, and found that this helper cannot start because |
Using asyncio.run is preferred by Python. `asyncio.get_event_loop` raises a `RuntimeError` on my machine, which means that there is no event loop set. This is a change in 3.14, seems that we need to use `asyncio.set_event_loop` before running. So I use `asyncio.run` directly to avoid this. Handle `SIGTERM` signal so we can quit gracefully. Firefox will send `SIGTERM` on *nix systems, and use Windows's way to kill the native app. Not using Windows so not sure if `SIGTERM` will also be raised on Windows, but I do not find a clean way to notify killing. See also: https://docs.python.org/3.14/library/asyncio-eventloop.html#asyncio.get_event_loop https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging#closing_the_native_app
5fbb82a to
f77047f
Compare
|
@iinuwa I have force-pushed changes, uvloop support is removed completely. |
Are you saying that asyncio is swallowing KeyboardInterrupt? Reading online I see that asyncio turns KeyboardInterrupt into CancelledError. And the base class for CancelledError was changed from Exception to BaseException in 3.8 so it shouldn't be caught by |
No, just the literal meaning, I noticed a never-ending while-loop, and then I made it breakable. |
But a KeyboardInterrupt will break that infinite loop. I tested the following code on Python 3.12: import asyncio
while True:
loop = asyncio.new_event_loop()
loop.run_until_complete(asyncio.sleep(100))and it breaks fine with CTRL-C, despite the inifinte loop: My wish is that we should look closer into why this mechanism is broken in credential_manager_shim instead of painting over the problem. |
All unhandled exceptions risen in the loop can break the loop and force the program exit abnormally. You will get a traceback, just like the output in your comment. If the program is exited with an unhandled exception, that does not mean Besides, Sending a My commit is going to handle this correctly: |
Thanks, you are right I mixed up SIGTERM and SIGINT. But the question remains, why is the exit status inportant to Firefox? If it sends SIGTERM to kill the program, I don't think it care if the exit code is 0 or 130 or died from signal. I believe it is important for reviewers and code readers what other side-effects of a graceful exit we want, besides the exit code. |
It is because Mozilla's documentation says firefox will send SIGTERM to its extensions' native app part when the extension is closing. We need to ensure the script can exit normally when meeting SIGTERM. Or we will get SIGKILL from firefox.
I agree. But we have a chance to quit program gracefully instead wait for being killed now, I think we'd better try our best to avoid it being killed. This is just a coding style. If you want to know other side-effects, I must say I have not researched about it. I just patched the code, packaged the extension, then loaded it in firefox. And the extension seems working. Considered that this script is just exchanging data between dbus and firefox, and those functions are all still not changed, I think there may no other side-effect? |
Thanks! Do you have a link to the documentation to add in the source code?
I disagree, if we are not aware of any needed cleanup side-effects, adding extra code is unnecessary burden at this moment. I'm a fan of crash-only software :) |
Not in source code, but in commit message.
Wow, you are the first one I know that like this style. It seems that our conversation is just about different code style. Let maintainer decide if there is any change needed. |
asyncio.runoruvloop.runto start main loopSIGTERMto quit gracefullyUsing asyncio.run is preferred by Python.
asyncio.get_event_loopraises aRuntimeErroron my machine, which means that there is no event loop set.This is a change in 3.14, seems that we need to use
asyncio.set_event_loopbefore running. So I useasyncio.rundirectly to avoid this.Handle
SIGTERMsignal so we can quit gracefully.Firefox will send
SIGTERMon *nix systems, and use Windows's way to kill the native app. Not using Windows so not sure ifSIGTERMwill also be raised on Windows, but I do not find a clean way to notify killing.