You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2018/09/26 10:34:29 UTC

[GitHub] otyyyywangwenbin opened a new issue #1707: ContextManager.continued NPE

otyyyywangwenbin opened a new issue #1707: ContextManager.continued NPE
URL: https://github.com/apache/incubator-skywalking/issues/1707
 
 
   I'm try writing a netty plugin and have a cross threads problem
   
   I read doc
   
   1. https://github.com/apache/incubator-skywalking/blob/5.x/docs/cn/Plugin-Development-Guide-CN.md#%E4%B8%89-contextsnapshot
   2. https://github.com/apache/incubator-skywalking/blob/5.x/docs/en/Plugin-Development-Guide.md#contextsnapshot
   
   Capture a ContextSnapshot in another thread and continued ContextSnapshot in below codes:
   ```java
       public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
           ChannelHandlerContext context = (ChannelHandlerContext) allArguments[0];
           Object msg = allArguments[1];
           if (msg instanceof HttpRequest) { // Client Request
               System.out.println(">> " + Thread.currentThread().getId() + ":" + context.channel().hashCode());
               Attribute<ContextSnapshot> attr = context.channel().attr(KEY_CONTEXT_SNAPSHOT);
               ContextSnapshot contextSnapshot = attr.get();
               attr.set(null);
               if (contextSnapshot == null) {
                   return;
               }
               ContextManager.continued(contextSnapshot);  (Line: 45)
               HttpRequest request = (HttpRequest) msg;
               ContextCarrier contextCarrier = new ContextCarrier();
               String uri = request.uri();
               AbstractSpan span = ContextManager.createExitSpan(uri, contextCarrier, String.valueOf(context.channel().remoteAddress()));
               Tags.URL.set(span, request.uri());
               Tags.HTTP.METHOD.set(span, request.method().name());
               span.setComponent(COMPONENT_NETTY_HTTP_CLIENT);
               SpanLayer.asHttp(span);
               CarrierItem next = contextCarrier.items();
               while (next.hasNext()) {
                   next = next.next();
                   request.headers().set(next.getHeadKey(), next.getHeadValue());
               }
               context.channel().attr(KEY_CONTEXT_SNAPSHOT).set(ContextManager.capture());
   
               System.out.println("===> encode client request $$$$$$$$ " + Thread.currentThread().getId() + ":" + context.channel().hashCode());
           }
           return;
       }
   ```
   
   skywalking-api.log :
   ```
   ERROR 2018-09-26 17:29:56:913 InstMethodsInter :  class[class io.netty.handler.codec.http.HttpClientCodec$Encoder] before method[encode] intercept failure
   java.lang.NullPointerException
   	at org.apache.skywalking.apm.agent.core.context.ContextManager.capture(ContextManager.java:143)
   	at org.apache.skywalking.apm.agent.core.context.ContextSnapshot.isFromCurrent(ContextSnapshot.java:119)
   	at org.apache.skywalking.apm.agent.core.context.ContextManager.continued(ContextManager.java:150)
   	at org.apache.skywalking.apm.plugin.netty.EncodeInterceptor.beforeMethod(EncodeInterceptor.java:45)
   	at org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstMethodsInter.intercept(InstMethodsInter.java:82)
   ```
   
   how to resolve this situation?
   skywalking 5.0.0-rc2

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services