|
1 |
| -# Common Errors |
2 |
| -This section lists some of the causes and solutions for common errors coming from [the safety system](concepts-safety.md). |
| 1 | +# Common error messages |
| 2 | +This section lists some causes and solutions for common errors coming from [the safety system](concepts-safety.md). |
3 | 3 |
|
4 | 4 | ## Errors related to the Dependency property
|
5 |
| -The [`Dependency`](xref:Unity.Entities.SystemState.Dependency) property is used to record the handles of all jobs a system is scheduling. This is important so that later systems can pass the right dependencies into their jobs to avoid data races. The safety system makes a best effort attempt to detect cases where a job has not been assigned to the `Dependency` property. In these cases, you will see an error such as: |
| 5 | +The [`Dependency`](xref:Unity.Entities.SystemState.Dependency) property is used to record the handles of all jobs a system is scheduling. This is important so that later systems can pass the right dependencies into their jobs to avoid data races. The safety system makes a best effort attempt to detect cases where a job isn't assigned to the `Dependency` property. In these cases, the following error is produced: |
| 6 | + |
6 | 7 | ```
|
7 | 8 | The system <SYSTEM NAME> reads <COMPONENT NAME> via <JOB NAME> but that type was not assigned to the Dependency property. To ensure correct behavior of other systems, the job or a dependency must be assigned to the Dependency property before returning from the OnUpdate method.
|
8 | 9 | ```
|
9 | 10 |
|
10 | 11 | Common causes for this error are:
|
11 |
| - - Your code does not assign the `JobHandle`s of the jobs it is scheduling to the `Dependency` property. |
12 |
| - - The safety system could not determine which component types your system is using. This can happen when you use [`EntityQueries`](xref:Unity.Entities.EntityQuery) that were not created using [`GetEntityQuery`](xref:Unity.Entities.SystemState.GetEntityQuery). Your system must not use any query that it did not create itself using `GetEntityQuery`. In particular, do not create entity queries using the `EntityManager`. |
13 |
| - - An exception elsewhere in the system update is causing `OnUpdate` to terminate before the `Dependency` property is assigned (in which case, you should see the real exception logged to the console as well, and this error is a red herring). |
| 12 | + - Your code doesn't assign the `JobHandle`s of the jobs it's scheduling to the `Dependency` property. |
| 13 | + - The safety system could not determine which component types your system used. This can happen when you use [`EntityQueries`](xref:Unity.Entities.EntityQuery) that weren't created with [`GetEntityQuery`](xref:Unity.Entities.SystemState.GetEntityQuery*). Your system must not use any query that it didn't create itself using `GetEntityQuery`. In particular, do not create entity queries using the `EntityManager`. |
| 14 | + - An exception elsewhere in the system update is causing `OnUpdate` to terminate before the `Dependency` property is assigned. In this case, the real exception is logged to the console, meaning that the previous error is misleading and can be ignored. |
14 | 15 |
|
15 | 16 | ## Errors related to parallel writing
|
16 |
| -You may sometimes encounter errors such as: |
| 17 | + |
| 18 | +You might sometimes encounter errors such as: |
| 19 | + |
17 | 20 | ```
|
18 | 21 | InvalidOperationException: <JOB FIELD> is not declared [ReadOnly] in a IJobParallelFor job. The container does not support parallel writing. Please use a more suitable container type.
|
19 | 22 | ```
|
20 |
| -This error means that you are using a parallel job and this job is accessing something in parallel in a way that may be unsafe. Parallel jobs in the Entities package include [`IJobChunk`](xref:Unity.Entities.IJobChunk) and [`IJobEntity`](xref:Unity.Entities.IJobEntity). |
21 | 23 |
|
22 |
| -It is generally unsafe to access data concurrently, unless your job is only reading that data. However, sometimes you may know through domain specific knowledge that your concurrent writes are safe. This is for example the case if you know that no two threads will ever access the same value in parallel. |
| 24 | +This error means that a parallel job is accessing something in parallel in a way that might be unsafe. Parallel jobs in the Entities package include [`IJobChunk`](xref:Unity.Entities.IJobChunk) and [`IJobEntity`](xref:Unity.Entities.IJobEntity), and [`ParallelFor` and `ParallelForTransform`](xref:um-job-system-jobs) in the core engine. |
| 25 | + |
| 26 | +It's generally unsafe to write the same data concurrently from multiple threads. However, sometimes you might know through domain specific knowledge that your concurrent writes are safe. This is for example the case if you know that no two threads will ever access the same value in parallel. |
23 | 27 |
|
24 | 28 | Common causes for this error are:
|
25 | 29 | - You are actually only reading from this data. In this case, adding the [`ReadOnly`](https://docs.unity3d.com/ScriptReference/Unity.Collections.ReadOnly.html) attribute is the correct fix.
|
26 | 30 | - You are using a native container in a parallel job without the [`ReadOnly`](https://docs.unity3d.com/ScriptReference/Unity.Collections.ReadOnly.html) attribute.
|
27 |
| - - You are using a [`ComponentLookup`](xref:Unity.Entities.ComponentLookup) in a parallel job without [`ReadOnly`](https://docs.unity3d.com/ScriptReference/Unity.Collections.ReadOnly.html) attribute. |
| 31 | + - You are using a [`ComponentLookup`](xref:Unity.Entities.ComponentLookup`1) in a parallel job without the [`ReadOnly`](https://docs.unity3d.com/ScriptReference/Unity.Collections.ReadOnly.html) attribute. |
28 | 32 |
|
29 |
| -If you can guarantee that your access is safe, you may use the [`NativeDisableParallelForRestriction`](https://docs.unity3d.com/ScriptReference/Unity.Collections.NativeDisableParallelForRestrictionAttribute.html) attribute to silence the error. |
| 33 | +If you can guarantee that your access is safe, you can use the [`NativeDisableParallelForRestriction`](https://docs.unity3d.com/ScriptReference/Unity.Collections.NativeDisableParallelForRestrictionAttribute.html) attribute to silence the error. |
30 | 34 |
|
31 | 35 | ## Errors related to missing dependencies on previously scheduled jobs
|
32 |
| -When the safety system detects that a job is missing a dependency on another job, you will see an error such as this: |
| 36 | + |
| 37 | +When the safety system detects that a job is missing a dependency on another job, it produces an error such as this: |
| 38 | + |
33 | 39 | ```
|
34 | 40 | InvalidOperationException: The previously scheduled job <JOB NAME> writes to the <OBJECT TYPE> <FIELD IN JOB>. You are trying to schedule a new job <OTHER JOB NAME>, which writes to the same <OBJECT TYPE> (via <FIELD IN OTHER JOB>). To guarantee safety, you must include <JOB NAME> as a dependency of the newly scheduled job.
|
35 | 41 | ```
|
36 |
| -The safety system will flag all cases where the same component type is used in multiple jobs that may run concurrently. In some cases, it may still be safe for these jobs to be executing in parallel as long as you can guarantee that the entities the concurrent jobs operate on do not overlap. |
| 42 | + |
| 43 | +The safety system will flag all cases where the same component type is used in multiple jobs that could run concurrently. Sometimes, it's still safe for these jobs to be executing in parallel as long as you can guarantee that the entities the concurrent jobs operate on don't overlap. |
37 | 44 |
|
38 | 45 | A common cause for this error is that you are scheduling multiple copies of the same job, each on a query with different shared component values. This is safe, as queries with different values for the same shared component type never overlap. You can use the [`NativeDisableContainerSafetyRestriction`](https://docs.unity3d.com/ScriptReference/Unity.Collections.LowLevel.Unsafe.NativeDisableContainerSafetyRestrictionAttribute.html) attribute on affected fields to disable this error.
|
39 | 46 |
|
40 | 47 | ## Errors related to safety handles
|
41 |
| -When the safety system detects that a job is reading a resource that it doesn't have access to, you may see an error such as: |
| 48 | + |
| 49 | +When the safety system detects that a job is reading a resource that it doesn't have access to, an error like the following might be produced: |
| 50 | + |
42 | 51 | ```
|
43 | 52 | InvalidOperationException: The <JOB FIELD> has been declared as [WriteOnly] in the job, but you are reading from it.
|
44 | 53 | ```
|
45 |
| -In practice, `[WriteOnly]` resources are extremely uncommon. A more likely cause for this error is that the read in question is occurring from a job that was launched from within another job. Launching jobs from jobs is not currently supported, and the resulting safety handles won't be set up correctly. This includes less obvious cases like using `.job.Run()` from inside of an `Entities.ForEach().Run()` lambda function, which is itself implemented as a main-thread job. |
| 54 | +In practice, `[WriteOnly]` resources are rare. A more likely cause for this error is that the read in question is occurring from a job that was launched from within another job. Launching jobs from jobs is not currently supported, and the resulting safety handles won't be set up correctly. This includes obscure cases like using `.job.Run()` from inside of an `Entities.ForEach().Run()` lambda function, which is itself implemented as a main-thread job. |
46 | 55 |
|
47 | 56 | ## Errors related to system type definitions
|
48 |
| -The Entities package makes extensive use of source generators to implement core features like `IJobEntity`, `ISystem`, |
49 |
| -and `SystemAPI`. One requirement this imposes is that all system types and `IJobEntity` implementations must have the |
50 |
| -`partial` keyword, so that the source generators can extend the types with additional generated methods. It's easy to |
51 |
| -forget to add the `partial` keyword, which may lead to the following compiler error: |
| 57 | + |
| 58 | +All system types and `IJobEntity` implementations must have the `partial` keyword, so that the source generators can extend the types with additional generated methods. If you forget to add the `partial` keyword, it might lead to the following compiler error: |
| 59 | + |
52 | 60 | ```
|
53 | 61 | error CS0101: The namespace <NAMESPACE> already contains a definition for <SYSTEM/JOB TYPE>
|
54 | 62 | ```
|
55 |
| -Also note that a system deriving from the managed `SystemBase` interface should be a `class`, |
| 63 | + |
| 64 | +Also note that a system deriving from the managed `SystemBase` interface must be a `class`, |
56 | 65 | while an unmanaged system implementing the `ISystem` interface must be a `struct`.
|
0 commit comments