question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Embedded MongoDB fails to shutdown gracefully in Spring Boot webapp

See original GitHub issue

Originally reported as spring-projects/spring-boot#5487.

The problem can be reproduced using Spring Boot’s spring-boot-sample-data-mongodb app, changing it to a web application using the following patch:

diff --git spring-boot-samples/spring-boot-sample-data-mongodb/pom.xml spring-boot-samples/spring-boot-sample-data-mongodb/pom.xml
index 1ff97bb..ff92880 100644
--- spring-boot-samples/spring-boot-sample-data-mongodb/pom.xml
+++ spring-boot-samples/spring-boot-sample-data-mongodb/pom.xml
@@ -21,7 +21,7 @@
        <dependencies>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
-                       <artifactId>spring-boot-starter</artifactId>
+                       <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
                <dependency>
                        <groupId>org.springframework.boot</groupId>

Shutting down the app generates the following output:

2016-03-24 20:17:32.379  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.378+0100 [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends
2016-03-24 20:17:32.379  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.378+0100 [signalProcessingThread] now exiting
2016-03-24 20:17:32.379  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.378+0100 [signalProcessingThread] dbexit: 
2016-03-24 20:17:32.380  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.378+0100 [signalProcessingThread] shutdown: going to close listening sockets...
2016-03-24 20:17:32.380  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.378+0100 [signalProcessingThread] closing listening socket: 5
2016-03-24 20:17:32.380  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.378+0100 [signalProcessingThread] closing listening socket: 7
2016-03-24 20:17:32.380  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.378+0100 [signalProcessingThread] removing socket file: /tmp/mongodb-41282.sock
2016-03-24 20:17:32.380  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.378+0100 [signalProcessingThread] shutdown: going to flush diaglog...
2016-03-24 20:17:32.380  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.378+0100 [signalProcessingThread] shutdown: going to close sockets...
2016-03-24 20:17:32.380  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.378+0100 [signalProcessingThread] shutdown: waiting for fs preallocator...
2016-03-24 20:17:32.381  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.378+0100 [signalProcessingThread] shutdown: closing all files...
2016-03-24 20:17:32.381  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.379+0100 [signalProcessingThread] closeAllFiles() finished
2016-03-24 20:17:32.381  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.379+0100 [signalProcessingThread] shutdown: removing fs lock...
2016-03-24 20:17:32.381  INFO 8202 --- [       Thread-3] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2016-03-24T20:17:32.379+0100 [signalProcessingThread] dbexit: really exiting now
2016-03-24 20:17:32.383  INFO 8202 --- [       Thread-7] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@446cdf90: startup date [Thu Mar 24 20:17:25 CET 2016]; root of context hierarchy
2016-03-24 20:17:32.385  INFO 8202 --- [       Thread-7] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2016-03-24 20:17:32.393  WARN 8202 --- [       Thread-1] d.flapdoodle.embed.mongo.runtime.Mongod  : sendShutdown /127.0.0.1:41282

java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_74]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_74]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_74]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_74]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_74]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_74]
    at de.flapdoodle.embed.mongo.runtime.Mongod.sendShutdown(Mongod.java:86) ~[de.flapdoodle.embed.mongo-1.50.2.jar!/:na]
    at de.flapdoodle.embed.mongo.AbstractMongoProcess.sendStopToMongoInstance(AbstractMongoProcess.java:133) [de.flapdoodle.embed.mongo-1.50.2.jar!/:na]
    at de.flapdoodle.embed.mongo.AbstractMongoProcess.stopInternal(AbstractMongoProcess.java:107) [de.flapdoodle.embed.mongo-1.50.2.jar!/:na]
    at de.flapdoodle.embed.process.runtime.AbstractProcess.stop(AbstractProcess.java:170) [de.flapdoodle.embed.process-1.50.1.jar!/:na]
    at de.flapdoodle.embed.process.runtime.Executable.stop(Executable.java:73) [de.flapdoodle.embed.process-1.50.1.jar!/:na]
    at de.flapdoodle.embed.process.runtime.Executable$JobKiller.run(Executable.java:90) [de.flapdoodle.embed.process-1.50.1.jar!/:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_74]

2016-03-24 20:17:32.494  WARN 8202 --- [       Thread-1] d.f.embed.mongo.AbstractMongoProcess     : could not stop mongod with db command, try next
2016-03-24 20:17:32.506  INFO 8202 --- [       Thread-1] d.f.e.process.runtime.ProcessControl     : execSuccess: false [kill, -2, 8226]
2016-03-24 20:17:32.506  WARN 8202 --- [       Thread-1] d.f.embed.mongo.AbstractMongoProcess     : could not stop mongod, try next
2016-03-24 20:17:32.507  WARN 8202 --- [       Thread-1] d.f.embed.mongo.AbstractMongoProcess     : could not stop mongod the second time, try one last thing

This happens regardless of the way the app is run - I’ve tried Intellij IDEA, mvn spring-boot:run and java -jar. However using SIGINT manages to shut it down correctly, as explained in the original issue.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
vpaviccommented, Feb 27, 2017

Hi @Loki-Afro, we did some refactoring of the samples in the meanwhile, the current command to run sample app that uses embedded Mongo is ./gradlew :spring-session-samples-boot-mongo:bootRun.

That being said, I cannot reproduce the original problem with either of our active branches (master and 1.4.x) so I’ll close this issue. Thanks.

0reactions
Loki-Afrocommented, Feb 26, 2017

@vpavic what is the current gradle command that will produce the error?

zarathustra@wotan ~/code/spring-session (git)-[master] % ./gradlew :samples:mongo:bootRun                                
FAILURE: Build failed with an exception.

* What went wrong:
Project 'samples' not found in root project 'spring-session-build'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 1.082 secs
Read more comments on GitHub >

github_iconTop Results From Across the Web

Embedded MongoDB with Spring not working - Stack Overflow
I solved the problem by installing mongo on the VM and then running the Spring boot instance. For me the embedded mongo driver ......
Read more >
Web - Spring
If you have not yet developed a Spring Boot web application, you can follow ... Graceful shutdown is supported with all four embedded...
Read more >
Shutdown a Spring Boot Application - Baeldung
By default, all the endpoints are enabled in Spring Boot Application except /shutdown; this is, naturally, part of the Actuator endpoints.
Read more >
[Solved]-Should the Spring Boot shutdown endpoint shut ...
You have something that prevents the JVM to exit besides your Spring Boot application. If you don't and you have a sample projet...
Read more >
Jetty : The Definitive Reference - Eclipse
If you are using Spring-Boot you must set generateOrigin to true. ... To perform a graceful shutdown of Jetty, the stats module must...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found