Skip to content

java.lang.IllegalStateException: Only one 'RetryTopicConfigurationSupport' is allowed when Spring context is reloaded #2477

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

Closed
jrenzullo opened this issue Nov 10, 2022 · 5 comments · Fixed by #2479

Comments

@jrenzullo
Copy link

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

Describe the bug
RetryTopicConfigurationSupport has a static variable to keep track of whether or not the class has been instantiated more than once. This breaks unit tests whenever the Spring context is refreshed.

public RetryTopicConfigurationSupport() {
	Assert.state(ONLY_ONE_ALLOWED.getAndSet(false), "Only one 'RetryTopicConfigurationSupport' is allowed");
}

To Reproduce
Setup a Kafka listener with a retry topic configuration. Then create a unit test where the Spring context is refreshed. I forced it by using the DirtiesContext annotation, but this can occur anytime the context is reloaded.

Expected behavior
You should be able to reset the Spring context without any unexpected side effects.

Sample
https://github.com/jrenzullo/spring-kafka-test-bug

@garyrussell
Copy link
Contributor

We handle this in the framework tests by subclassing this class in all retry topic test classes:

public class AbstractRetryTopicIntegrationTests {
protected AbstractRetryTopicIntegrationTests() {
}
@BeforeAll
static void reset() throws Exception { // NOSONAR
Field field = RetryTopicConfigurationSupport.class.getDeclaredField("ONLY_ONE_ALLOWED");
field.setAccessible(true);
((AtomicBoolean) field.get(null)).set(true);
}
}

@garyrussell
Copy link
Contributor

If you have a better suggestion, please submit a PR.

@jrenzullo
Copy link
Author

Are you suggesting every consumer of this library should do the same? I would suggest we remove this field entirely since static state causes issues with testing. Do you know the reason for having this field/check in the first place?

@jrenzullo
Copy link
Author

Also to be clear this is not specific to unit testing. This can happen any time you restart the Spring context, which could happen on a live production app as well.

@garyrussell
Copy link
Contributor

Good point. We should change it to ensure there is only one instance in each application context.

@garyrussell garyrussell added this to the 3.0.0 milestone Nov 11, 2022
garyrussell added a commit to garyrussell/spring-kafka that referenced this issue Nov 14, 2022
Resolves spring-projects#2477

Previously, a static field was used to detect/prevent multiple instances
of `RetryTopicConfigurationSupport`. This causes problems in test suites
or when an application context might be destroyed and re-created in the
same class loader.

Instead, detect and warn when multiple instances are found in an application context.

**cherry-pick to 2.9.x**
garyrussell added a commit to garyrussell/spring-kafka that referenced this issue Nov 14, 2022
Resolves spring-projects#2477

Previously, a static field was used to detect/prevent multiple instances
of `RetryTopicConfigurationSupport`. This causes problems in test suites
or when an application context might be destroyed and re-created in the
same class loader.

Instead, detect and warn when multiple instances are found in an application context.

**cherry-pick to 2.9.x**
artembilan pushed a commit that referenced this issue Nov 14, 2022
Resolves #2477

Previously, a static field was used to detect/prevent multiple instances
of `RetryTopicConfigurationSupport`. This causes problems in test suites
or when an application context might be destroyed and re-created in the
same class loader.

Instead, detect and warn when multiple instances are found in an application context.

**cherry-pick to 2.9.x**

* Fix test.
artembilan pushed a commit that referenced this issue Nov 14, 2022
Resolves #2477

Previously, a static field was used to detect/prevent multiple instances
of `RetryTopicConfigurationSupport`. This causes problems in test suites
or when an application context might be destroyed and re-created in the
same class loader.

Instead, detect and warn when multiple instances are found in an application context.

**cherry-pick to 2.9.x**

* Fix test.
# Conflicts:
#	spring-kafka/src/test/java/org/springframework/kafka/retrytopic/RetryTopicIntegrationTests.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants