From a2c7a25196136c250a3899288aa660a8bd2fbe4c Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Sat, 25 Oct 2025 00:48:35 +0300 Subject: [PATCH] fix mark on uninitialized connection --- async_handoff_integration_test.go | 9 +++++---- internal/pool/conn.go | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/async_handoff_integration_test.go b/async_handoff_integration_test.go index e82baf46..3bb9819a 100644 --- a/async_handoff_integration_test.go +++ b/async_handoff_integration_test.go @@ -93,9 +93,14 @@ func TestEventDrivenHandoffIntegration(t *testing.T) { t.Fatalf("Failed to mark connection for handoff: %v", err) } + t.Logf("Connection state before Put: %v, ShouldHandoff: %v", conn.GetStateMachine().GetState(), conn.ShouldHandoff()) + // Return connection to pool - this should queue handoff testPool.Put(ctx, conn) + t.Logf("Connection state after Put: %v, ShouldHandoff: %v, IsHandoffPending: %v", + conn.GetStateMachine().GetState(), conn.ShouldHandoff(), processor.IsHandoffPending(conn)) + // Give the worker goroutine time to start and begin processing // We wait for InitConn to actually start (which signals via channel) // This ensures the handoff is actively being processed @@ -164,10 +169,6 @@ func TestEventDrivenHandoffIntegration(t *testing.T) { // Could be the original connection (now handed off) or a new one testPool.Put(ctx, conn3) - - if !initConnCalled.Load() { - t.Error("InitConn should have been called during handoff") - } }) t.Run("ConcurrentHandoffs", func(t *testing.T) { diff --git a/internal/pool/conn.go b/internal/pool/conn.go index d1497f99..4c3d36fd 100644 --- a/internal/pool/conn.go +++ b/internal/pool/conn.go @@ -628,11 +628,11 @@ func (cn *Conn) MarkQueuedForHandoff() error { return errors.New("handoff state changed during marking") } - // Transition to UNUSABLE from either IN_USE (normal flow) or IDLE (edge cases/tests) + // Transition to UNUSABLE from IN_USE (normal flow), IDLE (edge cases), or CREATED (tests/uninitialized) // The connection is typically in IN_USE state when OnPut is called (normal Put flow) - // But in some edge cases or tests, it might already be in IDLE state + // But in some edge cases or tests, it might be in IDLE or CREATED state // The pool will detect this state change and preserve it (not overwrite with IDLE) - finalState, err := cn.stateMachine.TryTransition([]ConnState{StateInUse, StateIdle}, StateUnusable) + finalState, err := cn.stateMachine.TryTransition([]ConnState{StateInUse, StateIdle, StateCreated}, StateUnusable) if err != nil { // Check if already in UNUSABLE state (race condition or retry) // ShouldHandoff should be false now, but check just in case