prosgarz35 commented on PR #3198:
URL: https://github.com/apache/james-project/pull/3198#issuecomment-5806832789

       ### [IMAP] IDLE robustness: transport liveness checks and graceful 
teardown on mid-IDLE LOGOUT
   
       #### Context & Motivation
       1. **Heartbeat ghost writes on dropped TCP connections**:
          When a client terminates abruptly (TCP RST, connection drop, or 
network disruption), the Netty channel becomes inactive immediately, but the 
internal IMAP session state (`session.getState()`) may temporarily remain 
`SELECTED`
     until channel teardown finishes. During this window, the periodic 
`IdleProcessor` heartbeat runnable continued waking up and attempting writes 
into a closed channel, generating futile I/O work and relying on downstream 
write failures
     to stop.
       2. **Resource retention on mid-IDLE LOGOUT**:
          Certain scripts and non-conformant clients attempt to disconnect 
mid-IDLE by directly sending `tag LOGOUT` instead of the RFC 2177 mandated 
`DONE`. While `IdleProcessor` rejected this input with `BAD 
INVALID_CONTINUATION`, the
     underlying mailbox listener and session remained active and selected.
   
       #### Changes
       - **Added `ImapSession#isConnected()`**:
         - Introduced a default method in `ImapSession` returning `true` for 
100% backward compatibility with test fakes.
         - Implemented in `NettyImapSession` via `channel.isActive()`.
       - **Liveness guards in `IdleProcessor`**:
         - Heartbeat scheduling loop checks `session.isConnected()` both before 
sending the untagged heartbeat and before rescheduling itself. If disconnected, 
it proactively unregisters the mailbox listener, pops the line handler, and
     terminates immediately.
         - Continuation handler checks `!session.isConnected()` to avoid futile 
`BAD` responses when the remote peer is already disconnected.
       - **Graceful session teardown on `LOGOUT`**:
         - In `IdleProcessor`, if the continuation line indicates an intended 
`LOGOUT`, `session.logout().subscribe()` is triggered to ensure immediate 
mailbox deselection and resource release.
   
       #### Impact & Safety
       - **Zero API breakage**: Default interface method preserves binary and 
source compatibility for external implementations and test harnesses.
       - **Resource efficiency**: Eliminates useless background scheduling 
cycles and prevents leaked mailbox listeners on abnormal disconnections.
       - **Verification**: Passes `mvn test-compile -pl 
:protocols-imap,:james-server-protocols-imap4` and Checkstyle with 0 violations.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to