fix(server): replace select{} in Serve() with graceful lifecycle management#3321
fix(server): replace select{} in Serve() with graceful lifecycle management#3321cvictory wants to merge 1 commit into
Conversation
…gement
Replace the hard-blocking select{} in Server.Serve() with a stopCh
channel, enabling external callers to trigger graceful shutdown via
the new Server.Stop() method.
Changes:
- Add stopCh field to Server struct for lifecycle signaling
- NewServer now initializes stopCh
- Serve() blocks on <-stopCh instead of select{}
- Add Stop() method: safe, idempotent, allows Serve() restart
- Serve() re-creates stopCh if previously closed, supporting restart
- Add serve_stop_test.go with 4 lifecycle tests
Fixes #3042
|
There was a problem hiding this comment.
server/server.go:379-386 only flips serve=false and closes stopCh. But Serve() has already exported services at server/server.go:355-358 and registered the service instance at server/server.go:361; none of those resources are unexported/deregistered/destroyed. Existing teardown lives in paths like ServiceOptions.Unexport() (server/action.go:310), but Stop() never calls it. Result: Serve() returns, yet listeners and registry exposure can remain alive, so this is not a graceful shutdown and “Serve can be called again after shutdown” is misleading. The new tests miss this because they use an empty NewServer() with no exported service. Please add a real exported Triple/Dubbo service test: call Serve(), verify it accepts traffic, call Stop(), then verify the port/request is closed or fails and registry/protocol cleanup happened.



Bug
Server.Serve()usesselect {}to block forever, making it impossible to shut down gracefully from code or respond to lifecycle signals.select {}) has no termination path.Fix
stopCh chan struct{}toServerfor lifecycle signaling.NewServerinitializesstopCh.Serve()blocks on<-stopChinstead ofselect{}.Stop()method: safe, idempotent, and allowsServe()to be called again after shutdown.Serve()re-createsstopChif previously closed, supporting restart.serve_stop_test.gowith 4 lifecycle tests (all pass).Verification
TestStopBeforeServe,TestStopIdempotent,TestServeStopGracefulShutdown,TestServeAfterStopall pass.Fixes #3042