This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new 02e74a57f0 [FIX] IMAP LEAK: CLOSE, UNSELECT
02e74a57f0 is described below

commit 02e74a57f07d50ca21349983db631f92da401769
Author: Benoit TELLIER <[email protected]>
AuthorDate: Sun Feb 18 21:37:24 2024 +0100

    [FIX] IMAP LEAK: CLOSE, UNSELECT
    
    SelectedMailbox subscription was not well cleaned up
---
 .../james/imapserver/netty/NettyImapSession.java     | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git 
a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/NettyImapSession.java
 
b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/NettyImapSession.java
index 10e223acf5..4325d329e2 100644
--- 
a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/NettyImapSession.java
+++ 
b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/NettyImapSession.java
@@ -124,15 +124,15 @@ public class NettyImapSession implements ImapSession, 
NettyConstants {
     @Override
     public Mono<Void> deselect() {
         this.state = ImapSessionState.AUTHENTICATED;
-        this.selectedMailbox.set(null);
         return closeMailbox();
     }
 
     @Override
     public Mono<Void> selected(SelectedMailbox mailbox) {
         this.state = ImapSessionState.SELECTED;
-        return closeMailbox()
-            .then(Mono.fromRunnable(() -> selectedMailbox.set(mailbox)));
+        return Mono.fromCallable(() -> 
Optional.ofNullable(selectedMailbox.getAndSet(mailbox)))
+            .flatMap(maybeMailbox -> 
maybeMailbox.map(SelectedMailbox::deselect)
+                .orElse(Mono.empty()));
     }
 
     @Override
@@ -156,11 +156,15 @@ public class NettyImapSession implements ImapSession, 
NettyConstants {
     }
 
     private Mono<Void> closeMailbox() {
-        if (selectedMailbox.get() != null) {
-            return selectedMailbox.get().deselect()
-                .then(Mono.fromRunnable(() -> selectedMailbox.set(null)));
-        }
-        return Mono.empty();
+        return closeMailbox(selectedMailbox.getAndSet(null));
+    }
+
+    private Mono<Void> closeMailbox(SelectedMailbox value) {
+        return Optional.ofNullable(value)
+            .map(s -> s.deselect()
+                .then(Mono.fromRunnable(() -> selectedMailbox.set(null))))
+            .orElse(Mono.empty())
+            .then();
     }
 
     @Override


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to