Skip to content

Request: Add explicit error message when incorrectly registering codecs to prevent OutOfMemoryError #418

Description

@korri123

So I just spend a few hours debugging a strange issue that was due to this kind of code:

    @Test
    fun `verify circular reference in kmongo, codec and jackson causes out of memory error`() {
        data class Chunk (
            val name: String
        )

        val mongoClient = KMongo.createClient(
            MongoClientSettings.builder()
                .applyConnectionString(ConnectionString("mongodb://localhost:27017"))
                .codecRegistry(fromCodecs(
                    // doesn't need to be anything here but I had a custom codec
                ))
                .build()
        )
        // boom, out of memory
        mongoClient.getDatabase("test").getCollection<Chunk>()
            .insertOne(
                Chunk(
                    name = "test"
                )
            )
    }

Now this was resolved by adding MongoClientSettings.getDefaultCodecRegistry() to the codecRegistry call after the custom codecs which in hindsight is a pretty dumb mistake on my part.

The issue was basically a circular reference between LazyCodec.java's wrapped == JacksonCodec and JacksonCodec.kt's rawBsonDocumentCodec == LazyCodec so the eaches encode functions kept calling each other recursively until a java.lang.OutOfMemoryError happened.

Would it be possible to have an error message that detects if the circular references are there?

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