Skip to content

Add a try: except: around loop.add_signal_handler()#1011

Closed
ajkavanagh wants to merge 2 commits intojuju:masterfrom
ajkavanagh:add-guard-to-signal-handling
Closed

Add a try: except: around loop.add_signal_handler()#1011
ajkavanagh wants to merge 2 commits intojuju:masterfrom
ajkavanagh:add-guard-to-signal-handling

Conversation

@ajkavanagh
Copy link
Copy Markdown

For py 3.8, 3.9 and 3.10, the set_wakeup_fd() call fails on a background thread, due to [1], which has been fixed in py3.11. This patch simply catches the error if the python version can't do the adding of a signal handler so that the existing/previous behaviour continues.

[1] python/cpython#87173

Closes-Bug: #1010

Description

Closes-Bug: #1010

QA Steps

<Commands / tests / steps to run to verify that the change works:>

tox -e py3 -- tests/unit/...
tox -e integration -- tests/integration/...

All CI tests need to pass.

For py 3.8, 3.9 and 3.10, the set_wakeup_fd() call fails on a background
thread, due to [1], which has been fixed in py3.11.  This patch simply
catches the error if the python version can't do the adding of a signal
handler so that the existing/previous behaviour continues.

[1] python/cpython#87173

Closes-Bug: #1010
@cderici
Copy link
Copy Markdown
Contributor

cderici commented Jan 23, 2024

Hey @ajkavanagh, yeah I can see why this can be a problem off the main thread.

In general, I like this solution (except maybe just catch the RuntimeError (instead of (ValueError, OSError, RuntimeError)) and match the entire message instead of doing if 'main thread' not in str(e):, so that we'd catch all the problems other than this particular issue.

However, this makes me wonder whether pylibjuju installing signal handlers is correct to begin with. So, pylibjuju has two notions built-in to make it useful in general; a library, and a client (I'd like to eventually split these two in the future). Adding signal handlers makes sense from a client perspective, however, it doesn't makes sense at all from a library perspective.

I'm beginning to think that it's a mistake for pylibjuju to enforce certain way of handling a signal (for projects using pylibjuju as a library). So it makes slightly more sense to remove the code that adds (and removes) the signal handlers in a connection. That'll solve your problem as well. WDYT?

@ajkavanagh
Copy link
Copy Markdown
Author

In general, I like this solution (except maybe just catch the RuntimeError (instead of (ValueError, OSError, RuntimeError)) and match the entire message instead of doing if 'main thread' not in str(e):, so that we'd catch all the problems other than this particular issue.

Yes, that's a reasonable point; I was essentially just copying the existing model from juju/jasyncio.py to try to keep things similar.

However, this makes me wonder whether pylibjuju installing signal handlers is correct to begin with. So, pylibjuju has two notions built-in to make it useful in general; a library, and a client (I'd like to eventually split these two in the future). Adding signal handlers makes sense from a client perspective, however, it doesn't makes sense at all from a library perspective.

Well that was my first thought too. Libs generally shouldn't, I guess, be handling signals, as it ought to be something that the client-of-the-library ought to do.

I'm beginning to think that it's a mistake for pylibjuju to enforce certain way of handling a signal (for projects using pylibjuju as a library). So it makes slightly more sense to remove the code that adds (and removes) the signal handlers in a connection. That'll solve your problem as well. WDYT?

Yes, I agree with you. It would probably be good to give an example in the lib docs on how to catch signals so that the client can clean up properly as well, but I'd agree that the onus should be on the user-of-the-library to be responsible for cleaning up the code.

As such, please fee free to reject/close this change if you'd prefer a different approach. Thanks!

jujubot added a commit that referenced this pull request Jan 29, 2024
#1014

#### Description

In #1010 it's reported that running pylibjuju in an off-main thread blows up the `add_signal_handler` (for a good reason). Also per the discussion in the #1011 about signal handlers, this PR removes the code that installs signal handlers to the event loop at the connection creation. See [my comment](#1011 (comment)) in #1011 for details about reasoning.

Fixes #1010
@ajkavanagh
Copy link
Copy Markdown
Author

Based on #1014 merging, this is no longer needed. Thanks for the fix!

@ajkavanagh ajkavanagh closed this Jan 30, 2024
@ajkavanagh ajkavanagh deleted the add-guard-to-signal-handling branch January 30, 2024 11:02
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