epam / fix-antenna-net-core

FIX Antenna™ .NET Core is a high-performance low latency .NET FIX Engine.
https://www.b2bits.com/trading_solutions/fix_engines/fix_engine_net-core
Apache License 2.0
34 stars 15 forks source link

Loss of data integrity with threading from OnNewMessage() #104

Closed derMaaster closed 7 months ago

derMaaster commented 8 months ago

Hi,

I am sure this is not actually an issues with the fix-antenna-net-core library, so feel free to close this issue if my posting here is inappropriate.

I get a false return on fixMessage.IsTagExists(268) even though all the message cleary has Tag268 included in the messages according to the log file.

My code example here

(This happens on 35=X incoming messages, where 268 is a required field according to the providers dictionary)

derMaaster commented 8 months ago

Definitely losing data integrity from before OnNewMessage() writes to a Channel and when a new thread reads from the Channel

viktartse commented 8 months ago

Hi @derMaaster, one thing that is definitely should be done if there is a need to process the received message later is cloning the message. The engine re-uses the same chunk of memory for every FixMessage that is passed to the OnNewMessage handler. To clone the message you can do something like this:

var messageCopy = (FixMessage)message.Clone();

derMaaster commented 7 months ago

Hi @viktartse , apologies for getting back to you only now. That does in fact work. And thank you very much.

A note or just FYI - the .Clone function seems to be a relatively expensive operation in terms of memory, and somewhat in terms of memory and also latency to some degree. In the world of high frequency trading. (To my limited knowledge and expertise on those matters.)

Viktar-Tserashchuk commented 7 months ago

I agree a call to .Clone is not very cheap but an alternative would be the engine constructing a new FixMessage object for every new message from scratch instead of reusing the same object. That would also have a cost.