googlesamples / googlesamples/unity-jar-resolver

[Bug] Possible race condition while using CommandLine

Open
#599 3 comments 0 reactions 0 assignees View on GitHub
needs-attention type: question
Dominant language
C#
Stars
1.5k
Forks
371
Avg merge
14h 21m
Merged PRs (30d)
3

Description

### [REQUIRED] Please fill in the following fields:

* Unity editor version: 2020.3.41f1
* External Dependency Manager version: 1.2.175
* Source you installed EDM4U: .unitypackage
* Features in External Dependency Manager in use: CommandLine
* Plugins SDK in use: Firebase
* Platform you are using the Unity editor on: Linux

### [REQUIRED] Please describe the issue here:
While using Unity editor in batch mode on Linux we can meet the race condition inside CommanLine class, especially in AsyncStreamReader.
Steps to reproduce:
* Install **python3** to your OS
* Clone a repo with a minimal reproducable Unity project: https://github.com/CheeryLee/EDM4U_TestLinuxFirebase
* Run `cd /path/to/unpacked/project`
* Make sure there are no _Library_ and _Temp_ folders. It's important to reimport the project from a clean state before every start otherwise Firebase's AssetPostprocessor won't work.
* Launch Unity with such arguments: `/Unity/Hub/Editor/2020.3.41f1/Editor/Unity -projectPath ./ -batchmode -quit -logFile /dev/stdout`. **Be careful:** you must use batch mode, not GUI.

#### Please answer the following, if applicable:
**What's the issue repro rate?**
Pretty often (I would to say more than often), but not 100% - can't find a strict dependency. But I can say the following things:

1. removing **Firebase.Editor.dll** solves the problem. Repro: 0;
2. removing shaders from project solvers the problem too. Repro: 0;
3. using them together causes troubles.

**What happened? How can we make the problem occur?**
The problem appears when Unity executes Firebase's realization of AssetPostprocessor class. There is a sequental execution of Python scripts inside it. On the first step the script tries to determine installed version of Python via runing `python --version` by `CommandLine`:
https://github.com/firebase/firebase-unity-sdk/blob/main/editor/app/src/PythonExecutor.cs#L129

You are lucky to find the issue at first try if you see these log entries:

![image](https://user-images.githubusercontent.com/11297752/222210968-4680bc42-ec0e-45c4-9a43-038cc25ef3c6.png)

If nothing happens after 10 seconds, connect a debugger and pause the program to see what's going on. I use Rider, so the screenshots will be from there. These are the points in different threads where resolver execution suspends:

1. `CommandLine.AsyncStreamReaderMultiplexer` waits for queued item to be ready to read:

![Снимок экрана от 2023-03-01 20-15-23](https://user-images.githubusercontent.com/11297752/222213354-5145dad6-03b2-4498-afd7-c099cec8ef48.png)

2. `CommanLine.AsyncStreamReader` hasn't got a signal to free read event and thus hasn't fired `DataReceived` callback with completion argument:

![Снимок экрана от 2023-03-01 20-16-31](https://user-images.githubusercontent.com/11297752/222213884-87ee665a-bb26-4d56-8f63-aa9625b2958e.png)

3. Following the facts above we can't go on without `complete` event inside `RunViaShell` being set:

![Снимок экрана от 2023-03-01 20-20-10](https://user-images.githubusercontent.com/11297752/222214443-15dd3438-84e4-4756-a028-fa6b1cb27595.png)

In all cases, where the problem appears, `DataReceived` callback in `CommanLine.AsyncStreamReader` would never be fired with `complete == true`.
It occurs immediately after the start of shader compilation, that's why I provide a bunch of the shaders along with project. Can't be sure either it's a race condition between different parts of engine or maybe a deadlock inside resolver.
The only one way to skip the waiting is to kill Unity process and run it again. But it's a completely not a solution for CI/CD environment. 😿

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.