Jobified Networking - job dependency problems

Well this problem is really specific (Advanced).

I’ve the following error message:
“You are trying to schedule a new job which writes to the same NativeArray”

So basically I’m writing NetworkSerializationSystems code.
Therefore I have created custom jobtypes and containers for NativeNetworkWriter.
And the error happens when two jobs of different NetworkSerializationSystems try to write on the NativeNetworkWriter

Here is an example of a SerializationSystem:

public class SerializePositionSystem : BBSSerializeJobComponentSystem {
    [BurstCompile]
    struct SerializePosJob : IJobSerializeNetworkComponentData<Translation> {
        public void Execute(NativeNetworkWriter nativeNetworkWriter, ref Translation translation) {
            nativeNetworkWriter.Write(translation.Value);
        }
    }

    protected override JobHandle OnUpdateNetwork(JobHandle inputDeps) {
        return new SerializePosJob().Schedule(this, inputDeps);
    }
}

Scheduling a IJobSerializeNetworkComponentData works similar to IJobProcessComponentData
except that it is singlethreaded (base job is IJob) and it provides the NativeNetworkWriter as parameter.

The baseclass BBSNetworkBaseJobComponentSystem executes the OnUpdate and is responsible for adding inputDeps to the EndFrameBBSNetworking system:

    public abstract class BBSNetworkBaseJobComponentSystem : JobComponentSystem {
        //....
        protected sealed override JobHandle OnUpdate(JobHandle inputDeps) {
            inputDeps = OnUpdateNetwork(inputDeps);
            EndFrameBBSNetworking.AddJobHandleForNetworkProducer(inputDeps);
            return inputDeps;
        }
    }

EndFrameBBSNetworking works similar to a normal EntityCommandBufferSystem except that it provides methods to create NativeNetworkWriter and is also responsible for CombineDependencies to ensure everything is threadsafe and finished at the execution time of EndFrameBBSNetworking:

    public class EndFrameBBSNetworking : EntityCommandBufferSystem {
        //....
        internal void AddJobHandleForNetworkProducer(JobHandle producerJob) {
            m_NetworkProducerHandle = JobHandle.CombineDependencies(m_NetworkProducerHandle, producerJob);
        }


        protected override void OnUpdate() {
            base.OnUpdate();
            FlushBuffers();
        }

        private void FlushBuffers() {
            m_NetworkProducerHandle.Complete();
            m_NetworkProducerHandle = new JobHandle();

            for (int i = 0; i < m_PendingWriters.Count; i++) {
                NativeNetworkWriter writer = m_PendingWriters[i];
                //TODO Data transfer to network API
                writer.Dispose();
            }

            for (int i = 0; i < m_PendingReaders.Count; i++) {
                NativeNetworkReader reader = m_PendingReaders[i];
                reader.Dispose();
            }

            m_PendingWriters.Clear();
            m_PendingReaders.Clear();
        }
    }

Also the NativeNetworkWriter has the same implementation as the DataStreamWriter from multiplay project from github:
https://github.com/Unity-Technologies/multiplayer/blob/master/com.unity.transport/Runtime/DataStream.cs

Now my problem is that I don’t know why the system throw me the dependency exception…

It’s a lot of code so I can’t provide full source code…

By the way for all of you who are interested in this work.
I plan to publisch this.

Edit:
A more detailed view of the error and the system

Ok I made a test with just the NativeNetworkWriter and two jobs in the same system

public class TestSys : JobComponentSystem {

    struct Job1 : IJob {
        public NativeNetworkWriter NativeNetworkWriter;
        public void Execute() {
            NativeNetworkWriter.Write(1);
        }
    }

    struct Job2 : IJob {
        public NativeNetworkWriter NativeNetworkWriter;

        public void Execute() {
            NativeNetworkWriter.Write(2);
        }
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps) {
        var networkStream = new NativeNetworkWriter(1024, Unity.Collections.Allocator.TempJob);
        var job1 = new Job1() {
            NativeNetworkWriter = networkStream,
        };

        var job2 = new Job2() {
            NativeNetworkWriter = networkStream,
        };

        inputDeps = job1.Schedule(inputDeps);
        inputDeps = job2.Schedule(inputDeps);

        return inputDeps;
    }
}

this works without any problem (ignore dealocation)

I’ve found my problem…

I have to add “inputDeps = JobHandle.CombineDependencies(EndFrameBBSNetworking.NetworkJobHandle, inputDeps);” right bevor OnUpdateNetwork function.

    public abstract class BBSNetworkBaseJobComponentSystem : JobComponentSystem {
        protected sealed override JobHandle OnUpdate(JobHandle inputDeps) {
            inputDeps = JobHandle.CombineDependencies(EndFrameBBSNetworking.NetworkJobHandle, inputDeps);
            inputDeps = OnUpdateNetwork(inputDeps);
            EndFrameBBSNetworking.AddJobHandleForNetworkProducer(inputDeps);
            return inputDeps;
        }
}

@Joachim_Ante_1 is this the intended way for handling cross-system native containers?