Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Async/await

Discussion in 'Unity Build Automation' started by RDeluxe, Oct 16, 2019.

  1. RDeluxe

    RDeluxe

    Joined:
    Sep 29, 2013
    Posts:
    115
    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 :

    Code (CSharp):
    1.  
    2.  
    3. private static async Task Upload(string url, string filePath, string token) {
    4.     Debug.Log("Launching upload method, url: " + url + " , filepath: " + filePath + ", token:" + token);
    5.     var fileStream = new FileStream(filePath, FileMode.Open);
    6.     try {
    7.         // We need to use a StreamContent for this to work
    8.         var streamContent = new StreamContent(fileStream);
    9.         streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
    10.             FileName = Path.GetFileName(filePath),
    11.             Name = "file"
    12.         };
    13.         streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
    14.         var client = new HttpClient();
    15.         client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    16.         var multi = new MultipartFormDataContent {streamContent};
    17.         var response = await client.PostAsync(url, multi);
    18.         if (!response.IsSuccessStatusCode) {
    19.             Debug.Log("An error was returned from the server while uploading");
    20.             Debug.Log(JsonUtility.ToJson(response));
    21.             Debug.LogError(response.StatusCode);
    22.         } else {
    23.             Debug.Log("Asset bundle zip upload completed!");
    24.         }
    25.     } catch (Exception e) {
    26.         Debug.Log("An exception happened while uploading");
    27.         Debug.LogException(e);
    28.         throw;
    29.     } finally {
    30.         Debug.Log("Disposing of file stream (finally block)");
    31.         fileStream.Dispose();
    32.     }
    33. }
    34.         }
    35.  
    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.
     
  2. RDeluxe

    RDeluxe

    Joined:
    Sep 29, 2013
    Posts:
    115
    Fixed it by using

    Code (CSharp):
    1. var task = Task.Run(() => client.PostAsync(url, multi));
    2. task.Wait();
    3. var response = task.Result;
    instead, but I would expect CloudBuild to recognize an async method and to await it !
     
    StormChaserDev and Can-Baycay like this.
  3. victorw

    victorw

    Unity Technologies

    Joined:
    Sep 14, 2016
    Posts:
    459
    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?
     
    RDeluxe likes this.
  4. StormChaserDev

    StormChaserDev

    Joined:
    Jan 18, 2018
    Posts:
    13