Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle System.InvalidOperationException in case of big body buffer send #220

Merged
merged 11 commits into from
Jan 31, 2023

Conversation

Gsantomaggio
Copy link
Member

@Gsantomaggio Gsantomaggio commented Jan 26, 2023

Fixes #220 and part of #145

How to reproduce the issue:

See #221 (comment)
Important:

  • Server has to be remote
  • the body needs to be bigger than 3k

Main changes:

  • Make the WriteCommand Async:
    private async Task WriteCommand<T>(T command) where T : struct, ICommand
    {
    // Only one thread should be able to write to the output pipeline at a time.
    await writeLock.WaitAsync();
    {
    var size = command.SizeNeeded;
    var mem = new byte[4 + size]; // + 4 to write the size
    WireFormatting.WriteUInt32(mem, (uint)size);
    var written = command.Write(mem.AsSpan()[4..]);
    await writer.WriteAsync(new ReadOnlyMemory<byte>(mem));
    Debug.Assert(size == written);
    await writer.FlushAsync();
    }
    writeLock.Release();
    }

Remove sync Waits (because of https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2012) :

public async ValueTask<bool> Write<T>(T command) where T : struct, ICommand
{
await WriteCommand(command);
// we return true to indicate that the command was written
// In this PR https://github.com/rabbitmq/rabbitmq-stream-dotnet-client/pull/220
// we made all WriteCommand async so await is enough to indicate that the command was written
// We decided to keep the return value to avoid a breaking change
return true;
}

It caused Client timeout Error in case of multi-threads publish

Signed-off-by: Gabriele Santomaggio [email protected]

Signed-off-by: Gabriele Santomaggio <[email protected]>
@codecov
Copy link

codecov bot commented Jan 26, 2023

Codecov Report

Base: 92.33% // Head: 92.33% // No change to project coverage 👍

Coverage data is based on head (b5bfe4c) compared to base (b5bfe4c).
Patch has no changes to coverable lines.

❗ Current head b5bfe4c differs from pull request most recent head f22f814. Consider uploading reports for the commit f22f814 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #220   +/-   ##
=======================================
  Coverage   92.33%   92.33%           
=======================================
  Files          94       94           
  Lines        8262     8262           
  Branches      651      651           
=======================================
  Hits         7629     7629           
  Misses        494      494           
  Partials      139      139           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Signed-off-by: Gabriele Santomaggio <[email protected]>
Signed-off-by: Gabriele Santomaggio <[email protected]>
Signed-off-by: Gabriele Santomaggio <[email protected]>
Signed-off-by: Gabriele Santomaggio <[email protected]>
Copy link
Contributor

@Zerpet Zerpet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some early feedback, I understand the PR is still a WIP 🙂

RabbitMQ.Stream.Client/IProducer.cs Outdated Show resolved Hide resolved
@@ -239,15 +239,11 @@ public static async Task<Client> Create(ClientParameters parameters, ILogger log

public async ValueTask<bool> Publish(Publish publishMsg)
{
var publishTask = Publish<Publish>(publishMsg);
if (!publishTask.IsCompletedSuccessfully)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

private void WriteCommand<T>(T command) where T : struct, ICommand
private async Task WriteCommand<T>(T command) where T : struct, ICommand
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main change.
It makes WriteCommand async using WriteAsync Add FlushAsync instead of sync wait the task

{
_logger.LogWarning("Semaphore Wait timeout during publishing.");
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this Async Await. In some case, it raises semaphorefullexception
after:

_logger.LogWarning("Semaphore Wait timeout during publishing.");

the code continue to work failing the scope of the lock

}

return flushTask.Result.IsCompleted;
await WriteCommand(command);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now everything is async with the await we return true; to leave the API compatibility

Signed-off-by: Gabriele Santomaggio <[email protected]>
@Gsantomaggio Gsantomaggio changed the title work in progress validate frame Handle System.InvalidOperationException in case of big body buffer send Jan 30, 2023
@Gsantomaggio Gsantomaggio marked this pull request as ready for review January 30, 2023 09:33
var publishTask =
_client.Publish(new SubEntryPublish(_publisherId, publishingId,
CompressionHelper.Compress(subEntryMessages, compressionType)));
if (!publishTask.IsCompletedSuccessfully)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zerpet
Zerpet previously approved these changes Jan 30, 2023
@Gsantomaggio Gsantomaggio added this to the 1.1.1 milestone Jan 30, 2023
Copy link
Contributor

@lukebakken lukebakken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, since this is a general-purpose library, every await should have ConfigureAwait(false) - https://devblogs.microsoft.com/dotnet/configureawait-faq/#when-should-i-use-configureawaitfalse

Signed-off-by: Gabriele Santomaggio <[email protected]>
@Gsantomaggio Gsantomaggio modified the milestones: 1.1.1, 1.2 Jan 31, 2023
@Gsantomaggio Gsantomaggio merged commit 7f79681 into main Jan 31, 2023
@Gsantomaggio Gsantomaggio deleted the validate_frame branch January 31, 2023 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants