Remove per-message Task.Run from Service Bus deserialization - #1379
Open
Bernd Verst (berndverst) wants to merge 3 commits into
Open
Bernd Verst (berndverst) wants to merge 3 commits into
Bernd Verst (berndverst) wants to merge 3 commits into
Conversation
Return completed stream tasks for inline message bodies while retaining asynchronous blob-store loading. Add cross-target coverage for round trips, synchronous completion, and stream ownership. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 1347830a-17c5-4248-8e41-fee30d3066b4
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes per-message Task.Run scheduling from inline Service Bus message stream loading by returning already-completed Task<Stream> instances, reducing thread-pool work-item bursts during batch deserialization.
Changes:
- Replace
Task.Run(...)withTask.FromResult(...)for inline message bodies inServiceBusUtils.LoadMessageStreamAsync(bothNETSTANDARD2_0andnet48branches). - Add new unit tests intended to validate inline (compressed/uncompressed) round trips, synchronous completion for uncompressed inline messages, and async blob-store behavior/stream ownership for external messages.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/DurableTask.ServiceBus/Common/ServiceBusUtils.cs |
Removes thread-pool hop for inline message-body stream creation by returning completed tasks. |
Test/DurableTask.ServiceBus.Tests/ServiceBusUtilsTests.cs |
Adds tests for inline/external deserialization behaviors, but currently placed in a directory not used by the active test project. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Move ServiceBusUtilsTests into the lowercase test project directory and ensure the external stream is disposed even if the test fails. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot started reviewing on behalf of
Bernd Verst (berndverst)
September 30, 2026 00:36
View session
Copilot started reviewing on behalf of
Bernd Verst (berndverst)
September 30, 2026 00:39
View session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Task<Stream>for inline Service Bus message bodies on bothnetstandard2.0andnet48MemoryStream/GetBody<Stream>()behavior and leave external blob-store loading asynchronousMotivation and benefits
Inline Service Bus message bodies are already in memory. Previously, deserialization used
Task.Runjust to wrap those bytes in aMemoryStreamor retrieve the message's body stream. That operation performs no asynchronous I/O, so dispatching it to the thread pool adds a queued work item and scheduling overhead without making the underlying work asynchronous.Returning
Task.FromResultremoves that unnecessary thread-pool hop for every inline message. This is especially useful when processing batches or prefetched messages: instead of queuing a burst of small work items, the caller can immediately continue with the available stream. The expected benefit is less scheduling overhead and thread-pool contention during message deserialization. This PR does not include throughput or latency benchmarks, so it does not claim a measured end-to-end performance improvement.Compatibility
The private
Task<Stream>contract is unchanged, as are the inline stream contents, initial position, and disposal lifetime. Messages stored externally still returnLoadStreamAsyncdirectly, where asynchronous I/O is needed. Compressed inline messages also avoid the stream-loading hop, but decompression may still complete asynchronously.Tests
The tests live alongside the active project under lowercase
test/DurableTask.ServiceBus.Tests, so they are included on case-sensitive filesystems as well as Windows. The external-stream ownership assertion runs inside ausingscope, preserving the check that deserialization disposes the stream while guaranteeing cleanup if the test fails.dotnet test test\DurableTask.ServiceBus.Tests\DurableTask.ServiceBus.Tests.csproj -f net8.0 --filter "FullyQualifiedName~ServiceBusUtilsTests"dotnet test test\DurableTask.ServiceBus.Tests\DurableTask.ServiceBus.Tests.csproj -f net48 --filter "FullyQualifiedName~ServiceBusUtilsTests"Fixes #1376