]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: bug fix: utility function to spawn subprocesses did not unblock signals...
authorVasek Sraier <git@vakabus.cz>
Sat, 18 Jun 2022 21:16:05 +0000 (23:16 +0200)
committerVaclav Sraier <vaclav.sraier@nic.cz>
Fri, 24 Jun 2022 13:22:07 +0000 (13:22 +0000)
manager/knot_resolver_manager/utils/async_utils.py

index 5fc36169dc6c95ea5d1e666c78a7e87ca36b77d3..1cd7303e2c3c61a23ac96fd55768acc084896384 100644 (file)
@@ -1,6 +1,8 @@
 import asyncio
 import os
 import pkgutil
+import signal
+import sys
 import time
 from asyncio import create_subprocess_exec, create_subprocess_shell
 from pathlib import PurePath
@@ -10,13 +12,32 @@ from typing import Any, Dict, Generic, List, Optional, TypeVar, Union
 from knot_resolver_manager.compat.asyncio import to_thread
 
 
+def unblock_signals():
+    if sys.version_info.major >= 3 and sys.version_info.minor >= 8:
+        signal.pthread_sigmask(signal.SIG_UNBLOCK, signal.valid_signals())  # type: ignore
+    else:
+        # the list of signals is not exhaustive, but it should cover all signals we might ever want to block
+        signal.pthread_sigmask(
+            signal.SIG_UNBLOCK,
+            {
+                signal.SIGHUP,
+                signal.SIGINT,
+                signal.SIGTERM,
+                signal.SIGUSR1,
+                signal.SIGUSR2,
+            },
+        )
+
+
 async def call(
     cmd: Union[str, bytes, List[str], List[bytes]], shell: bool = False, discard_output: bool = False
 ) -> int:
     """
     custom async alternative to subprocess.call()
     """
-    kwargs: Dict[str, Any] = {}
+    kwargs: Dict[str, Any] = {
+        "preexec_fn": unblock_signals,
+    }
     if discard_output:
         kwargs["stdout"] = asyncio.subprocess.DEVNULL
         kwargs["stderr"] = asyncio.subprocess.DEVNULL