Skip to content

Commit 5aafbc2

Browse files
committed
Refine engine counter logic
Update counter logic to prevent negative values. Since the stop method can now be called more than once, it was possible for the counter to move into negative values. See gh-8227
1 parent 7fda9c1 commit 5aafbc2

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,34 @@ private void initialize() throws EmbeddedServletContainerException {
9090
synchronized (this.monitor) {
9191
try {
9292
addInstanceIdToEngineName();
93+
try {
94+
// Remove service connectors to that protocol binding doesn't happen
95+
// yet
96+
removeServiceConnectors();
9397

94-
// Remove service connectors to that protocol binding doesn't happen yet
95-
removeServiceConnectors();
98+
// Start the server to trigger initialization listeners
99+
this.tomcat.start();
96100

97-
// Start the server to trigger initialization listeners
98-
this.tomcat.start();
101+
// We can re-throw failure exception directly in the main thread
102+
rethrowDeferredStartupExceptions();
99103

100-
// We can re-throw failure exception directly in the main thread
101-
rethrowDeferredStartupExceptions();
104+
Context context = findContext();
105+
try {
106+
ContextBindings.bindClassLoader(context, getNamingToken(context),
107+
getClass().getClassLoader());
108+
}
109+
catch (NamingException ex) {
110+
// Naming is not enabled. Continue
111+
}
102112

103-
Context context = findContext();
104-
try {
105-
ContextBindings.bindClassLoader(context, getNamingToken(context),
106-
getClass().getClassLoader());
113+
// Unlike Jetty, all Tomcat threads are daemon threads. We create a
114+
// blocking non-daemon to stop immediate shutdown
115+
startDaemonAwaitThread();
107116
}
108-
catch (NamingException ex) {
109-
// Naming is not enabled. Continue
117+
catch (Exception ex) {
118+
containerCounter.decrementAndGet();
119+
throw ex;
110120
}
111-
112-
// Unlike Jetty, all Tomcat threads are daemon threads. We create a
113-
// blocking non-daemon to stop immediate shutdown
114-
startDaemonAwaitThread();
115121
}
116122
catch (Exception ex) {
117123
throw new EmbeddedServletContainerException(
@@ -279,6 +285,7 @@ Map<Service, Connector[]> getServiceConnectors() {
279285
@Override
280286
public void stop() throws EmbeddedServletContainerException {
281287
synchronized (this.monitor) {
288+
boolean wasStarted = this.started;
282289
try {
283290
this.started = false;
284291
try {
@@ -294,7 +301,9 @@ public void stop() throws EmbeddedServletContainerException {
294301
"Unable to stop embedded Tomcat", ex);
295302
}
296303
finally {
297-
containerCounter.decrementAndGet();
304+
if (wasStarted) {
305+
containerCounter.decrementAndGet();
306+
}
298307
}
299308
}
300309
}

0 commit comments

Comments
 (0)