Why Does My Spring Boot App Crash While Netty Still Runs? A Deep Dive
During a gray‑release a single server reported that the Spring container was closed while the Netty server still accepted HTTP requests, leading to an IllegalStateException; the article analyzes the root cause in Dubbo startup, explains the event flow, and proposes a reliable fix using ApplicationReadyEvent.
Introduction
During a gray‑release a single machine reported an error: the Spring container was already closed while the Netty server could still receive HTTP requests, resulting in a java.lang.IllegalStateException.
Root Cause Analysis
The failure originates from Dubbo’s startup. When the Dubbo service is initialized during the Spring refresh() phase, the Netty port is already occupied, causing DefaultApplicationDeployer to throw an exception. The exception propagates to SpringApplication.run(), which shuts down the Spring context. However, the Netty server, which listens to ContextRefreshedEvent, has already started and remains alive, leading to the “ApplicationContext has been closed” error when it processes the gray‑release request.
Solution
Replace the ContextRefreshedEvent listener with an ApplicationReadyEvent listener so that Netty starts only after the Spring context is fully ready. Optionally also listen for ApplicationFailedEvent or ExitCodeEvent to shut down Netty if the application fails to start.
Best Practices
Understand the startup order of Spring, Dubbo, and Netty.
Use gray‑release testing and monitor logs for port‑binding errors.
Keep dependency versions compatible and manage them with Maven/Gradle.
Apply the open‑closed principle and add comprehensive logging.
Conclusion
By aligning Netty’s lifecycle with Spring’s ApplicationReadyEvent, the application avoids the “container closed” exception and remains stable during deployments.
Sohu Smart Platform Tech Team
The Sohu News app's technical sharing hub, offering deep tech analyses, the latest industry news, and fun developer anecdotes. Follow us to discover the team's daily joys.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
