Skip to content

Eve document count broken with concurrent requests #1271

@ermescs

Description

@ermescs

This issue affects Eve when it is executed either on the Flask's development server with the default multi-threaded configuration, or when deployed in a production server such as Gunicorn using an async worker type (e.g. gevent).

Expected Behavior

For a given GET request, Eve should always return the proper document count in the _meta field, based on the client requested filter.

Actual Behavior

When performing multiple concurrent GET requests, in the scenario described above, there is a high chance of getting wrong results for the document count.

For instance, consider the following resource:

{
    "_id": 1,
    "value": "a"
},
{
    "_id": 2,
    "value": "a"
},
{
    "_id": 3,
    "value": "b"
}

If we perform the following two concurrent requests:

1. GET http://server/resource?where={"value": "a"}
2. GET http://server/resource?where={"value": "b"}

There is a high change of getting the following wrong results:

1.
{
    "_items": [
        {
            "_id": 1,
            "value": "a"
        },
        {
            "_id": 2,
            "value": "a"
        }
    ],
    "_meta": {
        "total": 2
    }
}

2.
{
    "_items": [
        {
            "_id": 3,
            "value": "b"
        }
    ],
    "_meta": {
        "total": 2    <---------------- WRONG!!!!
    }
}

or:

1.
{
    "_items": [
        {
            "_id": 1,
            "value": "a"
        },
        {
            "_id": 2,
            "value": "a"
        }
    ],
    "_meta": {
        "total": 1    <---------------- WRONG!!!!
    }
}

2.
{
    "_items": [
        {
            "_id": 3,
            "value": "b"
        }
    ],
    "_meta": {
        "total": 1
    }
}

This seems to be caused by the changes introduced in 61de6d8 and ac3bd78. With these changes, Eve stores the last used cursor and filter specification in a global object and later uses them to retrieve the document count. This can lead to the above issues when document count is accessed in a concurrent/async way for multiple requests/

Environment

  • Python version: 3.7.0
  • Eve version: 0.9
  • Flask version: 1.0.2
  • Gunicorn version: 19.9.0
  • Gevent version: 1.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions