Skip to content

Commit 8380333

Browse files
author
Andrii Bondarchuk
committed
Increase upsert queue size, fix stream disposal timing
Refactored MemoryStream handling during bulk upserts to ensure streams are disposed only after the Cosmos DB SDK completes reading, preventing premature disposal and potential errors.
1 parent 42d8f73 commit 8380333

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/FitSyncHub.Functions/Functions/EverestingHOFScraperHttpTriggerFunction.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,37 @@ private async Task<List<UpsertResult>> UpsertActivities(
214214
foreach (var activity in activities.EnumerateArray())
215215
{
216216
// Convert JsonElement → stream
217-
await using var stream = new MemoryStream();
217+
var stream = new MemoryStream(); //do not dispose, will be disposed after SDK read
218218
await using (var writer = new Utf8JsonWriter(stream))
219219
{
220220
activity.WriteTo(writer);
221221
}
222222

223223
stream.Position = 0;
224224

225-
// Upsert in bulk
226-
tasks.Add(_everestingHOFContainer.UpsertItemStreamAsync(
227-
stream,
228-
new PartitionKey(activity.GetProperty("id").GetString()),
229-
cancellationToken: cancellationToken)
230-
);
225+
var id = activity.GetProperty("id").GetString()!;
226+
227+
// Upsert in bulk — SDK may read/clone the stream asynchronously, so do not dispose it here
228+
tasks.Add(TaskToUpsertItemAndCloseStream(stream, id, cancellationToken));
229+
230+
async Task<ResponseMessage> TaskToUpsertItemAndCloseStream(MemoryStream stream, string id, CancellationToken cancellationToken)
231+
{
232+
try
233+
{
234+
return await _everestingHOFContainer.UpsertItemStreamAsync(
235+
stream,
236+
new PartitionKey(id),
237+
cancellationToken: cancellationToken);
238+
}
239+
catch (Exception)
240+
{
241+
throw;
242+
}
243+
finally
244+
{
245+
await stream.DisposeAsync();
246+
}
247+
}
231248
}
232249

233250
var result = new List<UpsertResult>();

0 commit comments

Comments
 (0)