Skip to content

Update start/stop handling of credential_manager_shim#130

Open
arenekosreal wants to merge 1 commit intolinux-credentials:mainfrom
arenekosreal:async-python
Open

Update start/stop handling of credential_manager_shim#130
arenekosreal wants to merge 1 commit intolinux-credentials:mainfrom
arenekosreal:async-python

Conversation

@arenekosreal
Copy link

@arenekosreal arenekosreal commented Feb 6, 2026

  • Use asyncio.run or uvloop.run to start main loop
  • Handle SIGTERM to quit gracefully

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.

@kalvdans
Copy link

kalvdans commented Feb 6, 2026

Stick to one framework, if we need uvloop, change the README.md file to pip install it.

@arenekosreal
Copy link
Author

Actually uvloop is completely optional. If the script failed to import it, it will simply fallback to use the built-in asyncio. Anyway, I will update README to mention about this later.

@kalvdans
Copy link

kalvdans commented Feb 6, 2026

Could you also motivate in the PR description why we need to quit gracefully instead of just terminating abruptly. Thanks.

@iinuwa
Copy link
Member

iinuwa commented Feb 6, 2026

Hello! Thanks for your contribution!

I copied in the text from your commit message into the PR description for visibility.

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.

We only target Linux, so we don't need to worry about Windows. 👍

Comment on lines 7 to 13
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
Copy link
Member

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())
Copy link
Member

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.

webext/README.md Outdated
Comment on lines 41 to 48
Then, optionally, you can use [uvloop](https://github.com/MagicStack/uvloop)>=0.18 as the web extension's async event loop implementation.
This is not required, we prefer using uvloop, while can still fallback to use the built-in asyncio if failed to import it.

If you are sure to use it, simply install `uvloop` like installing `dbus-next` mentioned above.
We will print a log to let you know if we are using uvloop.

# Setup Instructions

Copy link
Member

Choose a reason for hiding this comment

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

Let's drop this.

@arenekosreal
Copy link
Author

@kalvdans At first I was creating a package on AUR, and found that this helper cannot start because asyncio.get_event_loop had raised a RuntimeError. During fixing this problem, I noticed that this helper always runs in a while-loop and never breaks, then I changed the code to let it break the loop when needed.

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
@arenekosreal
Copy link
Author

@iinuwa I have force-pushed changes, uvloop support is removed completely.

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.

3 participants