Skip to content

Conversation

@Narita-1095305
Copy link
Contributor

Add Engine.Shutdown() method for graceful shutdown support

Description

This PR adds built-in graceful shutdown support to Gin Engine, allowing users to properly wait for active connections to finish before shutting down the server.

Changes

  • Add server and serverLock fields to Engine struct to store server reference
  • Add Shutdown(ctx context.Context) method that calls http.Server.Shutdown()
  • Modify Run(), RunTLS(), RunUnix(), RunListener() to store server reference
  • Add RunWithShutdown() convenience method with built-in signal handling
  • Add comprehensive tests for graceful shutdown

Usage

router := gin.Default()
router.GET("/", handler)

go router.Run(":8080")

// Wait for signal
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit

// Graceful shutdown
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
router.Shutdown(ctx)

Or use the convenience method:

router := gin.Default()
router.GET("/", handler)
router.RunWithShutdown(":8080", 10*time.Second) // Handles SIGINT/SIGTERM automatically

Related Issue

Addresses the need for built-in graceful shutdown support mentioned in documentation.

Checklist

  • I have read the contributing guidelines
  • I have added tests that prove my fix is effective or that my feature works
  • I have commented my code, particularly in hard-to-understand areas

@codecov
Copy link

codecov bot commented Jan 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.01%. Comparing base (3dc1cd6) to head (f894f4c).
⚠️ Report is 232 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4501      +/-   ##
==========================================
- Coverage   99.21%   99.01%   -0.21%     
==========================================
  Files          42       45       +3     
  Lines        3182     3035     -147     
==========================================
- Hits         3157     3005     -152     
- Misses         17       21       +4     
- Partials        8        9       +1     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 99.00% <100.00%> (?)
-tags go_json 98.93% <100.00%> (?)
-tags nomsgpack 98.99% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.24 99.01% <100.00%> (?)
go-1.25 99.01% <100.00%> (?)
macos-latest 99.01% <100.00%> (-0.21%) ⬇️
ubuntu-latest 99.01% <100.00%> (-0.21%) ⬇️

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.

@Narita-1095305 Narita-1095305 force-pushed the feat/graceful-shutdown branch 2 times, most recently from 67d083f to f61c6d0 Compare January 19, 2026 02:26
- Add server and serverLock fields to Engine struct
- Add Shutdown() method that calls http.Server.Shutdown()
- Modify Run/RunTLS/RunUnix/RunListener to store server reference
- Add RunWithShutdown convenience method with signal handling
- Add comprehensive tests for graceful shutdown (8 test cases)
- Fix lint errors (errorlint, testifylint, errcheck)
@Narita-1095305
Copy link
Contributor Author

Note on Codecov Project Coverage

The codecov/project check is failing due to a -0.21% decrease in overall project coverage (99.22% → 99.01%).

Verification

  • Patch coverage: 100% - All newly added code is fully covered by tests
  • Shutdown(): 100% coverage
  • RunWithShutdown(): 100% coverage
  • RunWithShutdownConfig(): 100% coverage

Cause of Coverage Drop

The slight decrease is not caused by the code in this PR. Local testing confirms:

github.com/gin-gonic/gin/gin.go:555:       Shutdown                  100.0%
github.com/gin-gonic/gin/graceful.go:31:   RunWithShutdown           100.0%
github.com/gin-gonic/gin/graceful.go:40:   RunWithShutdownConfig     100.0%

The project-wide coverage drop is related to existing packages (e.g., codec/json: 0%, ginS: 73.1%) rather than this PR's changes.

I believe this PR should be acceptable for review since all modified and added code has 100% test coverage.

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