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

Add params and type hints to stop() #6075 #6087

Conversation

shalearkane
Copy link
Contributor

@shalearkane shalearkane commented Feb 16, 2023

Fixes issue #6075

Overview

def stop():
"""
Graceful exit.
"""
graceful_stop.set()
should accept parameters signalnum and handler before being used as as signal handler in
def main(args):
signal.signal(signal.SIGTERM, stop)
try:
run(once=args.run_once, threads=args.threads, sleep_time=args.sleep_time, bulk=args.bulk)
except KeyboardInterrupt:
stop()
according to the python docs. However the same function is being called without parameters in Line 28 So I added *args to the function parameters to resolve the issue.

Alternative Solution

The other possible solution is to add the parameters:

def stop(signalnum, handler):

However, then for the function call in Line 28,

I will have to pass those arguments to stop().

Copy link
Contributor

@dchristidis dchristidis left a comment

Choose a reason for hiding this comment

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

I think your choosing to use variadic arguments is reasonable.

This change should be applied to all daemons and not just the Conveyor Preparer. In addition, I think it’s a good opportunity to add some type hints, given that they are trivial.

@shalearkane
Copy link
Contributor Author

On looking into other stop functions, I saw that they had parameters signum and frame which defaulted to None. So I changed the variadic argument to the same and added type hints.

@shalearkane shalearkane force-pushed the patch-6075-fix_TypeError_for_stop___in_daemons_conveyer_preparer branch from 806d676 to 534bdcf Compare February 26, 2023 17:41
@shalearkane
Copy link
Contributor Author

I have changed the arguments of the stop functions of other daemons to *args. I added type-hint None to the return type. Should I add type hints to other functions as well?

@dchristidis
Copy link
Contributor

I was surprised to see that so many stop() functions have some parameters already. I’m now not as confident about the best approach and would welcome someone else to comment. They way I see it, there are two alternatives:

  1. def stop(*args: Any) -> None: It’s simple and, at least in my eyes, somewhat suggests that the arguments are ignored.
  2. def stop(signum: Optional[int] = None, frame: Optional[FrameType] = None) -> None: More complex, but communicates well that this function is a signal handler.

@bari12
Copy link
Member

bari12 commented Mar 3, 2023

I prefer the second;

Copy link
Contributor

@dchristidis dchristidis left a comment

Choose a reason for hiding this comment

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

Be sure to import Optional and FrameType. Frequently we do it like so:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from types import FrameType
    from typing import Optional

And then place the type hints inside strings.

Please note that the commits should be squashed together. Finally, the commit message has some room for improvement.

@shalearkane shalearkane force-pushed the patch-6075-fix_TypeError_for_stop___in_daemons_conveyer_preparer branch from 13c5e47 to 863e73a Compare March 11, 2023 20:43
@shalearkane
Copy link
Contributor Author

I have imported the Types and I have fixed imports in several files and squashed the commit. Please review the commit message. Should I make it more descriptive?

Copy link
Contributor

@dchristidis dchristidis left a comment

Choose a reason for hiding this comment

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

And then place the type hints inside strings.

You missed this part. Type hints are evaluated and will lead to NameError exceptions if the necessary classes are not imported at runtime.

As for the commit message:

  1. It should follow the format <component>: <change_message> #<issue number>. You did well, but since you are touching multiple components, you should omit the first part (as opposed to prefixing the message with ‘bug’).
  2. The rest of the message can be misleading. ‘add default arguments’ suggests that the parameters already exist, which is not the case. As such, ‘add parameters’ is more appropriate.
  3. The body of the message is empty. You should use it to document why you are making this change. Explain how this is the proper signature for signal handlers and how deviating from it can lead to a TypeError.

@shalearkane shalearkane force-pushed the patch-6075-fix_TypeError_for_stop___in_daemons_conveyer_preparer branch from a230a92 to be6e646 Compare March 22, 2023 02:23
@shalearkane shalearkane requested review from dchristidis and removed request for mlassnig, bari12, rcarpa and cserf March 22, 2023 02:25
@shalearkane shalearkane force-pushed the patch-6075-fix_TypeError_for_stop___in_daemons_conveyer_preparer branch 3 times, most recently from c2baa16 to 38f24e0 Compare March 22, 2023 18:00
@shalearkane
Copy link
Contributor Author

I have updated my patch against the latest master branch and amended my commit message accordingly. Please review my changes 😄

Copy link
Contributor

@dchristidis dchristidis left a comment

Choose a reason for hiding this comment

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

Please review the failed tests. It would appear that there are some missing imports.

For example:
lib/rucio/daemons/conveyor/preparer.py:42:49: F821 undefined name 'FrameType'

Similarly, in some places you moved the import statement under a conditional block (i.e. if TYPE_CHECKING) for things that are already in use.

Finally, you’ll need to rebase your branch on the latest master.

@shalearkane shalearkane force-pushed the patch-6075-fix_TypeError_for_stop___in_daemons_conveyer_preparer branch 2 times, most recently from 1adac48 to 716717a Compare April 22, 2023 06:55
Copy link
Contributor

@dchristidis dchristidis left a comment

Choose a reason for hiding this comment

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

This is likely to be my last request for changes. Please adjust some of the imports as noted in the comments below.

In addition, I would like to ask you to adjust the commit message. Please ensure that there is a blank line between the subject and the rest of the message, then move ‘Fixes #6075’ to the end of the subject.

lib/rucio/daemons/atropos/atropos.py Outdated Show resolved Hide resolved
lib/rucio/daemons/c3po/c3po.py Outdated Show resolved Hide resolved
lib/rucio/daemons/hermes/hermes.py Outdated Show resolved Hide resolved
@shalearkane shalearkane force-pushed the patch-6075-fix_TypeError_for_stop___in_daemons_conveyer_preparer branch from 716717a to 6b367d7 Compare May 29, 2023 09:22
@shalearkane shalearkane force-pushed the patch-6075-fix_TypeError_for_stop___in_daemons_conveyer_preparer branch from 6b367d7 to fbc8bdb Compare June 27, 2023 18:27
The stop() functions used by daemons to gracefully stop had missing
parameters `signum` and `frame`. These parameters are passed
when the daemon receives SIGTERM signal. Since we have no use
for the parameter as of now, we have set them to `None` by default.
Also type-hints are added to the parameters and to the return type.

Signed-off-by: Soumik Dutta <shalearkane@gmail.com>
@shalearkane shalearkane force-pushed the patch-6075-fix_TypeError_for_stop___in_daemons_conveyer_preparer branch from fbc8bdb to 8ef7361 Compare June 27, 2023 18:33
@shalearkane
Copy link
Contributor Author

Sir, could you please review the changes? I have made all the requested changes and have rebased the code against master branch.

@dchristidis dchristidis changed the title bug: Fix TypeError for stop() in daemons.conveyer.preparer #6075 Add params and type hints to stop() #6075 Jun 30, 2023
@bari12 bari12 merged commit eec6dbc into rucio:master Jul 3, 2023
61 of 68 checks passed
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.

None yet

3 participants