[GODRIVER-3009] Fix encoding panic when encoding error fields Created: 11/Oct/23  Updated: 05/Dec/23  Resolved: 30/Nov/23

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: 1.13.0, 1.12.2
Fix Version/s: 1.13.1

Type: Bug Priority: Major - P3
Reporter: Preston Vasquez Assignee: Qingyang Hu
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
backports GODRIVER-3051 Panic when decoding is done concurrently Closed
Depends
is depended on by GODRIVER-2943 Fix failing logging tests Closed
Duplicate
is duplicated by GODRIVER-3051 Panic when decoding is done concurrently Closed
Related
is related to GODRIVER-3051 Panic when decoding is done concurrently Closed
Quarter: FY24Q4
Case:
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

When concurrently encoding structs with an exported "error" field, the following panic may occur:

panic: interface conversion: interface is nil, not bsoncodec.ValueEncoder
 
goroutine 35 [running]:
go.mongodb.org/mongo-driver/bson/bsoncodec.(*typeEncoderCache).LoadOrStore(...)
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/bsoncodec/codec_cache.go:45
go.mongodb.org/mongo-driver/bson/bsoncodec.(*Registry).storeTypeEncoder(0x14000144c40?, {0x1043ae578?, 0x1043919c0?}, {0x0?, 0x0?})
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/bsoncodec/registry.go:416 +0x7c
go.mongodb.org/mongo-driver/bson/bsoncodec.(*Registry).LookupEncoder(0x14000144c40, {0x1043ae578, 0x1043919c0})
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/bsoncodec/registry.go:411 +0x1f4
go.mongodb.org/mongo-driver/bson/bsoncodec.(*StructCodec).describeStructSlow(0x14000132100, 0x1043a67c0?, {0x1043ae578, 0x104392640}, 0x0, 0x0)
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/bsoncodec/struct_codec.go:515 +0x1fc
go.mongodb.org/mongo-driver/bson/bsoncodec.(*StructCodec).describeStruct(0x14000132100, 0x1?, {0x1043ae578?, 0x104392640}, 0xd8?, 0x2b?)
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/bsoncodec/struct_codec.go:483 +0xa0
go.mongodb.org/mongo-driver/bson/bsoncodec.(*StructCodec).EncodeValue(0x14000132100, {0x14000144c40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, ...)
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/bsoncodec/struct_codec.go:145 +0xc8
go.mongodb.org/mongo-driver/bson.(*Encoder).Encode(0x140000a6030, {0x104392640?, 0x1400008e020})
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/encoder.go:119 +0x264
go.mongodb.org/mongo-driver/bson.MarshalAppendWithContext({0x14000144c40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, {0x1044a76a0, ...}, ...)
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/marshal.go:208 +0x1f4
go.mongodb.org/mongo-driver/bson.MarshalAppendWithRegistry(...)
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/marshal.go:142
go.mongodb.org/mongo-driver/bson.MarshalWithRegistry(0x14000144c40, {0x104392640, 0x1400008e020})
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/marshal.go:96 +0x84
go.mongodb.org/mongo-driver/bson.Marshal(...)
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/marshal.go:52
main.main.func1()
        /Users/preston.vasquez/Developer/technical/workshop/mongo/bson/weirerrorthing/main.go:15 +0x80
created by main.main
        /Users/preston.vasquez/Developer/technical/workshop/mongo/bson/weirerrorthing/main.go:13 +0x3c
exit status 2

Detailed steps to reproduce the problem?

package main
 
import (
	"sync"
 
	"go.mongodb.org/mongo-driver/bson"
)
 
func main() {
	wg := sync.WaitGroup{}
	wg.Add(10_000)
	for i := 0; i < 10_000; i++ {
		go func() {
			defer wg.Done()
			bson.Marshal(struct{ LastError error }{})
		}()
	}
        wg.Wait()
}

Definition of done: what must be done to consider the task complete?

Fix encoding issue, no more panics

The exact Go version used, with patch level:

go version go1.20.5

The exact version of the Go driver used:

v1 development branch

The operating system and version (e.g. Windows 7, OSX 10.8, ...)

darwin/arm64

Security Vulnerabilities

NA



 Comments   
Comment by Githook User [ 05/Dec/23 ]

Author:

{'name': 'Preston Vasquez', 'email': 'prestonvasquez@icloud.com', 'username': 'prestonvasquez'}

Message: GODRIVER-3009 Fix concurrent panic in struct codec. (#1477) (#1489)

Co-authored-by: Qingyang Hu <103950869+qingyang-hu@users.noreply.github.com>
(cherry picked from commit 3adad2b18ecd3aed7a6937547d5af3f14645b786)
Branch: release/1.13
https://github.com/mongodb/mongo-go-driver/commit/c14556a79af33a343cd2a29a5f27ae199596f967

Comment by Githook User [ 30/Nov/23 ]

Author:

{'name': 'Qingyang Hu', 'email': '103950869+qingyang-hu@users.noreply.github.com', 'username': 'qingyang-hu'}

Message: GODRIVER-3009 Fix concurrent panic in struct codec. (#1477)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/868e9c0054472826699c252ed16ab3cf12156edd

Generated at Thu Feb 08 08:39:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.