Skip to content

Commit

Permalink
Changed: More useful debug info upon a signal (eg ABRT)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnot committed Mar 3, 2024
1 parent 9ffca30 commit f86df3c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies = [
"markdown >= 3.4.4",
"MarkupSafe >= 2.1.3",
"netaddr >= 1.2.1",
"thor >= 0.9.6",
"thor >= 0.11.3",
"typing-extensions >= 4.8.0",
]

Expand Down
69 changes: 41 additions & 28 deletions redbot/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import httplint
import thor
from thor.loop import _loop
from thor.tcp import TcpConnection

import redbot
from redbot.type import RawHeaderListType
Expand All @@ -50,7 +51,7 @@ class RedBotServer:

def __init__(self, config: SectionProxy) -> None:
self.config = config
self.handler = partial(RedHandler, server=self)
self.handler = partial(RedRequestHandler, server=self)

# Set up the watchdog
if notify is not None:
Expand All @@ -68,11 +69,23 @@ def __init__(self, config: SectionProxy) -> None:
thor.schedule(10, self.gc_state)

# Set up the server
server = thor.http.HttpServer(
self.http_server = thor.http.HttpServer(
self.config.get("host", "").encode("utf-8"),
self.config.getint("port", fallback=8000),
)
server.on("exchange", self.handler)
self.http_server.on("exchange", self.handler)

# Install signal handlers
for signum in [
signal.SIGSEGV,
signal.SIGABRT,
signal.SIGFPE,
signal.SIGBUS,
signal.SIGILL,
]:
signal.signal(signum, self.handle_signal)

def run(self) -> None:
try:
thor.run()
except KeyboardInterrupt:
Expand All @@ -87,6 +100,28 @@ def gc_state(self) -> None:
clean_saved_tests(self.config)
thor.schedule(self.config.getint("gc_mins", fallback=2) * 60, self.gc_state)

def handle_signal(
self, sig: int, frame: Optional[FrameType] = None
) -> signal.Handlers:
self.console(f"*** {signal.strsignal(sig)}\n")
current_frame = inspect.currentframe()
assert current_frame
frame = current_frame.f_back
assert frame
traceback.print_stack(frame, limit=1)
self.console(" * Local Variables")
for key, val in frame.f_locals.items():
self.console(f" {key}: {val}")
handlers = self.http_server.loop.registered_fd_handlers()
self.console(" * TCP Connections")
for handler in handlers:
if isinstance(handler, TcpConnection):
self.console(f" {repr(handler)}")
self.console(" * Scheduled Events")
for when, event in self.http_server.loop.scheduled_events():
self.console(f" {when:.2f} - {repr(event)}")
sys.exit(1)

def walk_files(self, dir_name: str, uri_base: bytes = b"") -> Dict[bytes, bytes]:
out: Dict[bytes, bytes] = {}
for root, _, files in os.walk(dir_name):
Expand All @@ -108,7 +143,7 @@ def console(message: str) -> None:
sys.stderr.write(f"{message}\n")


class RedHandler:
class RedRequestHandler:
static_types = {
b".html": b"text/html",
b".js": b"text/javascript",
Expand Down Expand Up @@ -224,29 +259,6 @@ def bad_request(self, why: bytes = b"bad request") -> None:
self.exchange.response_done([])


# dump stack on faults
def handle_signal(sig: int, frame: Optional[FrameType] = None) -> signal.Handlers:
sys.stderr.write(f"*** {signal.strsignal(sig)}\n")
current_frame = inspect.currentframe()
assert current_frame
frame = current_frame.f_back
assert frame
traceback.print_stack(frame, limit=1)
for key, val in frame.f_locals.items():
sys.stderr.write(f" {key}: {val}\n")
sys.exit(1)


for signum in [
signal.SIGSEGV,
signal.SIGABRT,
signal.SIGFPE,
signal.SIGBUS,
signal.SIGILL,
]:
signal.signal(signum, handle_signal)


# debugging output
def print_debug(message: str, profile: Optional[cProfile.Profile]) -> None:
sys.stderr.write(f"WARNING: {message}\n\n")
Expand Down Expand Up @@ -291,7 +303,8 @@ def main() -> None:
+ f"http://{conf['redbot'].get('host', '')}:{conf['redbot']['port']}/\n"
)

RedBotServer(conf["redbot"])
server = RedBotServer(conf["redbot"])
server.run()


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion test/test_webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def redbot_run():
conf = ConfigParser()
conf.read("config.txt")
redconf = conf["redbot"]
redbot.daemon.RedBotServer(redconf)
server = redbot.daemon.RedBotServer(redconf)
server.run()


if __name__ == "__main__":
Expand Down

0 comments on commit f86df3c

Please sign in to comment.