Skip to content

MongoReplicaSetTestResource fails to shutdown on Windows #40113

Open

Description

Describe the bug

I'm using MongoReplicaSetTestResource. When executing a build in Windows, it hangs while shutting down Quarkus after the tests and after a while i can see a stacktrace exactly like the one pasted here: flapdoodle-oss/de.flapdoodle.embed.mongo#373

I think this happens, because Flapdoodle constantly writes errors to the error-stream while shutdown is in progress. It then tries to close the error-stream, which not works because there are still messages to write.

The problem does not occour in our CI-pipeline, which is not Linux-based. When executing tests in Intellij, the shutdown takes very long, but in most of the cases, it works after a while.

I managed to develop a workaround by creating an additional test-resource which first re-configures the replica set and drops all connections to the second node:

public class MongoDBShutdownWorkaroundResource implements QuarkusTestResourceLifecycleManager {

    private static final String ADMIN_DB = "admin";

    private Integer port;
    private String replicaSet;

    @Override
    public void init(Map<String, String> initArgs) {
        port = MongoTestResource.port(initArgs);
        replicaSet = MongoReplicaSetTestResource.setReplicaSet(initArgs);
    }

    @Override
    public Map<String, String> start() {
        return null;
    }

    @Override
    public void stop() {
        try (MongoClient client = MongoClients.create("mongodb://localhost:" + port)) {
            Document replicaSetSetting = buildShutdownReplicaSetConfig();
            client.getDatabase(ADMIN_DB).runCommand(
                    new Document("replSetReconfig", replicaSetSetting)
                            .append("force", true));

            client.getDatabase(ADMIN_DB).runCommand(
                    new Document("dropConnections", "1")
                            .append("hostAndPort", List.of("localhost:" + (port + 1))));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private Document buildShutdownReplicaSetConfig() {
        Document rsConfig = new Document();
        rsConfig.append("_id", replicaSet);
        List<Document> members = new ArrayList<>();
        members.add(new Document().append("_id", 0).append("host", "localhost:" + port));
        rsConfig.append("members", members);
        rsConfig.append("version", 99);
        return rsConfig;
    }
}

Although this works, there is still a 15 seconds quiesce-period when shutting down. I think this is also unnecessary, but I was not able to find an option in Flapdoodle to modify this shutdown timeout. I tried to fix it by client.getDatabase("admin").runCommand(new Document("setParameter", "1").append("shutdownTimeoutMillisForSignaledShutdown", 0)); which unfortunately didn't fix it.

Expected behavior

Quarkus should be able to shutdown a MongoDB-replica-set even on Windows. This should happen as fast as possible to not slow down test-execution.

Actual behavior

MongoDB-replica-set is not shut down at all under Windows.

How to Reproduce?

I'm not sure if this happens on every Windows-env. I'm just having a project with

@QuarkusTestResource(value = MongoReplicaSetTestResource.class, initArgs = {
        @ResourceArg(name = MongoTestResource.VERSION, value = "V7_0"),
        @ResourceArg(name = MongoTestResource.PORT, value = "27099")
})

and then executing mvn clean install in the console.

Output of uname -a or ver

Microsoft Windows [Version 10.0.19045.4291]

Output of java -version

Java version: 17.0.10, vendor: Amazon.com Inc

Quarkus version or git rev

3.9.3

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.5 (57804ffe001d7215b5e7bcb531cf83df38f93546)

Additional information

No response

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

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions