Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling DefaultKafkaHeaderMapper constructor with the same ObjectMapper instance may result memory leak #2611

Closed
sbozcan opened this issue Mar 9, 2023 · 5 comments · Fixed by #3097
Labels
ideal-for-user-contribution An issue that would ideal for a user to get started with contributing. type: bug
Milestone

Comments

@sbozcan
Copy link

sbozcan commented Mar 9, 2023

In what version(s) of Spring for Apache Kafka are you seeing this issue?

2.8.2

Describe the bug

DefaultKafkaHeaderMapper is registering a SimpleModule in constructor to the given ObjectMapper instance. Since the SimpleModule's are unnamed modules, IGNORE_DUPLICATE_MODULE_REGISTRATIONS configuration of ObjectMapper doesn't handle multiple registration of this module. This is problematic and can cause a memory leak if the DefaultKafkaHeaderMapper(ObjectMapper mapper) constructor is called with the same ObjectMapper instance multiple times.

To Reproduce

Call the DefaultKafkaHeaderMapper(ObjectMapper mapper) function with same ObjectMapper instance multiple times.

Expected behavior

A new SimpleModule would be registered to the ObjectMapper beacuse of the new SimpleModule().addDeserializer(MimeType.class, new MimeTypeJsonDeserializer()) call.

Solution

This problematic situation can be avoided simply by giving a name to the SimpleModule created as below:

this.objectMapper.registerModule(new SimpleModule("mimeTypeJsonDeserializer", Version.unknownVersion())
						.addDeserializer(MimeType.class, new MimeTypeJsonDeserializer()));
@garyrussell
Copy link
Contributor

Thanks for pointing this out, but it would be rather unusual to create multiple header mappers.

Contributions are welcome.

@garyrussell garyrussell added this to the Backlog milestone Mar 9, 2023
@garyrussell garyrussell added the ideal-for-user-contribution An issue that would ideal for a user to get started with contributing. label Mar 9, 2023
@artembilan
Copy link
Member

Yeah... That's why I always say that it is better to not mutate an externally provided objects:

		this.objectMapper = objectMapper;
		this.objectMapper
				.registerModule(new SimpleModule().addDeserializer(MimeType.class, new MimeTypeJsonDeserializer()));

@garyrussell
Copy link
Contributor

That's a good point; that code should only be applied to an internally created OM.

@garyrussell
Copy link
Contributor

Maybe it can now be removed altogether (at least in 3.1); it was introduced a long time ago for backwards compatibility with old, unsupported, versions.

#892

@artembilan
Copy link
Member

I guess so. Since it is an internal API we probably can remove it without any side-effects.

@artembilan artembilan modified the milestones: Backlog, 3.2.0-M2 Mar 4, 2024
artembilan pushed a commit that referenced this issue Mar 4, 2024
Fixes: #2611

The `MimeTypeJsonDeserializer` was introduced a long time ago for backwards compatibility with old, unsupported, versions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ideal-for-user-contribution An issue that would ideal for a user to get started with contributing. type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants