parconc-examples
parconc-examples copied to clipboard
In chat example, two users can kick each other
In chat example from Chapter 12, it seems to me that there is no guarantee that when two users kick each other at the same time, only one of the kicks will succeed.
The kick operation is divided to two STM transactions (two atomically
calls): one of them is in server
function in runClient
(at line 214), the second one is in handleMessage
(at line 236). When we divide the transaction into two, we lose the atomicity, right?
Here is an example project (https://github.com/shiraeeshi/hs-parconc-chat-server) that shows how to make two concurrent kicks succeed. I added /pause
, /resume
, /pauseKicks
, /resumeKicks
commands. You can see the scenario in readme.
You can see the changes in this commit diff.
Yes, I think you're right. To fix this, either the two transactions should be combined, or the kick
function should check that the current user has not already been kicked.