I’m simply trying to connect to an XMPP server with SASL authentication (using the Smack library), but I keep on getting the exception:
Resource binding not offered by server:
at org.jivesoftware.smack.SASLAuthentication.bindResourceAndEstablishSession(SASLA uthentication.java:416)
at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java: 331)
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:395)
at com.benbria.xmpp.ServerClient.connect(ServerClient.java:32)
at com.benbria.xmpp.ServerClient.run(ServerClient.java:67)
at com.benbria.xmpp.ServerClient.main(ServerClient.java:81)
Looking into the code,
private String bindResourceAndEstablishSession(String resource) throws XMPPException {
// Wait until server sends response containing the <bind> element
synchronized (this) {
if (!resourceBinded) {
try {
wait(30000);
}
catch (InterruptedException e) {
// Ignore
}
}
} if (!resourceBinded) {
// Server never offered resource binding
throw new XMPPException("Resource binding not offered by server");
}
so it seems that it sits and waits on the wait block, and times-out and throws the exception since resourceBinded is false.
The interesting part, the only place I see resourceBinded being set is in the bindingRequired method:
void bindingRequired() {
synchronized (this) {
resourceBinded = true;
// Wake up the thread that is waiting in the #authenticate method
notify();
}
}
The problem is if the wait in the bindResourceAndEstablishSession method is holding the lock on the instance, the call to bindingRequired can never wake up the initial wait. Meaning unless the bindingRequired is called first, the bindResourceAndEstablishSession will always wait 30s, even worse, if the bindingRequired loses the race condition for setting the resourceBinded to true, an exception gets called and the resource isn’t binded, which is what I think I am seeing when I run a debugger on the code.
The synchronization block in that part of the code seems to have been there for years, so I hope I’m just missing something. So if anyone can enlighten me, please do.
Thanks,
Dan