Skip to content

Forwarding of RequestContext not working in async mode #34706

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

Open
blaghed opened this issue Apr 2, 2025 · 0 comments
Open

Forwarding of RequestContext not working in async mode #34706

blaghed opened this issue Apr 2, 2025 · 0 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-triage An issue we've not yet triaged or decided on

Comments

@blaghed
Copy link

blaghed commented Apr 2, 2025

Describe the bug
Trying to inherit the RequestContext to make use of the RequestAttributes after going into an @Async thread causes IllegalStateException with message Cannot ask for request attribute - request is not active anymore!.

To Reproduce
Create the following Configuration to setup the RequestContext inheritance (you can focus on the TaskDecorator part only if the rest is boilerplate to you):

@EnableAsync
@Configuration(proxyBeanMethods = false)
public class AsyncTaskConfiguration {

  // This is the bean that matters
  @Bean
  public TaskDecorator asyncTaskDecorator() {
    return runnable -> {
      final Optional<RequestAttributes> requestAttributes =
          Optional.ofNullable(RequestContextHolder.getRequestAttributes());
      return () -> {
        try {
          requestAttributes.ifPresent(it -> RequestContextHolder.setRequestAttributes(it, true));
          runnable.run();
        } finally {
          requestAttributes.ifPresent(it -> RequestContextHolder.resetRequestAttributes());
        }
      };
    };
  }

  // Making sure all TaskProcessors are applied without fanfare
  @Bean
  public BeanPostProcessor asyncBeanPostProcessor() {
    return new BeanPostProcessor() {

      private final List<TaskDecorator> taskDecorators = new ArrayList<>();

      @Override
      public Object postProcessBeforeInitialization(Object bean, String beanName)
          throws BeansException {
        if (bean instanceof TaskDecorator decorator) {
          taskDecorators.add(decorator);
        }
        if (bean instanceof ThreadPoolTaskExecutor taskExecutor) {
          taskExecutor.setTaskDecorator(new CompositeTaskDecorator(taskDecorators));
          return taskExecutor;
        }
        return bean;
      }
    };
  }

  // Make sure TaskProcessor beans are loaded before ThreadPoolTaskExecutor ones
  @Bean
  public static AbstractDependsOnBeanFactoryPostProcessor asyncTaskExecutorDependencies() {
    return new AbstractDependsOnBeanFactoryPostProcessor(
        ThreadPoolTaskExecutor.class,
        TaskDecorator.class) {
    };
  }
}

The code below will then fail if the request has already completed and the Async task is still running:

RequestContextHolder.getRequestAttributes().getAttribute("foo", SCOPE_REQUEST)

These calls still seem to work, though:

RequestContextHolder.getRequestAttributes().getAttribute("foo", SCOPE_SESSION);
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getAttribute("foo");

The validation for this is happening in a class that hasn't changed in a long time, so maybe this is dead code now?

	public Object getAttribute(String name, int scope) {
		if (scope == SCOPE_REQUEST) {
			if (!isRequestActive()) {
				throw new IllegalStateException(
						"Cannot ask for request attribute - request is not active anymore!");
			}
			return this.request.getAttribute(name);
		}

~https://github.com/spring-projects/spring-framework/blob/v6.2.5/spring-web/src/main/java/org/springframework/web/context/request/ServletRequestAttributes.java#L147-L154

Here is an example of a class that starts failing due to this nuance:

	private static URI getCachedBaseUri() {
		return (URI) getRequestAttributes().getAttribute(CACHE_KEY, RequestAttributes.SCOPE_REQUEST);
	}

~https://github.com/spring-projects/spring-hateoas/blob/2.4.1/src/main/java/org/springframework/hateoas/server/mvc/UriComponentsBuilderFactory.java#L93-L95

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 2, 2025
@jhoeller jhoeller added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

No branches or pull requests

3 participants