-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deadlock #95
Comments
Alright, that's a fair question. Do you have any repro scenario? It would be easier to check (or even to write a unit test) when we have a MCVE. I still didn't understood what thread exactly deadlock here, so I'm not sure how to proceed. I have an idea to make more things async / threadsafe by default in our code, but first I need to better understand this particular case. I agree that the code base isn't in the best state now. Hopefully I'll finish some big things in my life in a couple of months and will be able to come back to this library. |
In our project we have encountered with similar issues on simultaneous XmppStream.Write() attempts. At the least application waits a response from XmppStream.Write() from several threads. It seems to be a deadlock. Currently I'll try to update our code by adding simply |
Unfortunately, locking doesn't help. Currently we're captured
|
Alright, this is one thread. Could you please show stack for other thread (the one holding the lock)? |
I was not aware that second stack trace being useful taking into account that it was stopped on our lock, but I forgot about preceding code... So, please find it below, too:
|
Sorry for the delay - other priorities etc. Here's a MCVE which sends 10000 test messages. The deadlock will generally occur after a few thousand messages with several threads stuck at AsyncSocket.Write and one stuck at XmppStream.BytesWritten. Note that this is only appears to happen with AutoPresence = true . Also, I wouldn't normally create a new client and authenticate for each message as in this example, but the authentication seems to involved in causing the deadlock. XMPP server is OpenFire 4.2.3
|
Any progress here? And how dangerous to use |
Sorry, I was preoccupied with other great deeds, so wasn't able to participate in this case. Honestly I think that you, guys, have better opportunity to fix the issue if you have a repro case. Although, I will now do my best to research the case myself and fix it. I have some spare time and I'm on it right now. |
@KotM, I was able to reproduce the issue, although, sometimes it happens even with |
For now, I'm thinking about replacing the |
I've found an circular locking issue with the IStanzaEventListener methods in the XmppStream class .
Basically the IStanzaEventListener.StanzaReceived which acquires a [MethodImpl(MethodImplOptions.Synchronized)] lock can call IStanzaEventListener.BytesWritten which also needs to acquire a [MethodImpl(MethodImplOptions.Synchronized)] lock and therefore deadlocks.
The chain of calls is as follows:
XmppStream.StanzaReceived acquires a MethodImpl(MethodImplOptions.Synchronized) lock
--> calls XmppStream.OnElement
if (State is not ServerFeaturesState.Instance or SASLState.Instance or StartTLSState.Instance or CompressionState.Instance or SASLAuthedState.Instance)
--> invokes XmppStream.OnProtocol event
In IQTracker OnProtocol event is set to OnIQ
--> calls IQTracker.Call which calls the 'cb' callback which is set to JabberClient.GotSession in JabberClient.GotResource
--> sets JabberClient.IsAuthenticated
--> calls JabberClient.Presence
--> calls JabberClient.Write
--> calls SocketStanzaStream.JabberClient.Write
--> calls AsyncSocket.Write
--> calls Stream.BeginWrite with callback WroteData
--> WroteData calls SocketStanzaStream.OnWrite
--> calls XmppStream.BytesWritten which waits for a MethodImpl(MethodImplOptions.Synchronized) lock
Any ideas how to go about fixing this? The way this code uses callbacks is so convoluted its hard to know where to start without breaking something else.
The chain of events and subsequent deadlock is reproducible when sending a large number of XMPP messages.
The text was updated successfully, but these errors were encountered: