Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-3009

Fix encoding panic when encoding error fields

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.13.1
    • Affects Version/s: 1.13.0, 1.12.2
    • Component/s: None
    • Labels:
      None
    • Not Needed
    • Hide

      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?

      Show
      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?

      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

            Assignee:
            qingyang.hu@mongodb.com Qingyang Hu
            Reporter:
            preston.vasquez@mongodb.com Preston Vasquez
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: