Skip to content

fix: graceful shutdown on SIGTERM, improve deploy script - #245

Merged
cnlangzi merged 1 commit into
mainfrom
fix/graceful-shutdown
May 4, 2026
Merged

fix: graceful shutdown on SIGTERM, improve deploy script#245
cnlangzi merged 1 commit into
mainfrom
fix/graceful-shutdown

Conversation

@xiajiexia

Copy link
Copy Markdown
Collaborator

Summary

  • Exit with code 0 on SIGTERM to prevent systemd auto-restart (systemd has Restart=on-failure)
  • Fix config filename in deploy script: config.yaml -> config.yml
  • Simplify deploy script stop logic: stop systemd first, immediately kill process, verify port release
  • Improve error message to point to log file

Test plan

  • make deploy works without stale process issues
  • systemctl stop dbkrab exits cleanly (check: echo $? -> 0)
  • Port conflict still triggers panic and non-zero exit (for debugging)

@coderabbitai

coderabbitai Bot commented May 4, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@xiajiexia has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 31 minutes and 41 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3158746a-fd34-4c18-bf00-8e0764782565

📥 Commits

Reviewing files that changed from the base of the PR and between 6c1397e and 4a64de7.

📒 Files selected for processing (4)
  • Makefile
  • cmd/app/main.go
  • scripts/dbkrab.service
  • scripts/deploy.sh

Walkthrough

The pull request reorganizes the deployment and shutdown workflows across the Makefile, Go application, and deployment script. The deploy target is simplified to execute the deployment script only, while a new install target handles systemd service configuration. The Go application's shutdown handler now captures and logs received signals explicitly. The deployment script introduces configurable config file paths (defaulting to config.yml), adds helper functions for process monitoring, and implements an enhanced shutdown sequence coordinating systemd service stops with graceful process termination and optional forced kills.

Poem

🐰 Hops of joy through deploy and install,
Signals logged, no silence at all!
Config paths dance where we please,
Graceful shutdown sets our processes free
Systemd and scripts now work as one,
The rabbit's deployment dance is done!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main changes: graceful shutdown on SIGTERM and deploy script improvements, matching the core objectives.
Description check ✅ Passed The description is directly related to the changeset, clearly outlining the key improvements: SIGTERM handling, config filename fix, deploy logic simplification, and log message improvements.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 0/1 reviews remaining, refill in 31 minutes and 41 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Adding os.Exit(0) inside a defer in main will force a zero exit code even on unexpected failures or panics; consider wiring a specific SIGTERM handler or propagating a status code to main’s return path instead so only the intended shutdown path returns 0.
  • The deploy script’s port check hardcodes :9021 in ss output; if the port becomes configurable or the listener binds differently (e.g., IPv6, different interface or format), this check may silently fail—consider reading the port from config or making the check more robust.
  • The deploy script now immediately pkill -9’s the process, which removes any chance for a graceful shutdown; you might want to send a regular SIGTERM first and only escalate to SIGKILL if the process fails to exit within the port-release retry loop.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Adding `os.Exit(0)` inside a `defer` in `main` will force a zero exit code even on unexpected failures or panics; consider wiring a specific SIGTERM handler or propagating a status code to `main`’s return path instead so only the intended shutdown path returns 0.
- The deploy script’s port check hardcodes `:9021` in `ss` output; if the port becomes configurable or the listener binds differently (e.g., IPv6, different interface or format), this check may silently fail—consider reading the port from config or making the check more robust.
- The deploy script now immediately `pkill -9`’s the process, which removes any chance for a graceful shutdown; you might want to send a regular SIGTERM first and only escalate to SIGKILL if the process fails to exit within the port-release retry loop.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1 issue found across 3 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="cmd/app/main.go">

<violation number="1" location="cmd/app/main.go:336">
P2: `os.Exit(0)` bypasses the deferred cleanup in `main`, so shutdown can skip DB closes and other flushes.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread cmd/app/main.go Outdated
fmt.Fprintf(os.Stderr, "Dashboard stop error: %v\n", err)
}
log.Println("shutdown complete")
os.Exit(0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: os.Exit(0) bypasses the deferred cleanup in main, so shutdown can skip DB closes and other flushes.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At cmd/app/main.go, line 336:

<comment>`os.Exit(0)` bypasses the deferred cleanup in `main`, so shutdown can skip DB closes and other flushes.</comment>

<file context>
@@ -333,6 +333,7 @@ func main() {
 			fmt.Fprintf(os.Stderr, "Dashboard stop error: %v\n", err)
 		}
 		log.Println("shutdown complete")
+		os.Exit(0)
 	}()
 
</file context>
Suggested change
os.Exit(0)
return

@xiajiexia
xiajiexia force-pushed the fix/graceful-shutdown branch 3 times, most recently from 0f569a7 to 6c1397e Compare May 4, 2026 13:45

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
Makefile (1)

