Skip to content

Commit 4f9c841

Browse files
committed
Merge branch 'master' into godriver2388v2
2 parents ce3ed92 + 17a547f commit 4f9c841

File tree

196 files changed

+1810
-7563
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

196 files changed

+1810
-7563
lines changed

.evergreen/config.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,14 @@ functions:
388388
TASKFILE_TARGET: test-short
389389
args: [*task-runner, run-docker]
390390

391+
run-goleak-test:
392+
- command: subprocess.exec
393+
type: test
394+
params:
395+
binary: "bash"
396+
include_expansions_in_env: ["MONGODB_URI"]
397+
args: [*task-runner, test-goleak]
398+
391399
run-ocsp-server:
392400
- command: subprocess.exec
393401
params:
@@ -1100,6 +1108,26 @@ tasks:
11001108
- func: bootstrap-mongo-orchestration
11011109
- func: run-docker-test
11021110

1111+
- name: test-goroutine-leaks-replicaset
1112+
tags: ["goleak"]
1113+
commands:
1114+
- func: bootstrap-mongo-orchestration
1115+
vars:
1116+
TOPOLOGY: "replica_set"
1117+
AUTH: "noauth"
1118+
SSL: "nossl"
1119+
- func: run-goleak-test
1120+
1121+
- name: test-goroutine-leaks-sharded
1122+
tags: ["goleak"]
1123+
commands:
1124+
- func: bootstrap-mongo-orchestration
1125+
vars:
1126+
TOPOLOGY: "sharded_cluster"
1127+
AUTH: "noauth"
1128+
SSL: "nossl"
1129+
- func: run-goleak-test
1130+
11031131
- name: test-load-balancer-noauth-nossl
11041132
tags: ["load-balancer"]
11051133
commands:
@@ -2056,6 +2084,16 @@ buildvariants:
20562084
tasks:
20572085
- name: "test-docker-runner"
20582086

2087+
- name: goroutine-leaks-test
2088+
tags: ["pullrequest"]
2089+
display_name: "Goroutine Leaks Test"
2090+
run_on:
2091+
- ubuntu2204-large
2092+
expansions:
2093+
GO_DIST: "/opt/golang/go1.22"
2094+
tasks:
2095+
- name: ".goleak"
2096+
20592097
- matrix_name: "tests-rhel-36-with-zlib-support"
20602098
tags: ["pullrequest"]
20612099
matrix_spec: { version: ["3.6"], os-ssl-32: ["rhel87-64"] }

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Dockerfile for Go Driver local development.
22

33
# Build libmongocrypt in a separate build stage.
4-
FROM ubuntu:20.04 as libmongocrypt
4+
FROM artifactory.corp.mongodb.com/dockerhub/ubuntu:20.04 as libmongocrypt
55

66
RUN apt-get -qq update && \
77
apt-get -qqy install --no-install-recommends \
@@ -17,7 +17,7 @@ RUN cd /root && bash ./install-libmongocrypt.sh
1717

1818

1919
# Copy in the files from the libmongocrypt build stage.
20-
FROM ubuntu:20.04
20+
FROM artifactory.corp.mongodb.com/dockerhub/ubuntu:20.04
2121

2222
# Install common deps.
2323
RUN export DEBIAN_FRONTEND=noninteractive && \

Taskfile.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ tasks:
105105

106106
test-gcpkms: bash etc/run-gcpkms-test.sh
107107

108+
test-goleak: bash etc/run-goleak-test.sh
109+
108110
### Local FaaS tasks. ###
109111
build-faas-awslambda:
110112
requires:

bson/decoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (d *Decoder) SetRegistry(r *Registry) {
8787
d.dc.Registry = r
8888
}
8989

