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

Question: Is it safe to use PrefixingBufferWriter without calling Commit()? #344

Open
rmja opened this issue May 11, 2021 · 3 comments
Open
Labels

Comments

@rmja
Copy link
Contributor

rmja commented May 11, 2021

Hi,

I am using the PrefixingBufferWriter in a context that may throw after bytes have been written to it.
It does not implement IDisposable, yet it seems that it may have a sequence memory pool allocation if writing has started. I can see that the sequence is reset/memory returned when Commit() is called. But, how do I ensure that the allocated sequence is correctly returned to the pool in the case where Commit() is not called on a writer?

@AArnott
Copy link
Collaborator

AArnott commented May 31, 2021

I'm not sure. If an exception were thrown, I would tend to distrust anything in a writing chain. If the exception were thrown by Commit() itself for example, there's no telling how much data was written to the underlying writer. What kind of recovery did you have in mind after a writing exception is thrown?

@rmja
Copy link
Contributor Author

rmja commented Jun 1, 2021

I have something like this:

using var sequnce = SequencePool.Get();
var writer =  new PrefixingBufferWriter(sequence, 4);

// Do something that writes - it may throw

writer.Commit();
sequence.Flush();
...

In this case I do not plan to rely on any of the bytes written to the writer in case that the intermediate writing logic throws, but I would like to have guarantees that the writer does not leak. Does this make sense?

@AArnott
Copy link
Collaborator

AArnott commented Jun 1, 2021

Well, it certainly won't leak but it may not recycle arrays without adding a member that can recycle without trying to write again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants