jackson-databind: Intermittent failure to serialize payloads with `boolean` field (jackson 2.15.2)

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

getting - failed to serialize payload error for boolean field intermittently with java liberty servers, few times a week and then all the requests are getting same error until the server is restarted. we are using java 1.8, ibm websphere application server liberty 21.0.0.9 2.15.2 version of jackson databind

Version Information

2.15.2

Reproduction

<-- Any of the following

  1. Brief code sample/snippet: include here in preformatted/code section
  2. Longer example stored somewhere else (diff repo, snippet), add a link
  3. Textual explanation: include here –>
// Your code here
public class BaseRequest{
private boolean isStatus = false;
//getters, setters, few other fields
}

The payload is not having this field being sent, but intermittently we are getting failed to serialize payload error for this and then the particular server on which it started this error will start for all the other api calls and the server becomes non-responsive, after restart of the server the server becomes normal and no error for most of the time, and then happens once a week or two weeks

Expected behavior

no serialization error

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 25 (18 by maintainers)

Commits related to this issue

Most upvoted comments

Discoveries

So I got curious about GeneratedMethodAccessorXXX, and this is what I learned:

  1. GeneratedMethodAccessorXXX classes are dynamically created by NativeMethodAccessorImpl when .invoke(...) is called on the same Method instance many times: https://github.com/openjdk/jdk8u/blob/e32d62e2a39510f643b32d615e76f0ff9be3d9f3/jdk/src/share/classes/sun/reflect/NativeMethodAccessorImpl.java#L49-L60
    1. They will be created on the 16th Method.invoke(...) call - here’s a sample of how they look like:
    public class GeneratedMethodAccessor2 extends MethodAccessorImpl {
      public GeneratedMethodAccessor2() {
      }
    
      public Object invoke(Object var1, Object[] var2) throws InvocationTargetException {
          Boolean var10000 = new Boolean;
          if (var1 == null) {
              throw new NullPointerException();
          } else {
              TestBean var10002;
              try {
                  var10002 = (TestBean)var1;
                  if (var2 != null && var2.length != 0) {
                      throw new IllegalArgumentException();
                  }
              } catch (NullPointerException | ClassCastException var4) {
                  throw new IllegalArgumentException(var4.toString());
              }
    
              boolean var5;
              try {
                  var5 = var10002.isStatus();
              } catch (Throwable var3) {
                  throw new InvocationTargetException(var3);
              }
    
              var10000.<init>(var5);
              return var10000;
          }
      }
    }
    
    1. They will then be used on the 17th Method.invoke(...) call by DelegatingMethodAccessorImpl.
  2. If an exception is thrown by the called accessor method (e.g. NPE thrown trying to convert a null Boolean wrapper to primitive), the accessor method itself will be included in the stack trace:
Caused by: java.lang.NullPointerException
    at <..snip>TestBean.isStatus(NPEGetterTest.java:33)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:688)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:772)
  1. One way to make GeneratedMethodAccessorXXX throw NPE is by Jackson passing null as first arg (i.e. bean) to Method.invoke(...) here: https://github.com/FasterXML/jackson-databind/blob/74a57b2f6cd3b6e0fe2a61ed16734f15ba2abc95/src/main/java/com/fasterxml/jackson/databind/ser/BeanPropertyWriter.java#L688 …which is not technically possible because:
    1. That arg came from ObjectMapper.writeValueAsString, i.e. what the caller passed to Jackson to serialize.
    2. When that arg is passed from ObjectMapper.writeValueAsString all the way to BeanPropertyWriter.serializeAsField, none of the methods the arg was passed to ever re-assign it.
    3. If somehow it DID happen (it didn’t), it would’ve SUCCESSFULLY serialize as null or an NPE would’ve been thrown much, much earlier.

Suspicious

Here are the things I found suspicious:

  1. The reporter-provided stack trace says:
    Caused by: com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.JavaClass[“isStatus”])
    …but when using the provided class to reproduce the issue, I see this:
    com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: <…snip>TestBean[“status”])
    …so how did they get isStatus as property name?
  2. I checked the latest OpenJDK’s DelegatingMethodAccessorImpl (and the previous versions) and saw that it only contains 49 line-of-code, but we see this in the reporter-provided stack trace:
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55) ~[?:1.8.0]

Conclusion (current)

Ignoring the suspicious isStatus property name, my current hypothesis is:

  1. The reporter’s company is using a specially patched/hotfixed JDK/JRE…
  2. …which has generated buggy GeneratedMethodAccessorXXX class.

At this point we should probably stop @fc76gsk from throwing out questions, without giving answers to the questions asked 🤔

@apimodel("JavaClass")
public class JavaClass {
@ApiModelProperty(value="status of the message", hidden=true)
private boolean isStatus = false;
private boolean useEmail = false;

