Skip to content

Conversation

@Dugowitch
Copy link
Contributor

@Dugowitch Dugowitch commented Jan 27, 2026

Secure Coding Practices Checklist GitHub Link

Secure Coding Checklist

  • Input Validation
  • Output Encoding
  • Authentication and Password Management
  • Session Management
  • Access Control
  • Cryptographic Practices
  • Error Handling and Logging
  • Data Protection
  • Communication Security
  • System Configuration
  • Database Security
  • File Management
  • Memory Management
  • General Coding Practices

Summary by Sourcery

Improve graceful shutdown behavior and observability for the HTTP server and signal handling.

Enhancements:

  • Add a configurable grace-period delay before cancelling the application context on SIGINT/SIGTERM to better align with Kubernetes shutdown expectations.
  • Log a debug message when the HTTP server begins graceful shutdown to improve shutdown traceability.

Tests:

  • Update server shutdown tests to assert the additional debug log and adjusted log ordering during graceful shutdown.

@jira-linking
Copy link

jira-linking bot commented Jan 27, 2026

Referenced Jiras:
https://issues.redhat.com/browse/RHINENG-21760

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 27, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds additional observability around application shutdown by logging received termination signals and graceful HTTP server shutdown, and updates tests accordingly.

Sequence diagram for enhanced shutdown logging

sequenceDiagram
    participant OS as OS
    participant HandleSignals as HandleSignals
    participant Context as CancelContext
    participant Server as RunServer
    OS->>HandleSignals: SIGINT or SIGTERM
    HandleSignals->>HandleSignals: LogInfo starting grace period
    HandleSignals->>HandleSignals: Sleep defaultK8sGracePeriod/4
    HandleSignals->>Context: Cancel()
    Context->>Server: ctx.Done()
    Server->>Server: LogDebug gracefully shutting down server
    Server->>Server: Shutdown()
Loading

File-Level Changes

Change Details Files
Improve signal handling observability and introduce a short grace period before context cancellation.
  • Wrap SIGINT/SIGTERM handling in a goroutine that captures the received signal value.
  • Log an informational message indicating the start of a grace period and include the specific signal name.
  • Sleep for a fraction of the default Kubernetes termination grace period before cancelling the application context.
  • Log completion of signal handling after context cancellation.
base/base.go
Enhance HTTP server shutdown logging when the application context is cancelled.
  • In the server goroutine, log a debug message just before invoking http.Server.Shutdown when the context is done.
  • Keep existing error logging for shutdown failures unchanged.
base/utils/gin.go
Adjust server tests to assert the new graceful shutdown logging behavior.
  • Increase the expected number of captured log entries in the RunServer test from one to two.
  • Update assertions to validate the order and content of the new graceful shutdown debug log followed by the existing successful close log.
  • Tidy imports to satisfy gofmt/goimports ordering.
base/utils/gin_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

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:

  • In HandleSignals, consider logging the actual signal value (e.g., using the value read from the channel) instead of a generic SIGTERM/SIGINT message to make it clearer which signal triggered shutdown.
  • In RunServer, it may be useful to include the server address/port in the "gracefully shutting down server" log message to aid debugging when multiple servers are running.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `HandleSignals`, consider logging the actual signal value (e.g., using the value read from the channel) instead of a generic `SIGTERM/SIGINT` message to make it clearer which signal triggered shutdown.
- In `RunServer`, it may be useful to include the server address/port in the "gracefully shutting down server" log message to aid debugging when multiple servers are running.

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.

@codecov-commenter
Copy link

codecov-commenter commented Jan 28, 2026

Codecov Report

❌ Patch coverage is 25.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.27%. Comparing base (e9178c5) to head (fb2b112).
⚠️ Report is 22 commits behind head on master.

Files with missing lines Patch % Lines
base/base.go 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2030      +/-   ##
==========================================
+ Coverage   59.18%   59.27%   +0.08%     
==========================================
  Files         133      134       +1     
  Lines        8599     8618      +19     
==========================================
+ Hits         5089     5108      +19     
  Misses       2967     2967              
  Partials      543      543              
Flag Coverage Δ
unittests 59.27% <25.00%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Dugowitch Dugowitch marked this pull request as draft January 28, 2026 10:57
@Dugowitch Dugowitch force-pushed the RHINENG-21760 branch 2 times, most recently from 3539e2c to 40cb335 Compare January 28, 2026 16:55
Add a short delay, a portion of the grace period between SIGTERM and
SIGKILL, before context cancellation to allow the in-flight requests to
finish and thus prevent gRPC CANCELED errors from Kessel middleware.
@Dugowitch Dugowitch marked this pull request as ready for review January 28, 2026 17:04
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • The fixed defaultK8sGracePeriod and unconditional time.Sleep(defaultK8sGracePeriod / 4) in HandleSignals make shutdown latency rigid and potentially misaligned with actual Kubernetes terminationGracePeriodSeconds; consider making this duration configurable or derived from environment/config instead of hard-coded.
  • Using time.Sleep in the signal-handling goroutine makes the pre-cancel delay non-interruptible (e.g., on a second signal); consider using a time.Timer or context with timeout so you can still react promptly to additional signals or early-exit conditions during the grace window.
  • For the new signal and shutdown messages, consider using structured logging (fields for signal type and grace-period duration) rather than string concatenation to make log filtering and analysis easier in production.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The fixed `defaultK8sGracePeriod` and unconditional `time.Sleep(defaultK8sGracePeriod / 4)` in `HandleSignals` make shutdown latency rigid and potentially misaligned with actual Kubernetes `terminationGracePeriodSeconds`; consider making this duration configurable or derived from environment/config instead of hard-coded.
- Using `time.Sleep` in the signal-handling goroutine makes the pre-cancel delay non-interruptible (e.g., on a second signal); consider using a `time.Timer` or context with timeout so you can still react promptly to additional signals or early-exit conditions during the grace window.
- For the new signal and shutdown messages, consider using structured logging (fields for signal type and grace-period duration) rather than string concatenation to make log filtering and analysis easier in production.

## Individual Comments

### Comment 1
<location> `base/base.go:30-32` </location>
<code_context>
 	signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
 	go func() {
-		<-c
+		sig := <-c
+		utils.LogInfo("starting grace period for " + sig.String())
+		time.Sleep(defaultK8sGracePeriod / 4)
 		CancelContext()
 		utils.LogInfo("SIGTERM/SIGINT handled")
</code_context>

<issue_to_address>
**issue (bug_risk):** Blocking sleep in signal handler delays context cancellation and may reduce actual shutdown time window.

Sleeping in the signal-handling goroutine means shutdown doesn’t start until `defaultK8sGracePeriod/4` has elapsed, reducing the time available for cleanup and delaying all logic tied to this context. Instead, consider calling `CancelContext()` immediately and moving any delay to a separate goroutine that only governs process exit, or make the sleep duration configurable (including zero) so it can be tuned to the actual grace period of the deployment environment.
</issue_to_address>

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.

@Dugowitch
Copy link
Contributor Author

/retest

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