aws / aws-sdk-java-v2

The official AWS SDK for Java - Version 2
Apache License 2.0
2.12k stars 804 forks source link

Thread locals are lost after the control is returned from SDK methods to the client code #5242

Open electronic-dk opened 1 month ago

electronic-dk commented 1 month ago

Describe the feature

This is pretty much a copy of this issue #2142 to be able to continue the discussion. It's been closed for a while, so I guess it's not actively monitored anymore even though the issue still exists.

Basically the issue is that since aws sdk v2 uses netty nio, all thread locals are lost after any async sdk method is completed, that includes spring security context, mdc, servlet request attributes or anything else the developer might have put into a thread local. Unfortunately, there seems to be no way to tell the sdk to preserve this context or extend the sdk in a way that makes it possible. This makes it really hard to use aws sdk v2 in a spring web mvc environment without resorting to migrating to kotlin coroutines or some hacky workarounds (e.g. aspects). Specifying SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR is also not a solution, since this executor is used after the control has returned from the netty thread pool so the context is already lost at this point.

Use Case

Logging and tracing in a Spring Web MVC environment. Trace and span ids are stored as MDC context (ThreadLocal variables). It's also common to put app-specific data into MDC context to be able to match log lines to a specific request. Currently, all this data are lost after any SDK call, so tracing essentially breaks after an AWS SDK call.

Proposed Solution

No response

Other Information

A shaky workaround I ended up using is to have an aspectj aspect around AWS SDK methods that adds a CompletableFuture handler that restores Thread Locals.

Acknowledgements

AWS Java SDK version used

2.25.43

JDK version used

17.0.6

Operating System and version

macOS Sonoma 14.5

electronic-dk commented 1 month ago

I've filed this one as a feature request and not a bug, as I wasn't sure if Thread Locals support was even considered initially. But the issue can be converted into a bug if you think it gives more clarity.

OS version does not really matter, the issue presents on all OS that Java supports. It can also be observed on Java 11 and 21.