dfinity / agent-js

A collection of libraries and tools for building software around the Internet Computer, in JavaScript.

Home Page:https://agent-js.icp.xyz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unexpected behavior when calling the same canister function multiple times asynchronously

infu opened this issue · comments

commented

Describe the bug
Unexpected behavior when calling the same canister function multiple times asynchronously.

To Reproduce

await Promise.all(Array(10).fill(0).map(x => {
    return canister.counter_add()
}))

Where canister was created with Agent

Expected behavior
Should call the function 10 times.
Instead, only one call gets through and all 10 function calls receive copies of the 1st result (possibly because of cache or some kind of protection)
if counter_add returns the current count, then we will receive [1,1,1,1,1,1,1,1,1,1]
I stumbled upon that behavior like 3-4 times and I thought the problem was in my canisters or js.

If we add a 1ms delay, it works as expected

await Promise.all(Array(10).fill(0).map(async x => {
    await delay(1);
    return canister.counter_add()
}))

This will result in something like [1,2,5,4,3,6,7,8,9,10]

I wonder if the JS agent uses the “nonce” in the IC request to make these requests actually different (if they get the same timestamp in the expiry field). And judging from the recently merged PR #554 the answer is no, and the solution is already on master.

The JS Agent now uses nonces by default, so I think that this issue is covered. @infu are you satisfied with the latest implementation?