Add implementation note regarding server interceptors and thread locals#7482
Add implementation note regarding server interceptors and thread locals#7482ejona86 merged 2 commits intogrpc:masterfrom ST-DDT:javadoc/interceptor-concurrency
Conversation
|
Using a thread local like that is thread-hostile. The StreamObserver documentation tried to set expectations by saying it must be thread-compatible, but it seems ThreadLocal wasn't discussed on the linked page. {Client,Server}Call have "not thread-safe" mention, but don't go into more detail. It seems the Java community uses the terms frequently, but there isn't a good website to link to provide explanation: the details are in things like Java Concurrency in Practice and Effective Java. I think ClientCall.Listener is probably a better place to document this than ClientInterceptor, although we can make a reminder in the interceptor. I think the suggested language is too strong and not actually true. A cache stored in a ThreadLocal would be fine. I think we instead need to note that each call may be on a different thread. I think we should come up with whatever language we want, and include it across StreamObserver, ClientCall+Listener, ServerCall+Listener. We can then have {Server,Client}Interceptor have a brief mention and say to read the {Server,Client}Call documentation. We might say something like:
|
|
The |
|
@ST-DDT, thank you! It is very good to be more clear here. |
There seems to be some confusion regarding the correct usage of thread locals inside
ServerInterceptors.In all these examples the thread local is assigned during
interceptCall/startCalland only removed on completion/error/close.I try to warn about these broken implementations, but they are still out there after all and even new ones are created. IMO we should add a hint to the
ServerInterceptorthat strongly warns about the wrong usage of thread locals insideServerInterceptors.I also considered adding such a hint to
ServerCall.Listenerand the the corresponding client classes, but I would like to ask for opinion first.