111-118: 💤 Low value

Minor redundancy: install restarts service that deploy already started.

The deploy target (via deploy.sh) already starts the service with systemctl start dbkrab. Then install does systemctl restart dbkrab, which works but is redundant. Consider whether install should skip the restart if the goal is just to set up systemd, or if deploy.sh should skip starting the service when called from install.

This is not a bug since restart on an already-running service is safe, but it adds a few seconds of unnecessary downtime.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Makefile` around lines 111 - 118, The install Makefile target redundantly
restarts the service that deploy (via deploy.sh) already starts; update the
install target (or deploy.sh) so they don't both restart/start the service:
either remove or conditionalize the "systemctl restart dbkrab" in the install
target (referencing the install target and its "systemctl restart dbkrab" line)
or modify deploy.sh to skip starting when invoked from install (referencing
deploy.sh and its "systemctl start dbkrab" call); ensure the chosen change still
copies scripts/dbkrab.service and runs "systemctl daemon-reload" and "systemctl
enable dbkrab" so the unit is installed without forcing an extra restart.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@cmd/app/main.go`:
- Around line 328-329: Replace the plain stdlib log.Printf call used after
receiving a signal with a structured slog.Info call: when you read sig from
sigCh (sig := <-sigCh) call slog.Info("shutting down", "signal", sig) instead of
log.Printf, and ensure the package imports use log/slog so structured logging is
used everywhere; update any import list to include slog and remove the
log.Printf usage in the shutdown path.

In `@scripts/deploy.sh`:
- Line 115: The deploy.sh echo references /opt/dbkrab/logs/dbkrab.log which is
inaccessible under the service's ProtectSystem settings; update the deploy
message in deploy.sh to point to the allowed path /var/log/dbkrab/dbkrab.log (or
alternatively modify the systemd unit to add ReadWritePaths=/opt/dbkrab/logs/),
and ensure this matches the logging config in internal/config/config.go (which
currently resolves to ./logs/dbkrab.log) so logs are written/read from the same
permitted location.

---

Nitpick comments:
In `@Makefile`:
- Around line 111-118: The install Makefile target redundantly restarts the
service that deploy (via deploy.sh) already starts; update the install target
(or deploy.sh) so they don't both restart/start the service: either remove or
conditionalize the "systemctl restart dbkrab" in the install target (referencing
the install target and its "systemctl restart dbkrab" line) or modify deploy.sh
to skip starting when invoked from install (referencing deploy.sh and its
"systemctl start dbkrab" call); ensure the chosen change still copies
scripts/dbkrab.service and runs "systemctl daemon-reload" and "systemctl enable
dbkrab" so the unit is installed without forcing an extra restart.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 1a356b55-2cd8-4801-b9b7-c82565ad1617

📥 Commits

Reviewing files that changed from the base of the PR and between 2dd057c and 6c1397e.

📒 Files selected for processing (3)
  • Makefile
  • cmd/app/main.go
  • scripts/deploy.sh

Comment thread cmd/app/main.go
Comment on lines +328 to +329
sig := <-sigCh
log.Printf("Received %v, shutting down...", sig)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Use slog.Info instead of log.Printf for shutdown logging.

The signal capture is a good addition, but log.Printf violates the coding guidelines. Use structured logging with slog.

Proposed fix
-		sig := <-sigCh
-		log.Printf("Received %v, shutting down...", sig)
+		sig := <-sigCh
+		slog.Info("Received signal, shutting down...", "signal", sig)

As per coding guidelines: "Use log/slog for structured logging" and "Prohibit using log.Print and log.Printf for logging".

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
sig := <-sigCh
log.Printf("Received %v, shutting down...", sig)
sig := <-sigCh
slog.Info("Received signal, shutting down...", "signal", sig)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cmd/app/main.go` around lines 328 - 329, Replace the plain stdlib log.Printf
call used after receiving a signal with a structured slog.Info call: when you
read sig from sigCh (sig := <-sigCh) call slog.Info("shutting down", "signal",
sig) instead of log.Printf, and ensure the package imports use log/slog so
structured logging is used everywhere; update any import list to include slog
and remove the log.Printf usage in the shutdown path.

Comment thread scripts/deploy.sh
- Signal handler uses context cancellation for graceful shutdown
- Simplify systemd service: remove security restrictions causing issues
- Add HOME env var for config file discovery
- Simplify Makefile: stop/start/restart use systemd directly
- reset target uses make stop/start for consistency
- Fix log file creation with proper permissions
@xiajiexia
xiajiexia force-pushed the fix/graceful-shutdown branch from 6c1397e to 4a64de7 Compare May 4, 2026 14:14
@cnlangzi
cnlangzi merged commit 4c0385a into main May 4, 2026
4 checks passed
@cnlangzi
cnlangzi deleted the fix/graceful-shutdown branch May 4, 2026 14:52
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.

2 participants