I have been attempting to debug a problem when using multiple MVars, however to no luck.
My code uses two MVars: one to store the servers current state, and another to pass network events to and from the client threads. However after connecting and disconnecting several times, the server stops sending data upon new clients connecting (presumably because the network events MVar is emptied for whatever reason) and eventually trips up with the error: *** Exception: thread blocked indefinitely in an MVar operation
I have concluded the following in trying to debug this issue over the last few days:
- The functions used to modify the MVar(s) wont throw exceptions
- The problem does not occur until a client either connects, or connects then disconnects
- The issue seems to occur randomly (sometimes several clients can connect then disconnect, other times it happens right away)
I have isolated the problem to three files:
- https://github.com/Mattiemus/IMC-Server/blob/master/IMC.hs (the exception gets thrown in
sense) - https://github.com/Mattiemus/IMC-Server/blob/master/IMC/Networking/Server.hs (Modifined within
applicationhandleClient, andcleanupClient) - https://github.com/Mattiemus/IMC-Server/blob/master/IMC/Utilities/Concurrency.hs (functions which push and pop to a list stored in an MVar)
I am totally out of ideas, as I only use modifyMVar and withMVar (so surely it should never be totally left empty) - my only assumption is that maybe an Exception is being thrown while modifying the MVar, however I think this is highly unlikely.
Any help is appreciated, this problem has been bugging me for some time now.
senseyou are using anMVarvery much like a channel. Why not use aChaninstead? - Chris KuklewiczmodifyMVarandwithMVarare safe. I do not seeswapMVarused, which is unsafe, is it used elsewhere? - Chris KuklewiczswapMVarwas removed in favor of using modifyMVar_ due to this problem, however the core issue still persists. I dont useChanbecause I dont want to block while the channel is empty, I want to use it as if there is simply an empty list of events. SeepopAllMVarin Concurrency.hs - Mattiemus