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

fix: correct typing on signal handler #664

Merged
merged 2 commits into from
Nov 13, 2024
Merged

Conversation

igor47
Copy link
Contributor

@igor47 igor47 commented Nov 12, 2024

the signum received in the signal handler is an int, not a Signals object. this leads to errors like so:

[2024-11-12 16:49:05,288] [PID 151] [MainThread] [dramatiq.ForkProcess(0)] [INFO] Stopping fork process...
    res = self._popen.wait(timeout)
  File "/usr/local/lib/python3.10/multiprocessing/popen_fork.py", line 43, in wait
    return self.poll(os.WNOHANG if timeout == 0.0 else 0)
  File "/usr/local/lib/python3.10/multiprocessing/popen_fork.py", line 27, in poll
    pid, sts = os.waitpid(self.pid, flag)
  File "/usr/local/lib/python3.10/site-packages/dramatiq/cli.py", line 579, in sighandler
    logger.info("Sending signal %r to subprocesses...", getattr(signum, "name", signum))
  File "/usr/local/lib/python3.10/logging/__init__.py", line 1477, in info
    self._log(INFO, msg, args, **kwargs)
  File "/usr/local/lib/python3.10/logging/__init__.py", line 1624, in _log
    self.handle(record)
  File "/usr/local/lib/python3.10/logging/__init__.py", line 1634, in handle
    self.callHandlers(record)
  File "/usr/local/lib/python3.10/logging/__init__.py", line 1696, in callHandlers
    hdlr.handle(record)
  File "/usr/local/lib/python3.10/logging/__init__.py", line 966, in handle
    self.acquire()
  File "/usr/local/lib/python3.10/logging/__init__.py", line 917, in acquire
    self.lock.acquire()
  File "/usr/local/lib/python3.10/site-packages/dramatiq/cli.py", line 580, in sighandler
    stop_subprocesses(signum)
  File "/usr/local/lib/python3.10/site-packages/dramatiq/cli.py", line 571, in stop_subprocesses
    logger.warning("Failed to send %r to PID %d.", signum.name, proc.pid)
AttributeError: 'int' object has no attribute 'name'

here, we correctly type the signal handler functions, and also convert the int to a Signals for consistency

dramatiq/cli.py Outdated
except OSError: # pragma: no cover
if proc.exitcode is None:
logger.warning("Failed to send %r to PID %d.", signum.name, proc.pid)
logger.warning(f"Failed to send {signum.name} to PID {proc.pid}.")
Copy link
Owner

Choose a reason for hiding this comment

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

I would prefer to keep the format strings as they were since f-strings format eagerly, but loggers format lazily.

Copy link
Contributor

Choose a reason for hiding this comment

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

The linter should reject that anyway with W1203.

dramatiq/cli.py Outdated
reload_process = signum == getattr(signal, "SIGHUP", None)
if signum == signal.SIGINT:
signum = signal.SIGTERM

logger.info("Sending signal %r to subprocesses...", getattr(signum, "name", signum))
logger.info(f"Sending signal {signum.name} to subprocesses...")
Copy link
Owner

Choose a reason for hiding this comment

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

Ditto here.

the `signum` received in the signal handler is an `int`, not a `Signals`
object. this leads to errors like so:

```
[2024-11-12 16:49:05,288] [PID 151] [MainThread] [dramatiq.ForkProcess(0)] [INFO] Stopping fork process...
    res = self._popen.wait(timeout)
  File "/usr/local/lib/python3.10/multiprocessing/popen_fork.py", line 43, in wait
    return self.poll(os.WNOHANG if timeout == 0.0 else 0)
  File "/usr/local/lib/python3.10/multiprocessing/popen_fork.py", line 27, in poll
    pid, sts = os.waitpid(self.pid, flag)
  File "/usr/local/lib/python3.10/site-packages/dramatiq/cli.py", line 579, in sighandler
    logger.info("Sending signal %r to subprocesses...", getattr(signum, "name", signum))
  File "/usr/local/lib/python3.10/logging/__init__.py", line 1477, in info
    self._log(INFO, msg, args, **kwargs)
  File "/usr/local/lib/python3.10/logging/__init__.py", line 1624, in _log
    self.handle(record)
  File "/usr/local/lib/python3.10/logging/__init__.py", line 1634, in handle
    self.callHandlers(record)
  File "/usr/local/lib/python3.10/logging/__init__.py", line 1696, in callHandlers
    hdlr.handle(record)
  File "/usr/local/lib/python3.10/logging/__init__.py", line 966, in handle
    self.acquire()
  File "/usr/local/lib/python3.10/logging/__init__.py", line 917, in acquire
    self.lock.acquire()
  File "/usr/local/lib/python3.10/site-packages/dramatiq/cli.py", line 580, in sighandler
    stop_subprocesses(signum)
  File "/usr/local/lib/python3.10/site-packages/dramatiq/cli.py", line 571, in stop_subprocesses
    logger.warning("Failed to send %r to PID %d.", signum.name, proc.pid)
AttributeError: 'int' object has no attribute 'name'
```

here, we correctly type the signal handler functions, and also convert
the `int` to a `Signals` for consistency
@igor47
Copy link
Contributor Author

igor47 commented Nov 13, 2024

addressed

@igor47 igor47 requested a review from Bogdanp November 13, 2024 06:36
@Bogdanp Bogdanp merged commit 3825347 into Bogdanp:master Nov 13, 2024
11 checks passed
@Bogdanp
Copy link
Owner

Bogdanp commented Nov 13, 2024

Forgot to say thanks. So, thanks!

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