Async/await

Hello there !

I have a PostExport method working perfectly well locally but which seems to silently “fail” on Cloud build.

By fail I mean that the method is correctly executed until the point it encounters an await , then cloud build carries on with the build even if my method if not completed.

Here is the async method used in the PostExport :

private static async Task Upload(string url, string filePath, string token) {
    Debug.Log("Launching upload method, url: " + url + " , filepath: " + filePath + ", token:" + token);
    var fileStream = new FileStream(filePath, FileMode.Open);
    try {
        // We need to use a StreamContent for this to work
        var streamContent = new StreamContent(fileStream);
        streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
            FileName = Path.GetFileName(filePath),
            Name = "file"
        };
        streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
        var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
        var multi = new MultipartFormDataContent {streamContent};
        var response = await client.PostAsync(url, multi);
        if (!response.IsSuccessStatusCode) {
            Debug.Log("An error was returned from the server while uploading");
            Debug.Log(JsonUtility.ToJson(response));
            Debug.LogError(response.StatusCode);
        } else {
            Debug.Log("Asset bundle zip upload completed!");
        }
    } catch (Exception e) {
        Debug.Log("An exception happened while uploading");
        Debug.LogException(e);
        throw;
    } finally {
        Debug.Log("Disposing of file stream (finally block)");
        fileStream.Dispose();
    }
}
        }

The first Debug is working correctly, but I never get any other debug logs (nothing in the catch, nor in the finally block, etc).

The method is called like this :
await Upload(uploadUrl, filePath, token);

It’s working locally, and I’m pretty confident it was working until recently in cloud build.

Fixed it by using

var task = Task.Run(() => client.PostAsync(url, multi));
task.Wait();
var response = task.Result;

instead, but I would expect CloudBuild to recognize an async method and to await it !

2 Likes

For the most part Unity Cloud Build is just a wrapper for batch mode builds which do have specific requirements around execution order and timing. It’s entirely possible that this is a bug in batchmode.

Which version of Unity are you running locally?
Which version of Unity are you running for Unity Cloud Build?
When this worked previously, were you running a different version of Unity in Cloud Build?

1 Like

Hey guys,

could it be that any asynchronous operation in Unity batchmode does not work when having the -quit argument (which UCB obviously uses) since:

-batchmode with -quit does not work because Unity does not run EditorApplication.update and quit after a single frame.
(see: mirrors / Cysharp / UniTask · GitCode and UniTask cannot await during batchmode · Issue #237 · Cysharp/UniTask (github.com))

Cheers