emschwartz / ilp3

An implementation of Interledger V3
5 stars 3 forks source link

Race Condition when adjusting balances #7

Closed dappelt closed 6 years ago

dappelt commented 6 years ago

balance.js caches account balances. This could result in a race condition. For example:

For two requests arriving at the same time and with the sametransfer.from, the check if an account is cached (line 21) evaluates to false for both. Whichever db read returns first will initialize the cached balance and move on to adjust the balance (line 58). However, when the second db read returns it overwrites the adjusted balance of the first request.

Solution: Make sure that the check if an account exists and the DB read is atomic.

Also, make sure there is no race condition when you write back the balance to the DB (line 83).

emschwartz commented 6 years ago

I fixed the first part of the issue but I'm not sure what you meant by "make sure there is no race condition when you write back the balance to the DB (line 83)." Do you see a race condition there now?

dappelt commented 6 years ago

I thought there is a race condition in the rollback logic, but at a second glance, it looks safe.