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

GODRIVER-3009 Fix concurrent panic in struct codec. #1477

Merged
merged 6 commits into from
Nov 30, 2023

Conversation

qingyang-hu
Copy link
Collaborator

@qingyang-hu qingyang-hu commented Nov 21, 2023

GODRIVER-3009

Summary

Remove the unnecessary nil cache, so nil encoder/decoder will not be stored.

Background & Motivation

The original panic was:

interface conversion: interface is nil, not bsoncodec.ValueEncoder

from bson/bsoncodec/codec_cache.go:45

The root cause is at https://github.com/mongodb/mongo-go-driver/blob/master/bson/bsoncodec/struct_codec.go#L481

Copy link

API Change Report

No changes found!

@qingyang-hu qingyang-hu changed the title GODRIVER-3009 Fix nil encoder/decoder GODRIVER-3009 Fix concurrent panic in struct codec Nov 22, 2023
@qingyang-hu qingyang-hu changed the title GODRIVER-3009 Fix concurrent panic in struct codec GODRIVER-3009 Fix concurrent panic in struct codec. Nov 22, 2023
@qingyang-hu qingyang-hu marked this pull request as ready for review November 22, 2023 17:44
@qingyang-hu qingyang-hu requested a review from a team as a code owner November 22, 2023 17:44
@qingyang-hu qingyang-hu requested review from prestonvasquez and matthewdale and removed request for a team November 22, 2023 17:44
Copy link
Collaborator

@prestonvasquez prestonvasquez left a comment

Choose a reason for hiding this comment

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

We need to add the following test case to TestRegistryBuilder/Register/Lookup:

				{
					"nil",
					nil,
					nil,
					ErrNilType,
					false,
				},

We might also add a test that captures the repro case in bson package:

func TestConcurrentEncoding(t *testing.T) {
	t.Parallel()

	wg := sync.WaitGroup{}
	wg.Add(10_000)
	for i := 0; i < 10_000; i++ {
		go func() {
			defer wg.Done()
			Marshal(struct{ LastError error }{})
		}()
	}

	wg.Wait()
}

The latter might be caught by the race detector, but it couldn't hurt to have it around. What are your thoughts?

@@ -388,6 +388,9 @@ func (r *Registry) RegisterTypeMapEntry(bt bsontype.Type, rt reflect.Type) {
// If no encoder is found, an error of type ErrNoEncoder is returned. LookupEncoder is safe for
// concurrent use by multiple goroutines after all codecs and encoders are registered.
func (r *Registry) LookupEncoder(valueType reflect.Type) (ValueEncoder, error) {
if valueType == nil {
return nil, ErrNoEncoder{Type: valueType}
Copy link
Collaborator

Choose a reason for hiding this comment

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

The LookupEncoder and LookupDecoder return values should match when valueType is nil. I think it makes sense to return ErrNilType here rather than ErrNoEncoder

Suggested change
return nil, ErrNoEncoder{Type: valueType}
return nil, ErrNilType

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree. However, it will be a breaking change. How do you think about filing a v2.0 ticket instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The error message for ErrNilType is

"cannot perform a decoder lookup on <nil>"

We'll need to update the error message to make sense for either case.

Copy link
Collaborator

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@qingyang-hu qingyang-hu merged commit 868e9c0 into mongodb:master Nov 30, 2023
37 checks passed
prestonvasquez pushed a commit to prestonvasquez/mongo-go-driver that referenced this pull request Dec 5, 2023
prestonvasquez added a commit that referenced this pull request Dec 5, 2023
Co-authored-by: Qingyang Hu <103950869+qingyang-hu@users.noreply.github.com>
matthewdale pushed a commit that referenced this pull request Dec 5, 2023
Co-authored-by: Qingyang Hu <103950869+qingyang-hu@users.noreply.github.com>
(cherry picked from commit 3adad2b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants