@@ -147,7 +147,28 @@ func (snh *NotificationHandler) handleMoving(ctx context.Context, handlerCtx pus
147147 if err := snh .markConnForHandoff (poolConn , newEndpoint , seqID , deadline ); err != nil {
148148 // Log error but don't fail the goroutine - use background context since original may be cancelled
149149 internal .Logger .Printf (context .Background (), logs .FailedToMarkForHandoff (poolConn .GetID (), err ))
150+ return
151+ }
152+
153+ // Queue the handoff immediately if the connection is idle in the pool.
154+ // If the connection is in use (StateInUse), it will be queued when returned to the pool via OnPut.
155+ // This handles the case where the connection is idle and might never be retrieved again.
156+ if poolConn .GetStateMachine ().GetState () == pool .StateIdle {
157+ if snh .manager .poolHooksRef != nil && snh .manager .poolHooksRef .workerManager != nil {
158+ if err := snh .manager .poolHooksRef .workerManager .queueHandoff (poolConn ); err != nil {
159+ internal .Logger .Printf (context .Background (), logs .FailedToQueueHandoff (poolConn .GetID (), err ))
160+ } else {
161+ // Mark the connection as queued for handoff to prevent it from being retrieved
162+ // This transitions the connection to StateUnusable
163+ if err := poolConn .MarkQueuedForHandoff (); err != nil {
164+ internal .Logger .Printf (context .Background (), logs .FailedToMarkForHandoff (poolConn .GetID (), err ))
165+ } else {
166+ internal .Logger .Printf (context .Background (), logs .MarkedForHandoff (poolConn .GetID ()))
167+ }
168+ }
169+ }
150170 }
171+ // If connection is StateInUse, the handoff will be queued when it's returned to the pool
151172 })
152173 return nil
153174 }
0 commit comments