Skip to content
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

OF-2921: Prevent deadlock by not broadcasting synchronously #2635

Merged
merged 3 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ public synchronized void terminateDetached(LocalSession session) {
Presence presence = new Presence();
presence.setType(Presence.Type.unavailable);
presence.setFrom(session.getAddress());
router.route(presence);

// Broadcast asynchronously, to reduce the likelihood of the broadcast introducing a deadlock (OF-2921).
TaskEngine.getInstance().submit(() -> router.route(presence));
}

session.getStreamManager().onClose(router, serverAddress);
Expand Down Expand Up @@ -1360,7 +1362,9 @@ public void onConnectionClose(Object handback) {
Presence presence = new Presence();
presence.setType(Presence.Type.unavailable);
presence.setFrom(session.getAddress());
router.route(presence);

// Broadcast asynchronously, to reduce the likelihood of the broadcast introducing a deadlock (OF-2921).
TaskEngine.getInstance().submit(() -> router.route(presence));
}

session.getStreamManager().onClose(router, serverAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,14 +944,25 @@ public void deliver(Packet queueOrPushStanza) throws UnauthorizedException {
if (stanzasToPush.isEmpty()) {
return;
}
synchronized (streamManager)
{

// When stream management is enabled, deliver and record stanzas under a mutex. If it's not enabled, don't
// acquire the lock to reduce lock contention (OF-2921).
if (!streamManager.isEnabled()) {
// Push stanzas to the client.
for (final Packet stanzaToPush : stanzasToPush) {
if (conn != null) {
conn.deliver(stanzaToPush);
}
streamManager.sentStanza(stanzaToPush);
}
} else {
synchronized (streamManager) {
// Push stanzas to the client.
for (final Packet stanzaToPush : stanzasToPush) {
if (conn != null) {
conn.deliver(stanzaToPush);
}
streamManager.sentStanza(stanzaToPush);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,18 @@ public void onTextMethod(String stanza)
public void onError(Throwable error)
{
Log.debug("Error detected; connection: {}, session: {}", wsConnection, wsSession, error);
synchronized (this) {
try {
if (isWebSocketOpen()) {
Log.warn("Attempting to close connection on which an error occurred: {}", wsConnection, error);
wsConnection.close(new StreamError(StreamError.Condition.internal_server_error), !isWebSocketOpen());
} else {
Log.debug("Error detected on websocket that isn't open (any more):", error);
wsConnection.close(null, !isWebSocketOpen());
}
} catch (Exception e) {
Log.error("Error disconnecting websocket", e);
} finally {
wsSession = null;
try {
if (isWebSocketOpen()) {
Log.warn("Attempting to close connection on which an error occurred: {}", wsConnection, error);
wsConnection.close(new StreamError(StreamError.Condition.internal_server_error), !isWebSocketOpen());
} else {
Log.debug("Error detected on websocket that isn't open (any more):", error);
wsConnection.close(null, !isWebSocketOpen());
}
} catch (Exception e) {
Log.error("Error disconnecting websocket", e);
} finally {
wsSession = null;
}
}

Expand Down
Loading