You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2023/01/31 00:13:58 UTC

[james-project] branch master updated: JAMES-3613 IMAP misses sessionId upon channelActive (#1413)

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

btellier 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 587e9eb954 JAMES-3613 IMAP misses sessionId upon channelActive (#1413)
587e9eb954 is described below

commit 587e9eb9543d92652a7636a5b7ae55ab7fcba475
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Tue Jan 31 07:13:52 2023 +0700

    JAMES-3613 IMAP misses sessionId upon channelActive (#1413)
---
 .../james/imapserver/netty/IMAPMDCContext.java     |  1 -
 .../netty/ImapChannelUpstreamHandler.java          | 26 +++++++++++++---------
 2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/IMAPMDCContext.java b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/IMAPMDCContext.java
index d484877857..9bb41e0d3b 100644
--- a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/IMAPMDCContext.java
+++ b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/IMAPMDCContext.java
@@ -74,7 +74,6 @@ public class IMAPMDCContext {
 
     public static MDCBuilder from(ImapSession imapSession) {
         return MDCBuilder.create()
-            .addToContext("sessionId", imapSession.sessionId().asString())
             .addToContext(MDCBuilder.USER, Optional.ofNullable(imapSession.getUserName())
                 .map(Username::asString)
                 .orElse(""))
diff --git a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java
index d451b4ed0a..7963e12c35 100644
--- a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java
+++ b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java
@@ -170,13 +170,15 @@ public class ImapChannelUpstreamHandler extends ChannelInboundHandlerAdapter imp
 
     @Override
     public void channelActive(ChannelHandlerContext ctx) throws Exception {
+        SessionId sessionId = SessionId.generate();
         ImapSession imapsession = new NettyImapSession(ctx.channel(), secure, compress, authenticationConfiguration.isSSLRequired(),
-            authenticationConfiguration.isPlainAuthEnabled(), SessionId.generate(),
+            authenticationConfiguration.isPlainAuthEnabled(), sessionId,
             authenticationConfiguration.getOidcSASLConfiguration());
-        MDCBuilder boundMDC = IMAPMDCContext.boundMDC(ctx);
-        imapsession.setAttribute(MDC_KEY, boundMDC);
         ctx.channel().attr(IMAP_SESSION_ATTRIBUTE_KEY).set(imapsession);
-        try (Closeable closeable = boundMDC.build()) {
+        MDCBuilder boundMDC = IMAPMDCContext.boundMDC(ctx)
+            .addToContext(MDCBuilder.SESSION_ID, sessionId.asString());
+        imapsession.setAttribute(MDC_KEY, boundMDC);
+        try (Closeable closeable = mdc(imapsession).build()) {
             InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
             LOGGER.info("Connection established from {}", address.getAddress().getHostAddress());
             imapConnectionsMetric.increment();
@@ -194,7 +196,11 @@ public class ImapChannelUpstreamHandler extends ChannelInboundHandlerAdapter imp
     private MDCBuilder mdc(ChannelHandlerContext ctx) {
         ImapSession maybeSession = ctx.channel().attr(IMAP_SESSION_ATTRIBUTE_KEY).get();
 
-        return Optional.ofNullable(maybeSession)
+        return mdc(maybeSession);
+    }
+
+    private MDCBuilder mdc(ImapSession imapSession) {
+        return Optional.ofNullable(imapSession)
             .map(session -> {
                 MDCBuilder boundMDC = (MDCBuilder) session.getAttribute(MDC_KEY);
 
@@ -206,13 +212,13 @@ public class ImapChannelUpstreamHandler extends ChannelInboundHandlerAdapter imp
 
     @Override
     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
-        try (Closeable closeable = mdc(ctx).build()) {
+        // remove the stored attribute for the channel to free up resources
+        // See JAMES-1195
+        ImapSession imapSession = ctx.channel().attr(IMAP_SESSION_ATTRIBUTE_KEY).getAndSet(null);
+        try (Closeable closeable = mdc(imapSession).build()) {
             InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
             LOGGER.info("Connection closed for {}", address.getAddress().getHostAddress());
 
-            // remove the stored attribute for the channel to free up resources
-            // See JAMES-1195
-            ImapSession imapSession = ctx.channel().attr(IMAP_SESSION_ATTRIBUTE_KEY).getAndSet(null);
             Disposable disposableAttribute = ctx.channel().attr(REQUEST_IN_FLIGHT_ATTRIBUTE_KEY).getAndSet(null);
 
             Optional.ofNullable(imapSession)
@@ -353,7 +359,7 @@ public class ImapChannelUpstreamHandler extends ChannelInboundHandlerAdapter imp
                     writer.flush();
                     ctx.fireChannelReadComplete();
                 }))
-                .contextWrite(ReactorUtils.context("imap", mdc(ctx))), message)
+                .contextWrite(ReactorUtils.context("imap", mdc(session))), message)
             // Manage throttling errors
             .doOnError(ctx::fireExceptionCaught)
             .subscribe();


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org