Unexpected concurrency in async method calls in List initialization [closed]

I wrote a method that populates a list with objects returned by an asynchronous method.

public async Task<ObjectToReturn> GetObjectToReturnAsync()
{
    var objectToReturn = new ObjectToReturn
    {
        List1 = [
            await AsyncMethod1("A", null),
        ],
        List2 = [
            await AsyncMethod1("B", false),
            await AsyncMethod1("C", false),
            await AsyncMethod1("D", false),
            await AsyncMethod1("E", false),
        ],
        List3 = [
            await AsyncMethod1("B", true),
            await AsyncMethod1("C", true),
            await AsyncMethod1("D", true),
            await AsyncMethod1("E", true),
        ]
    };

    return objectToReturn;
}

private async Task<ObjectInList> AsyncMethod1(string status, bool? reopened)
{
    var total = await repository.GetCountByStatusAsync(status, reopened);

    return new ObjectInList(status, total);
}

and here is the method in the repository

public async Task<int> GetCountByStatusAsync(string status, bool? reopened = null)
{
    var query = context.Table1.AsQueryable();
    
    // if (reopened != null)
    //     query = query.Where(c => c.reopened);

    var count = await query.CountAsync(c => c.status == status);
    
    return count;
}

here the exceution log

[2024-09-06T07:14:02.302Z] Executed DbCommand (51ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.303Z] SELECT count(*)::int
[2024-09-06T07:14:02.304Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.305Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.312Z] Executed DbCommand (2ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.313Z] SELECT count(*)::int
[2024-09-06T07:14:02.314Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.314Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.314Z] Executed DbCommand (1ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.316Z] SELECT count(*)::int
[2024-09-06T07:14:02.317Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.318Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.321Z] Executed DbCommand (1ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.322Z] SELECT count(*)::int
[2024-09-06T07:14:02.323Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.324Z] Executed DbCommand (1ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.324Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.328Z] SELECT count(*)::int
[2024-09-06T07:14:02.328Z] Executed DbCommand (2ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.329Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.331Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.331Z] Executed DbCommand (1ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.330Z] SELECT count(*)::int
[2024-09-06T07:14:02.335Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.334Z] Executed DbCommand (1ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.336Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.333Z] SELECT count(*)::int
[2024-09-06T07:14:02.337Z] Executed DbCommand (2ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.338Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.340Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.339Z] SELECT count(*)::int
[2024-09-06T07:14:02.337Z] SELECT count(*)::int
[2024-09-06T07:14:02.342Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.341Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.344Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.344Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.432Z] Error, Self referencing loop detected for property 'target' with type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[ObjectToReturn,Class1+<GetObjectToReturnAsync>d__2]'. Path 'moveNextAction'.
[2024-09-06T07:14:02.464Z] Title                 : Invalid data processException        : Self referencing loop detected for property 'target' with type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[ObjectToReturn,Class1+<GetObjectToReturnAsync>d__2]'. Path 'moveNextAction'. Inner Exception  :Stack Trace      :    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)

it seems that the last calls are not executed sequentially.
Why this problem and how can I solve?

sorry for the first poor post. I mistakenly assumed that the information provided might be sufficient

5

I created a fiddle showing that the methods are executed in order as expected: https://dotnetfiddle.net/PC8Xo0.

The output is:

A 
B False
C False
D False
E False
F True
G True
H True
I True

This demonstrates that the async methods are awaited in the order they are written.

The issue you mentioned might be elsewhere in the code.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật