clone returned byte slice in MarshalValue #1913
Open
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.
No issue number yet, as for now unsure if this is intended behaviour or a bug.
Summary
This PR is not ready for merge. It's more to start a discussion around a behaviour I noticed while upgrading to v2 of the driver.
After the v2 driver upgrade unit-tests failed in our project. I took me a long time to find the issue, but I think the root cause is that 'MarshalValue' hands out byte slices that are potentially shared with the Buffers from the pool. This can lead to nasty race-conditions where the byte slices are modified in our test-setup.
I tried to reproduce the behaviour in a unit-test. But this somehow did not work. However the unit-test is a stripped down version of our unit-test in the project and should give you an idea how this leads to a problem with our tests. I can detail this further of course, if it helps to understand the issue.
The fix is easy in the end. If the byte slice is copied, the problem stops. I can do this on the usage side, but I think this shared behaviour of the returned bytes is very unintuitives and error prone in the edge-cases?
The PR proposes a copy of the bytes before returning them. Not sure that is something that is aligned with goals of the library function.
Background & Motivation
This PR fixes a very unintuitive and hard bug to find while "only" upgrading to v2 of the MongoDB driver. I assume other users of the library could also experience this and a proposal for a fix is up for a discussion.