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

DefaultSimpUserRegistry Use ReentrantLock to Prevent Pinning #34538

Closed
wants to merge 1 commit into from
Closed
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
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.jspecify.annotations.Nullable;

@@ -45,6 +47,7 @@
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @author SeungHun Choi
* @since 4.2
*/
public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicationListener {
@@ -57,7 +60,7 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
/* Secondary lookup across all sessions by id */
private final Map<String, LocalSimpSession> sessions = new ConcurrentHashMap<>();

private final Object sessionLock = new Object();
private final Lock sessionLock = new ReentrantLock();


/**
@@ -110,7 +113,9 @@ else if (event instanceof SessionConnectedEvent) {
if (user instanceof DestinationUserNameProvider destinationUserNameProvider) {
name = destinationUserNameProvider.getDestinationUserName();
}
synchronized (this.sessionLock) {

this.sessionLock.lock();
Comment on lines +116 to +117

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.sessionLock.lock();
sessionLock.lock();

There is no overloaded field that makes the this. access required, right? Both are correct, but if there is no ambiguity, it's more concise to leave it out.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your valuable feedback.
I actually considered this when making the code changes.

Looking at other classes that use ReentrantLock, I noticed that they all include this.
so I decided to keep it for consistency.
(e.g., SubProtocolWebSocketHandler, InMemoryWebSessionStore.ExpiredSessionChecker)

try {
LocalSimpUser simpUser = this.users.get(name);
if (simpUser == null) {
simpUser = new LocalSimpUser(name, user);
@@ -119,10 +124,13 @@ else if (event instanceof SessionConnectedEvent) {
LocalSimpSession session = new LocalSimpSession(sessionId, simpUser);
simpUser.addSession(session);
this.sessions.put(sessionId, session);
} finally {
this.sessionLock.unlock();
}
}
else if (event instanceof SessionDisconnectEvent) {
synchronized (this.sessionLock) {
this.sessionLock.lock();
try {
LocalSimpSession session = this.sessions.remove(sessionId);
if (session != null) {
LocalSimpUser user = session.getUser();
@@ -131,6 +139,8 @@ else if (event instanceof SessionDisconnectEvent) {
this.users.remove(user.getName());
}
}
} finally {
this.sessionLock.unlock();
}
}
else if (event instanceof SessionUnsubscribeEvent) {