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

I 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?

[ Full content available at: 
https://github.com/apache/incubator-skywalking/issues/1706 ]
This message was relayed via gitbox.apache.org for [email protected]

Reply via email to