Hi, everyone!
Our product consists of two parts. This is the main application and the Network Extension. One of the ways these processes interact with each other is through a shared User Defaults storage based on App Groups.
Accordingly, processes have the need to notify each other about changes in the storage. KVO technology is used for this purpose and enables it. But as it turned out, there can be a problem with KVO between processes. If one process makes several property changes in a row, the system cannot guarantee the correct sequence of these events in the other process.
So, for example, Network Extension writes several property values in a row, two instances of the same repository (which are in different processes) show a different sequence of its changes.
So in Network Extension we have the following sequence of changes:
Value in ObservableCustomUserDefaultsStored changed from started to stopping
…
Value in ObservableCustomUserDefaultsStored changed from stopping to stopped
…
Value in ObservableCustomUserDefaultsStored changed from stopped to starting
…
Value in ObservableCustomUserDefaultsStored changed from starting to started
This sequence is correct, the states change one after another. But in the main application we have the following notifications:
Value in ObservableCustomUserDefaultsStored changed from started to stopped
…
Value in ObservableCustomUserDefaultsStored changed from starting to started
Value in ObservableCustomUserDefaultsStored changed from stopped to starting
In this case, the sequence of property value changes is quite different.
In the KVO documentation we can see the following: "It is important to note that KVO happens synchronosly and on the same thread as the actual change." This is naturally done for one process and the notifications come in the right order, but it is not done for several processes and the statement "KVO notifications go directly to observing objects when changes in property values occur" is broken, processes receive changes in different order breaking their atomicity.
Issue FB11888777 has been created for this problem, where we have attached both the application logs at the time of the problem and the system logs with the VPN Extension keyed logging.