Skip to content

SpringApplication.exit(...) not closing application due to MongoDB locks for cluster with multiple nodes #4711

Closed
@infoShare

Description

@infoShare

When using simple application with spring boot web app and spring-data-mongodb connected to MongoDB cluster with multiple nodes SpringApplication.exit(...) is usually not closing application due to locks (for example - GC execution is releasing lock).

Sample application:

@SpringBootApplication
public class SpringDataMongodbSSL {

    public static void main(String[] args) {
        SpringApplication.run(SpringDataMongodbSSL.class, args);
    }


    @EventListener(ApplicationReadyEvent.class)
    public void close(ApplicationReadyEvent event) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("Closing context");
                SpringApplication.exit(event.getApplicationContext(), () -> 0);
                System.out.println("Context closed");
            }
        }, 15 * 1000);
    }
}

Configuration on application.yml contain only mongo uri and server.shutdown=graceful to print shutdown on the console (behavior the same without this property).

Execution logs:

Discovered replica set primary DB_URL:27017 with max election id 7fffffff0000000000000026 and max set
Closing context
Commencing graceful shutdown. Waiting for active requests to complete
Graceful shutdown complete
Context closed

And then application is still running.

Profiling results:
image

Repository with example: Repository
Spring Boot: 3.3.0
Java: 21

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      SpringApplication.exit(...) not closing application due to MongoDB locks for cluster with multiple nodes · Issue #4711 · spring-projects/spring-data-mongodb