# Race condition when listing devices

**URL:** <https://hub.mender.io/t/race-condition-when-listing-devices/1753>\
**Category:** General Discussions\
**Created:** [April 5, 2020, 4:00pm UTC](https://hub.mender.io/t/race-condition-when-listing-devices/1753 "2020-04-05T16:00:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ngrilly](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/ngrilly/32/468_2.png) [@ngrilly](https://hub.mender.io/u/ngrilly)\
**Post date:** [April 5, 2020, 4:00pm UTC](https://hub.mender.io/t/race-condition-when-listing-devices/1753/1 "2020-04-05T16:00:07Z")

</div>

Hi,

We are using the API to fetch the list of all devices: [https://docs.mender.io/2.3/apis/enterprise/management-apis/device-inventory#devices-get](https://docs.mender.io/2.3/apis/enterprise/management-apis/device-inventory#devices-get)

But it looks there is a race condition due to the design of the pagination mechanism, with the `page` field:

- List of devices is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Process A requests `GET /devices?page=1&per_page=5` and receives 1, 2, 3, 4, 5
- Process B deletes device 5 which is the last device on page 1
- List of devices becomes 1, 2, 3, 4, 6, 7, 8, 9, 10
- Process A requests `GET /devices?page=2&per_page=5` and receives 7, 8, 9, 10: device 6 has been skipped!

Any idea or plan to fix this?

---

<div class="post-metadata">

**Author:** ![mirzak](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/mirzak/32/2056_2.png) [@mirzak](https://hub.mender.io/u/mirzak)\
**Post date:** [April 6, 2020, 7:56am UTC](https://hub.mender.io/t/race-condition-when-listing-devices/1753/2 "2020-04-06T07:56:52Z")

</div>

Hi @ngrilly,

> But it looks there is a race condition due to the design of the pagination mechanism, with the `page` field:

Actually what you are describing is expected behavior. The API calls are independent of each other and when you perform a paged API call you are asking for a **paged view of the current DB** content and it is not depended on previous API call.

- List of devices becomes 1, 2, 3, 4, 6, 7, 8, 9, 10, means
  - `GET /devices?page=1&per_page=5` = 1,2,3,4,6
  - `GET /devices?page=2&per_page=5` = 7,8,9,10

`per_page` works as a limit., that is a maximum number of results per page. This could be clarified in the API docs.

---

<div class="post-metadata">

**Author:** ![ngrilly](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/ngrilly/32/468_2.png) [@ngrilly](https://hub.mender.io/u/ngrilly)\
**Post date:** [April 8, 2020, 11:16am UTC](https://hub.mender.io/t/race-condition-when-listing-devices/1753/3 "2020-04-08T11:16:54Z")

</div>

Hi @mirzak 🙂

Yes, I understand this is the expected behavior with the current API. But it makes impossible to synchronize the list of devices with another external system.

Could we have a paging mechanism similar to what DynamoDB does for example (returning a LastEvaluatedKey which can be used to filter the next request by fetching only the items after LastEvaluatedKey)?

[https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API\_Scan.html](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html)

---

<div class="post-metadata">

**Author:** ![tranchitella](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/tranchitella/32/606_2.png) [@tranchitella](https://hub.mender.io/u/tranchitella)\
**Post date:** [April 8, 2020, 1:00pm UTC](https://hub.mender.io/t/race-condition-when-listing-devices/1753/4 "2020-04-08T13:00:39Z")

</div>

Hello @ngrilly

What you are asking for is not currently available in the device inventory APIs. It is a valuable feature, though, especially in data-sync scenarios. We recorded it in our backlog, and the product owner will prioritize it.

Please be aware that Mender 2.4, which we’ll release early June, implements new APIs for dynamic filtering, using operators (e.g., greater than) tied together with custom sorting and pagination. I believe these new API end-points are going to cover your use-case and allow a loop which emulates stateful pagination.

---

<div class="post-metadata">

**Author:** ![ngrilly](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/ngrilly/32/468_2.png) [@ngrilly](https://hub.mender.io/u/ngrilly)\
**Post date:** [April 12, 2020, 9:57am UTC](https://hub.mender.io/t/race-condition-when-listing-devices/1753/5 "2020-04-12T09:57:27Z")

</div>

> [@tranchitella](#):
>
> new APIs for dynamic filtering, using operators (e.g., greater than) tied together with custom sorting and pagination

Yes, that should solve the issue. Thanks!
