Race condition Gateway Tag Change Script — shared tag reads- memory tag

Two Gateway Tag Change Scripts fire simultaneously when the PLC changes shift:

PLC
 |
 |── Shift tag changes ──► Script A
 |                              writes memoryTag(Mtag1)
 |
 |── Machine status changes ──► Script B
                                    reads memoryTag(Mtag1)

The race

Both scripts fire at the same instant. Script B reads the memory tag before Script A has finished writing it.

Script A                    Script B
         
        |                           |
starts writing                  starts reading
memoryTag                       memoryTag
        |                           |
        |                    reads OLD value  ← stale
        |                           |
finishes writing             writes DB row
memoryTag (new value)        with OLD Mtag1 value

Script B gets the old value. The DB row is filed under the wrong production period.

Question

Is there a supported pattern to guarantee Script B reads the memory tag only after Script A has finished writing it?
Is there any better Approach to achieve it

Is it always sequential? Any reason not to start Script B after Script A in the same gateway script to completely sidestep having 2 scripts running at once?

Either that or use an intermediate tag that is both "script a has finished and machine status has changed"

Best practice is not to write to the same tag you are reading from, for the same reasons the OP is demonstrating.

saipeddireddy10, if you can tell us what you're trying to do, we can probably give you a better answer. nvm, I think I see what you're trying to do.
What are you writing with script A?

You will always have race possibilities like this since Ignition is highly multi-threaded and parallelized. The best way to ensure sequential behavior is to use just one script.

I wouldn't try to write the production period in the machine status script. Just write the new status and timestamp. The timestamp implicitly indicates the period when looked up later.

Yeah, it is always sequential, but both the scripts only trigger on a shift change, so it is once per shift, after that script B runs on it owns tag changes.

I am use an intermediate tag a Script_A_Finish as False on starting of script A and only become true on it execution completed. on scrpt B i am checking if Script_A_Finish = True then execute else skip the execution but then I missing the data insert

Script A starts
Script_A_Finish → False
|
Script B fires ← Script_A_Finish = False → SKIP
| machine status event lost forever
Script A finishes
Script_A_Finish → True
|
nothing refires Script B

Then making it one script seems to be ideal. Or, make the scripts project level and call them from a single event.

What is firing script b? It sounds like script B should be fired on change when script A finish is true.

{My Bold} This is simply not true, or possible within tag events. There's no ordering forced on tag value deliveries, and the corresponding tag events, with everything running mostly in parallel. And based on driver poll phase shift versus PLC logic, events in the PLC can be seen out of order in Ignition.

You cannot do this reliably with two tag change events. Use one event and one script.

I also do not see any value add to making it 2 scripts instead of one, each on their tag changes, if they really are supposed to be sequentially run every time. You don't gain anything for splitting them up.

script A sits on shift tag changes events, script just copy plc tag value to the intermediate memory tag(it is triggered on every start of shift)
script B sits on machine status tag events, it uses this intermediate memory tag and insert the data to DB

reason why I am using this intermediate memory tag is for fail safe, if I don't receive plc value in a particular time then I am updating on other script

You should put both scripts in the project library. Then in Script A, you call Script B.

This allows you to call both scripts from different tag changes, and Script B can always be called sequentially at the end of Script A.