parconc-examples icon indicating copy to clipboard operation
parconc-examples copied to clipboard

In chat example, two users can kick each other

Open shiraeeshi opened this issue 3 years ago • 2 comments

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.

shiraeeshi avatar Mar 23 '21 16:03 shiraeeshi

You can see the changes in this commit diff.

shiraeeshi avatar Mar 23 '21 17:03 shiraeeshi

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.

simonmar avatar Apr 02 '21 10:04 simonmar