MQL5 EA lifecycle ordering during account change/reconnect

 
We need clarification on the documented ordering guarantees for an
already-running Expert Advisor when the trading account changes or a
REASON_ACCOUNT reconnect occurs.

Assume an EA uses a two-event protocol:

Event handler A:
- reads AccountInfo*/TerminalInfo* data
- prepares a response in private memory
- returns without publishing it

Later event handler B:
- rechecks the same account/server context
- verifies IsStopped() == false
- immediately commits/publishes the previously prepared response

Question:

If an account change or REASON_ACCOUNT-related reconnect occurs after
handler B's final context check but before its response publication,
does MetaTrader 5 guarantee that the EA's old instance will receive
deinitialization / REASON_ACCOUNT before it can complete that
publication?

More specifically:

1. Does deinitialization caused by account change/reconnect have
ordering priority over an already queued EA event handler?

2. Can the currently executing handler continue running and publish
data after the terminal account context has changed but before
OnDeinit(REASON_ACCOUNT) is delivered?

3. Is there any documented MQL5 mechanism that provides a lifecycle
fence guaranteeing that no response from the old EA instance can be
committed after the account context has changed?

We are not asking whether events are processed sequentially in general.
We specifically need the ordering guarantee between account lifecycle
changes, OnDeinit(REASON_ACCOUNT), and publication by the old EA
instance.

If no such guarantee exists, please confirm that explicitly.

Thank you.
 
Kuna Shoo:
We need clarification on the documented ordering guarantees for an
already-running Expert Advisor when the trading account changes or a
REASON_ACCOUNT reconnect occurs.

Assume an EA uses a two-event protocol:

Event handler A:
- reads AccountInfo*/TerminalInfo* data
- prepares a response in private memory
- returns without publishing it

Later event handler B:
- rechecks the same account/server context
- verifies IsStopped() == false
- immediately commits/publishes the previously prepared response

Question:

If an account change or REASON_ACCOUNT-related reconnect occurs after
handler B's final context check but before its response publication,
does MetaTrader 5 guarantee that the EA's old instance will receive
deinitialization / REASON_ACCOUNT before it can complete that

publication?

1. There is no "old" instance, it's the same EA that continues to run.

2. If the EA is already running an event handler it will go to the end of it. An event handler processing is never stopped to run an other event handler.


More specifically:

1. Does deinitialization caused by account change/reconnect have

ordering priority over an already queued EA event handler?

That's a good question.
2. Can the currently executing handler continue running and publish
data after the terminal account context has changed but before

OnDeinit(REASON_ACCOUNT) is delivered?

Yes without a doubt as I explained already above.

3. Is there any documented MQL5 mechanism that provides a lifecycle
fence guaranteeing that no response from the old EA instance can be
committed after the account context has changed?

We are not asking whether events are processed sequentially in general.
We specifically need the ordering guarantee between account lifecycle
changes, OnDeinit(REASON_ACCOUNT), and publication by the old EA
instance.

If no such guarantee exists, please confirm that explicitly.

Thank you.

In case an event handler is already running, there is no such guarantee.

For the subsequent events I don't know, I asked MetaQuotes.