'What would cause a for / foreach loop to break without explicitly calling a break?

I have a durable function that calls a method that simply adds a row to an efcore object. It doesn't call db save.

When I step through the code, and get to the for loop, it will immediately jump to the line after the for loop. If I step into the call to add the efcore object, and back to the for loop, it continues and loops to the next item. If I press F5 to let it go without debugging, it immediately "breaks" the for loop.

It jumps out of the for loop where i wrote //HERE!!!!!!

I'm pulling my hair out on this one.

obligatory code:

//foreach (stagingFileMap stagingFileMap in fileMaps)
foreach (stagingFileMap stagingFileMap in fileMaps)
{
    if (ActivitySyncHelper.IsSyncCancelled(aso, _configuration))
    {
        break;
    }

    if (!string.IsNullOrEmpty(stagingFileMap.URL))
    {
        // Ensure the url is valid
        try
        {
            string x = await GetTotalBytes(stagingFileMap.URL);
            double.TryParse(x, out double fileByteCount);

            if (fileByteCount > 0)
            {
                // Create or update the video in vimeo
                if (string.IsNullOrEmpty(stagingFileMap.VimeoId))
                {
                    // Azure won't be ready with its backups, so use confex host for video 'get'
                    string title = stagingFileMap.FileName;
                    if (stagingFileMap.FileName.Length > 127)
                    {
                        title = stagingFileMap.FileName.Substring(0, 127);
                    }

                    Video video = vimeoClient.UploadPullLinkAsync(stagingFileMap.URL, title, stagingFileMap.id, meetingIdFolder.Uri).Result;
                    stagingFileMap.VimeoId = video.Id.ToString();
                    stagingFileMap.VimeoId = video.Id.ToString();
                    //HERE!!!!!!
                    await syncLog.WriteInfoMsg($"Vimeo create {stagingFileMap.FileName}");
                    //HERE!!!!!!
                }
                else
                {
                    // Attempt to pull the existing video and update it

                    if (long.TryParse(stagingFileMap.VimeoId, out long videoId))
                    {
                        Video video = vimeoClient.GetVideoAsync(videoId).Result;
                        if (video.Id.HasValue)
                        {
                            Video res = await vimeoClient.UploadPullReplaceAsync(stagingFileMap.URL, video.Id.Value, fileByteCount);
                            await syncLog.WriteInfoMsg($"Vimeo replace {stagingFileMap.FileName} id {res.Id}");
                        }
                    }

                }

                break;
            }

            

        }
        catch (Exception ex)
        {
            // IDK what to do besides skip it and continue
            // log something once logging works
            await syncLog.WriteErrorMsg(aso, ex.Message);
            await syncLog.Save();
            continue;
        }

        // We need to save here requently because if there is big error, all the work syncing to vimeo will be desync with the DB
        dbContext.Update(stagingFileMap);
        await dbContext.SaveChangesAsync();
        await syncLog.Save();
    }
}
await dbContext.DisposeAsync();
public async Task WriteInfoMsg( string msg)
{
    SyncAttemptDetail sad = new()
    {
        SyncAttemptId = _id,
        Message = msg,
        MsgLevel = SyncAttemptMessageLevel.Info,
        AddDate = DateTime.UtcNow,
        AddUser = "SYSTEM"
    };
    await _dbContext.SyncAttemptDetail.AddAsync(sad);
}


Solution 1:[1]

I'm dumb. Theres LITERALLY a break command in there.

Solution 2:[2]

The await will create a task and immediately return it (the debugger will follow this path too). The loop will continue in the task.

To attach the debugger to the task, add a break point after the await.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CarComp
Solution 2 Orace