 public boolean isStatus() {
    return isStatus;
 }

public void setIsStatus(boolean isStatus) {
    this.isStatus = isStatus;
}

public boolean isUseEmail() {
    return useEmail;
}

public void setUseEmail(boolean useEmail) {
    this.useEmail = useEmail;
}
}

this is the same class, updated the name, pls check

This is the class

@ApiModel("JavaClass")
public class JavaClass{
@ApiModelProperty(value="status of the message", hidden=true)
private boolean isStatus = false;
//getters, setters, few other fields
}

We are not able to reproduce this issue. Here is the stack trace: message=Service validation error. com.JavaException: txid=xxxxxxxxx: Failed to serialize payload of com.JavaClass at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1258) [com.ibm.ws.webcontainer_1.1.56.jar:?] at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:746) [com.ibm.ws.webcontainer_1.1.56.jar:?] at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:443) [com.ibm.ws.webcontainer_1.1.56.jar:?] at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1227) [com.ibm.ws.webcontainer_1.1.56.jar:?] at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1011) [com.ibm.ws.webcontainer_1.1.56.jar:?] at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:75) [com.ibm.ws.webcontainer_1.1.56.jar:?] at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:938) [com.ibm.ws.webcontainer_1.1.56.jar:?] at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost$2.run(DynamicVirtualHost.java:279) [com.ibm.ws.webcontainer_1.1.56.jar:?] at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink$TaskWrapper.run(HttpDispatcherLink.java:1159) [com.ibm.ws.transport.http_1.0.56.jar:?] at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink.wrapHandlerAndExecute(HttpDispatcherLink.java:428) [com.ibm.ws.transport.http_1.0.56.jar:?] at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink.ready(HttpDispatcherLink.java:387) [com.ibm.ws.transport.http_1.0.56.jar:?] at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:566) [com.ibm.ws.transport.http_1.0.56.jar:?] at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.handleNewRequest(HttpInboundLink.java:500) [com.ibm.ws.transport.http_1.0.56.jar:?] at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.processRequest(HttpInboundLink.java:360) [com.ibm.ws.transport.http_1.0.56.jar:?] at com.ibm.ws.http.channel.internal.inbound.HttpICLReadCallback.complete(HttpICLReadCallback.java:70) [com.ibm.ws.transport.http_1.0.56.jar:?] at com.ibm.ws.channel.ssl.internal.SSLReadServiceContext$SSLReadCompletedCallback.complete(SSLReadServiceContext.java:1824) [com.ibm.ws.channel.ssl_1.0.56.jar:?] at com.ibm.ws.tcpchannel.internal.WorkQueueManager.requestComplete(WorkQueueManager.java:504) [com.ibm.ws.channelfw_1.0.56.jar:?] at com.ibm.ws.tcpchannel.internal.WorkQueueManager.attemptIO(WorkQueueManager.java:574) [com.ibm.ws.channelfw_1.0.56.jar:?] at com.ibm.ws.tcpchannel.internal.WorkQueueManager.workerRun(WorkQueueManager.java:958) [com.ibm.ws.channelfw_1.0.56.jar:?] at com.ibm.ws.tcpchannel.internal.WorkQueueManager$Worker.run(WorkQueueManager.java:1047) [com.ibm.ws.channelfw_1.0.56.jar:?] at com.ibm.ws.threading.internal.ExecutorServiceImpl$RunnableWrapper.run(ExecutorServiceImpl.java:238) [com.ibm.ws.threading_1.1.56.jar:?] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1160) [?:1.8.0] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [?:1.8.0] at java.lang.Thread.run(Thread.java:818) [?:2.9 (09-11-2019)] Caused by: com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.JavaClass[“isStatus”]) at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:402) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:361) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:323) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:780) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:479) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:318) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4719) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3964) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] … 35 more Caused by: java.lang.NullPointerException at sun.reflect.GeneratedMethodAccessor961.invoke(Unknown Source) ~[?:?] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55) ~[?:1.8.0] at java.lang.reflect.Method.invoke(Method.java:508) ~[?:1.8.0] at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:688) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:772) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:479) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:318) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4719) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3964) ~[com.fasterxml.jackson.core-jackson-databind-2.15.2.jar:2.15.2] … 35 more

Can you at least share the stack trace of the error?

This is just my opinion. This issue is super hard to reproduce and there are lots of moving parts. If this was my problem, I would try replacing the code that I suspect. There are lots of alternative libraries for working with JSON. In a few weeks, you will know if replacing the code has fixed the issue. If it hasn’t you need to then focus on something else to find the cause.

What we need is a reproduction. Jackson-only reproduction to be more specific. Without seeing how it is used, we cannot tell what is causing the problem