vaphes / pocketbase

PocketBase client SDK for python
https://pypi.org/project/pocketbase/
MIT License
331 stars 38 forks source link

Issues deleting keys while iterating a list of dictionary #76

Closed FrontiniFederico closed 7 months ago

FrontiniFederico commented 9 months ago

Problem Description

While iterating through a list of dictionaries, rarely and very randomly a ClientResponseError gets raised. From Pocketbase logs I can see that the following code somewhat tries to delete the same key twice, resulting in this issue. It does seem a kind of race condition of some sort?

Steps to Reproduce

I have not much to say here, I can paste the code that results in this error but it's very sporadic:


filter: dict = {"filter": f'SomeKey = "SomeValue"', "sort": "-LastSeen"}
query_response = pb_client.collection("Some_collection").get_full_list(query_params=filter)
trimmed_response: list[dict] = []
for element in query_response:
    temp: dict = {}
    temp["id"] = element.id
    temp["LastSeen"] = element.last_seen
    trimmed_response.append(temp)
for element in trimmed_response[1:]:
    pb_client.collection("Some_collection").delete(element["id"])

Expected Behavior

I wouldn't expect this code to randomly try to delete the same key twice.

Current Behavior

Somehow, it does. I think that #18 tries to fix this?

Context

m29h commented 9 months ago

Hi, can you put in a check directly after building your trimmed_response list and before executing your "delete" loop in order to ensure that your trimmed_response does not include any duplicates in the first place. for testing purpose you could add something quick-and-dirty like:

duplicates = [
    x
    for x in trimmed_response
    if [e["id"] for e in trimmed_response].count(x["id"]) >= 2
]
if duplicates:
    print("Error: There are the following duplicate IDs: ", duplicates)

The #18 you refer to relates to a fix in the realtime service which is not relevant here as you use the normal record service.

Are you sure that you are the only user of your pocketbase instance? If someone else is concurrently deleting elements from the pocketbase every once in a while while your code excecutes the behaviour you see would be somehow "expected".

The code being executed within this library to delete a record is rather straightforward and well covered by our library integration tests. In that sense you should double-check if your problem is not related to your specific context and/or environment somehow.

m29h commented 7 months ago

closed due to inactivity