Why do messages sometimes have strange data when I'm using asynchronous processing?

For performance, the AMPS clients reuse the message object that's passed into asynchronous message handlers rather than creating a new object each time the message handler is called.

If you are saving the message between calls to the message handler, or passing the message to another thread, you must make a copy of the message object. Otherwise, AMPS will overwrite the data in the message object after the message handler returns.

If your application processes messages asynchronously, this also means that the message object may be in the process of being updated during processing, which means that the processor may see inconsistent results. As with most multithreading issues, this is timing dependent, and so the results may vary from message to message, or on different runs of the program.

To solve the problem, simply make a copy of the message object, or the relevant data, if you need the data outside of the call to the message handler.

Keywords: threading, garbled data, object reuse, troubleshooting

Last updated