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

BeanCurrentlyInCreationException is thrown when multiple threads simultaneously try to create a factory bean after upgrade to spring 6.2.0 #33972

Open
dt-mafe opened this issue Nov 26, 2024 · 0 comments
Labels
status: waiting-for-triage An issue we've not yet triaged or decided on

Comments

@dt-mafe
Copy link

dt-mafe commented Nov 26, 2024

After upgrading to spring boot 3.4 and spring 6.2.0 we sometimes see a BeanCurrentlyInCreationException in our logs after application startup.
Looking into it, this seems to be happening when multiple threads simultaneously first create a FactoryBean (in our application we are using spring mvc and this happens when multiple concurrent http requests happen after startup where a BeanCurrentlyInCreationException is thrown for a request scoped bean).

This was working fine in spring 6.1.15 and is broken in 6.2.0.

From a quick look, I would suspect that this is related to changes in this issue where locking in FactoryBeanRegistrySupport was removed.

The following is an example test (inspired by the example of this issue) which runs without any exception in 6.1.15 but does throw an exception in 6.2.0

package org.springframework.beans.factory.xml;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.support.StaticApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

class BeanFactoryRaceConditionTest {

	private final ExecutorService executorService = Executors.newFixedThreadPool(10);

	@Test
	void testRaceCondition() {
		StaticApplicationContext applicationContext = new StaticApplicationContext();
		applicationContext.registerSingleton("book", BookFactory.class);
		List<Future<?>> allFutures = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			var future = executorService.submit(() -> {
				for (int j = 0; j < 1000; j++) {
					try {
						Book book = applicationContext.getBean(Book.class);
						Assertions.assertThat(book).isNotNull();
					} catch (BeanCurrentlyInCreationException e) {
						throw new RuntimeException(e);
					}
				}
			});
			allFutures.add(future);
		}
		Assertions.assertThatCode(() -> {
			for (Future<?> future : allFutures) {
				future.get();
			}
		}).doesNotThrowAnyException();
	}

	static class Book {

	}

	static class BookFactory implements FactoryBean<Book> {

		@Override
		public Book getObject() {
			return new Book();
		}

		@Override
		public Class<?> getObjectType() {
			return Book.class;
		}
	}
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

No branches or pull requests

2 participants