Skip to content

Commit

Permalink
Fix killCursors edge case (#3030)
Browse files Browse the repository at this point in the history
Closes #1514.
  • Loading branch information
AlekSi committed Jul 12, 2023
1 parent f5deeb7 commit f38331f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
10 changes: 4 additions & 6 deletions integration/commands_administration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1202,10 +1202,8 @@ func TestCommandsAdministrationKillCursors(t *testing.T) {
AssertEqualDocuments(t, expected, actual)
})

t.Run("WrongType", func(tt *testing.T) {
tt.Parallel()

t := setup.FailsForFerretDB(tt, "https://github.com/FerretDB/FerretDB/issues/1514")
t.Run("WrongType", func(t *testing.T) {
t.Parallel()

c, err := collection.Find(ctx, bson.D{}, options.Find().SetBatchSize(1))
require.NoError(t, err)
Expand All @@ -1215,12 +1213,12 @@ func TestCommandsAdministrationKillCursors(t *testing.T) {
var actual bson.D
err = collection.Database().RunCommand(ctx, bson.D{
{"killCursors", collection.Name()},
{"cursors", bson.A{c.ID(), "foo"}},
{"cursors", bson.A{c.ID(), int32(100500)}},
}).Decode(&actual)
expectedErr := mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "BSON field 'killCursors.cursors.1' is the wrong type 'string', expected type 'long'",
Message: "BSON field 'killCursors.cursors.1' is the wrong type 'int', expected type 'long'",
}
AssertEqualCommandError(t, expectedErr, err)

Expand Down
5 changes: 5 additions & 0 deletions internal/handlers/common/killcursors.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func KillCursors(ctx context.Context, msg *wire.OpMsg, registry *cursor.Registry
iter := cursors.Iterator()
defer iter.Close()

var ids []int64
cursorsKilled := types.MakeArray(0)
cursorsNotFound := types.MakeArray(0)
cursorsAlive := types.MakeArray(0)
Expand Down Expand Up @@ -87,6 +88,10 @@ func KillCursors(ctx context.Context, msg *wire.OpMsg, registry *cursor.Registry
)
}

ids = append(ids, id)
}

for _, id := range ids {
cursor := registry.Get(id)
if cursor == nil || cursor.DB != db || cursor.Collection != collection || cursor.Username != username {
cursorsNotFound.Append(id)
Expand Down

0 comments on commit f38331f

Please sign in to comment.