90-
// DefaultDocumentM causes the Decoder to always unmarshal documents into the primitive.M type. This
90+
// DefaultDocumentM causes the Decoder to always unmarshal documents into the bson.M type. This
9191
// behavior is restricted to data typed as "interface{}" or "map[string]interface{}".
9292
func (d *Decoder) DefaultDocumentM() {
9393
d.dc.defaultDocumentType = reflect.TypeOf(M{})
@@ -101,7 +101,7 @@ func (d *Decoder) AllowTruncatingDoubles() {
101101
}
102102

103103
// BinaryAsSlice causes the Decoder to unmarshal BSON binary field values that are the "Generic" or
104-
// "Old" BSON binary subtype as a Go byte slice instead of a primitive.Binary.
104+
// "Old" BSON binary subtype as a Go byte slice instead of a bson.Binary.
105105
func (d *Decoder) BinaryAsSlice() {
106106
d.dc.binaryAsSlice = true
107107
}

bson/decoder_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,8 @@ func TestDecoderConfiguration(t *testing.T) {
647647

648648
var got objectIDTest
649649
err := dec.Decode(&got)
650-
assert.EqualError(t, err, "error decoding key id: decoding an object ID to a non-hexadecimal string representation is not supported")
650+
const want = "error decoding key id: decoding an object ID into a string is not supported by default (set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal string)"
651+
assert.EqualError(t, err, want)
651652
})
652653
t.Run("DefaultDocumentM top-level", func(t *testing.T) {
653654
t.Parallel()

bson/doc.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@
4747
// 5. BSON boolean unmarshals to a bool.
4848
// 6. BSON embedded document unmarshals to the parent type (i.e. D for a D, M for an M).
4949
// 7. BSON array unmarshals to a bson.A.
50-
// 8. BSON ObjectId unmarshals to a primitive.ObjectID.
51-
// 9. BSON datetime unmarshals to a primitive.DateTime.
52-
// 10. BSON binary unmarshals to a primitive.Binary.
53-
// 11. BSON regular expression unmarshals to a primitive.Regex.
54-
// 12. BSON JavaScript unmarshals to a primitive.JavaScript.
55-
// 13. BSON code with scope unmarshals to a primitive.CodeWithScope.
56-
// 14. BSON timestamp unmarshals to an primitive.Timestamp.
57-
// 15. BSON 128-bit decimal unmarshals to an primitive.Decimal128.
58-
// 16. BSON min key unmarshals to an primitive.MinKey.
59-
// 17. BSON max key unmarshals to an primitive.MaxKey.
60-
// 18. BSON undefined unmarshals to a primitive.Undefined.
50+
// 8. BSON ObjectId unmarshals to a bson.ObjectID.
51+
// 9. BSON datetime unmarshals to a bson.DateTime.
52+
// 10. BSON binary unmarshals to a bson.Binary.
53+
// 11. BSON regular expression unmarshals to a bson.Regex.
54+
// 12. BSON JavaScript unmarshals to a bson.JavaScript.
55+
// 13. BSON code with scope unmarshals to a bson.CodeWithScope.
56+
// 14. BSON timestamp unmarshals to an bson.Timestamp.
57+
// 15. BSON 128-bit decimal unmarshals to an bson.Decimal128.
58+
// 16. BSON min key unmarshals to an bson.MinKey.
59+
// 17. BSON max key unmarshals to an bson.MaxKey.
60+
// 18. BSON undefined unmarshals to a bson.Undefined.
6161
// 19. BSON null unmarshals to nil.
62-
// 20. BSON DBPointer unmarshals to a primitive.DBPointer.
63-
// 21. BSON symbol unmarshals to a primitive.Symbol.
62+
// 20. BSON DBPointer unmarshals to a bson.DBPointer.
63+
// 21. BSON symbol unmarshals to a bson.Symbol.
6464
//
6565
// The above mappings also apply when marshaling a D or M to BSON. Some other useful marshaling mappings are:
6666
//

bson/primitive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (d DateTime) MarshalJSON() ([]byte, error) {
6060
return d.Time().UTC().MarshalJSON()
6161
}
6262

63-
// UnmarshalJSON creates a primitive.DateTime from a JSON string.
63+
// UnmarshalJSON creates a bson.DateTime from a JSON string.
6464
func (d *DateTime) UnmarshalJSON(data []byte) error {
6565
// Ignore "null" so that we can distinguish between a "null" value and
6666
// valid value that is the zero time (as reported by time.Time.IsZero).

bson/string_codec.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package bson
88

99
import (
10+
"errors"
1011
"fmt"
1112
"reflect"
1213
)
@@ -50,14 +51,16 @@ func (sc *stringCodec) decodeType(dc DecodeContext, vr ValueReader, t reflect.Ty
5051
return emptyValue, err
5152
}
5253
case TypeObjectID:
53-
oid, err := vr.ReadObjectID()
54-
if err != nil {
55-
return emptyValue, err
56-
}
5754
if dc.objectIDAsHexString {
55+
oid, err := vr.ReadObjectID()
56+
if err != nil {
57+
return emptyValue, err
58+
}
5859
str = oid.Hex()
5960
} else {
60-
return emptyValue, fmt.Errorf("decoding an object ID to a non-hexadecimal string representation is not supported")
61+
const msg = "decoding an object ID into a string is not supported by default " +
62+
"(set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal string)"
63+
return emptyValue, errors.New(msg)
6164
}
6265
case TypeSymbol:
6366
str, err = vr.ReadSymbol()

docs/migration-2.0.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,34 @@ The `NewRegistryBuilder` function has been removed along with the `bsoncodec.Reg
867867

868868
### Decoder
869869

870+
The BSON decoding logic has changed to decode into a `bson.D` by default.
871+
872+
The example shows the behavior change.
873+
874+
```go
875+
// v1
876+
877+
b1 := bson.M{"a": 1, "b": bson.M{"c": 2}}
878+
b2, _ := bson.Marshal(b1)
879+
b3 := bson.M{}
880+
bson.Unmarshal(b2, &b3)
881+
fmt.Printf("b3.b type: %T\n", b3["b"])
882+
// Output: b3.b type: primitive.M
883+
```
884+
885+
```go
886+
// v2
887+
888+
b1 := bson.M{"a": 1, "b": bson.M{"c": 2}}
889+
b2, _ := bson.Marshal(b1)
890+
b3 := bson.M{}
891+
bson.Unmarshal(b2, &b3)
892+
fmt.Printf("b3.b type: %T\n", b3["b"])
893+
// Output: b3.b type: bson.D
894+
```
895+
896+
Use `Decoder.DefaultDocumentM()` or set the `DefaultDocumentM` field of `options.BSONOptions` to always decode documents into the `bson.M` type.
897+
870898
#### NewDecoder
871899

872900
The signature of `NewDecoder` has been updated without an error being returned.
@@ -1043,11 +1071,11 @@ The signature of `Reset` has been updated without an error being returned.
10431071

10441072
#### DefaultDocumentD / DefaultDocumentM
10451073

1046-
`Decoder.DefaultDocumentD` has been removed since a document, including a top-level value (e.g. you pass in an empty interface value to Decode), is always decoded into a `bson.D` by default. Therefore, use `Decoder.DefaultDocumentM` to always decode a document into a `bson.M` to avoid unexpected decode results.
1074+
`Decoder.DefaultDocumentD()` has been removed since a document, including a top-level value (e.g. you pass in an empty interface value to Decode), is always decoded into a `bson.D` by default. Therefore, use `Decoder.DefaultDocumentM()` to always decode a document into a `bson.M` to avoid unexpected decode results.
10471075

10481076
#### ObjectIDAsHexString
10491077

1050-
`Decoder.ObjectIDAsHexString` method enables decoding a BSON ObjectId as a hexadecimal string. Otherwise, the decoder returns an error by default instead of decoding as the UTF-8 representation of the raw ObjectId bytes, which results in a garbled and unusable string.
1078+
`Decoder.ObjectIDAsHexString()` method enables decoding a BSON ObjectId as a hexadecimal string. Otherwise, the decoder returns an error by default instead of decoding as the UTF-8 representation of the raw ObjectId bytes, which results in a garbled and unusable string.
10511079

10521080
### Encoder
10531081

@@ -1183,8 +1211,20 @@ A new `RawArray` type has been added to the `bson` package as a primitive type t
11831211

11841212
### ValueMarshaler
11851213

1186-
The `MarshalBSONValue` method of the `ValueMarshaler` interface is only required to return a byte type value representing the BSON type to avoid importing the `bsontype` package.
1214+
The `MarshalBSONValue` method of the [ValueMarshaler](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#ValueMarshaler) interface now returns a `byte` value representing the [BSON type](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#Type). That allows external packages to implement the `ValueMarshaler` interface without having to import the `bson` package. Convert a returned `byte` value to [bson.Type](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#Type) to compare with the BSON type constants. For example:
1215+
1216+
```go
1217+
btype, _, _ := m.MarshalBSONValue()
1218+
fmt.Println("type of data: %s: ", bson.Type(btype))
1219+
fmt.Println("type of data is an array: %v", bson.Type(btype) == bson.TypeArray)
1220+
```
11871221

11881222
### ValueUnmarshaler
11891223

1190-
The `UnmarshalBSONValue` method of the `ValueUnmarshaler` interface is only required to take a byte type argument representing the BSON type to avoid importing the Go driver package.
1224+
The `UnmarshalBSONValue` method of the [ValueUnmarshaler](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#ValueUnmarshaler) interface now accepts a `byte` value representing the [BSON type](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#Type) for the first argument. That allows packages to implement `ValueUnmarshaler` without having to import the `bson` package. For example:
1225+
1226+
```go
1227+
if err := m.UnmarshalBSONValue(bson.TypeEmbeddedDocument, bytes); err != nil {
1228+
log.Fatalf("failed to decode embedded document: %v", err)
1229+
}
1230+
```

etc/run-goleak-test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
# run-goleak-test
3+
# Run goroutine leak tests.
4+
set -eu
5+
set +x
6+
7+
echo "Running internal/test/goleak"
8+
pushd internal/test/goleak
9+
go test -v ./... >> ../../../test.suite
10+
popd

0 commit comments

Comments
 (0)