Skip to content

Tardigrade: handle SIGTERM/SIGINT so OnStop runs on shutdown - #29

Merged
Al-Muhandis merged 1 commit into
risoflora:mainfrom
Al-Muhandis:tardigrade-signal-handling
Oct 27, 2026
Merged

Al-Muhandis merged 1 commit into
risoflora:mainfrom
Al-Muhandis:tardigrade-signal-handling

Conversation

@Al-Muhandis

Copy link
Copy Markdown
Contributor

Summary

The Tardigrade broker installs handlers for SIGTERM/SIGINT that wake up the main loop (Terminate(0)), so a systemctl stop (or Ctrl+C) now shuts the application down gracefully instead of being killed on the spot by the default signal disposition.

Problem

TApplication.DoRun waits on a condition variable forever, and nothing ever calls Terminate. On SIGTERM the process dies immediately: the unit finalizations never run, and TBrookHTTPServer.OnStop (reachable via Free -> Destroy -> SetActive(False) -> DoClose) never fires. As a result, daemons built on the broker have no way to flush state or release resources at shutdown — the callback silently never happens.

Changes

  • BrookTardigradeBroker: on UNIX, Run registers a one-shot handler for SIGTERM and SIGINT.
  • The handler only calls Application.Terminate(0) (sets the terminated flag and signals the condition variable); everything else — finalizations, OnStop — runs in normal execution context on the way out.

Testing

Running in production as part of a Telegram moderation daemon (FPC 3.2.2, Linux x86_64, systemd):

  • systemctl stop now exits with status 0 and fires OnStop (verified by the daemon saving its training base during shutdown);
  • repeated signals are ignored after the first one;
  • non-UNIX builds are unaffected (the change is {$IFDEF UNIX}-guarded).

Thank you for the great library!

The default disposition killed the process on the spot: the unit
finalizations never ran and TBrookHTTPServer.OnStop (Free -> Destroy ->
SetActive(False) -> DoClose) never fired. The signal handler only
wakes the main loop via Terminate(0), the rest happens in normal
context. Local patch pending an upstream PR.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-27T07:05:20.997196Z
We couldn’t complete this review. Comment @codex review to try again.
f6af2cb PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Al-Muhandis
Al-Muhandis merged commit ce1eec3 into risoflora:main Oct 27, 2026
1 check passed
@Al-Muhandis
Al-Muhandis deleted the tardigrade-signal-handling branch October 27, 2026 07:16
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.

1 participant