You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At concurrent calling of get_or_create from different places of a program we get the same node which was created once
Actual Behavior (Mandatory)
At concurrent calling of get_or_create from different places of a program we can get different nodes which we consider as duplicates
How to Reproduce the Problem
Run the code in example. Probably you should increase the amount of created nodes if your's hardware better than mine. I wrapped code into for loop because sometimes the bug can not be caught on the first attempt. Looks like race condition.
Simple Example
As a simple alternative to concurrent calls from different places of entire program we can use asyncio.gather
importasyncioimportneomodel.configfromneomodelimportAsyncStructuredNode, StringPropertyneomodel.config.DATABASE_URL='bolt://neo4j:12345678@neo4j:7687'name='some_name'classSomeNode(AsyncStructuredNode):
name=StringProperty(required=True)
asyncdefmain():
forcounterinrange(100):
print(f'iteration {counter}')
# cleanupawaitneomodel.adb.cypher_query("MATCH (n) DETACH DELETE n")
# create a lot of the same nodes# this should return a lot of instances with the same idcreated_nodes=awaitasyncio.gather(*(
SomeNode.get_or_create(dict(name=name))
for_inrange(1000)
))
# check that all instances have the same idids=set(i[0].element_id_propertyforiincreated_nodes)
assertlen(ids) ==1# check that there is only one node in the neo4jassertlen(awaitSomeNode.nodes) ==1if__name__=='__main__':
asyncio.run(main())
Specifications (Mandatory)
Currently used versions
Versions
-OS: linux, 3.10.14-slim
-Library:5.3.0 [extras]
-Neo4j:5.19.0 community
The text was updated successfully, but these errors were encountered:
AnikinNN
changed the title
concurrent async calls of get_or_create lead to duplikate nodes
concurrent async calls of get_or_create lead to duplicate nodes
May 30, 2024
Hmmmm... Yes, it makes sense the this would happen, as it is a kind of race condition. The thing is, get_or_create is intended for batch operations, which should not be expected to avoid such collisions. Batch operations can run parallel, but then the user has to ensure that a given object is not shared across different parallel batches.
So... I don't see a way around this. Do you have an idea ?
Expected Behavior (Mandatory)
At concurrent calling of
get_or_create
from different places of a program we get the same node which was created onceActual Behavior (Mandatory)
At concurrent calling of
get_or_create
from different places of a program we can get different nodes which we consider as duplicatesHow to Reproduce the Problem
Run the code in example. Probably you should increase the amount of created nodes if your's hardware better than mine. I wrapped code into for loop because sometimes the bug can not be caught on the first attempt. Looks like race condition.
Simple Example
As a simple alternative to concurrent calls from different places of entire program we can use
asyncio.gather
Specifications (Mandatory)
Currently used versions
Versions
-OS: linux, 3.10.14-slim
-Library:5.3.0 [extras]
-Neo4j:5.19.0 community
The text was updated successfully, but these errors were encountered: