Copilot commented on code in PR #1484:
URL: https://github.com/apache/pulsar-client-go/pull/1484#discussion_r3239540298


##########
pulsar/internal/connection_test.go:
##########
@@ -0,0 +1,195 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package internal
+
+import (
+       "context"
+       "net/url"
+       "sync"
+       "testing"
+       "time"
+
+       pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto"
+       "github.com/apache/pulsar-client-go/pulsar/log"
+       "github.com/prometheus/client_golang/prometheus"
+       "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
+)
+
+func TestConnectionRejectRequestsAfterClose(t *testing.T) {
+       c := newTestConnection()
+
+       c.Close()
+
+       assertConnectionClosed(t, c)
+}
+
+func TestConnectionSendRequestRaceWithClose(t *testing.T) {
+       // Regression test for concurrent Add/Wait on WaitGroup during Close.
+       //
+       // Without proper synchronization between:
+       //   - registerIncomingRequest() calling WaitGroup.Add(1)
+       //   - Close() calling WaitGroup.Wait()
+       //
+       // Go 1.25+ may panic with:
+       //
+       //   sync: WaitGroup is reused before previous Wait has returned
+       //
+       // This test continuously issues requests while concurrently closing
+       // the connection to maximize the Add/Wait overlap window.
+
+       const (
+               numTrials     = 20
+               numGoroutines = 100
+       )
+
+       for trial := 0; trial < numTrials; trial++ {
+               c := newTestConnection()
+
+               startCh := make(chan struct{})
+               stopCh := make(chan struct{})
+
+               panicCh := make(chan any, numGoroutines)
+
+               var wg sync.WaitGroup
+
+               for i := 0; i < numGoroutines; i++ {
+                       wg.Add(1)
+
+                       go func(idx int) {
+                               defer wg.Done()
+
+                               defer func() {
+                                       if r := recover(); r != nil {
+                                               panicCh <- r
+                                       }
+                               }()
+
+                               <-startCh
+
+                               for {
+                                       select {
+                                       case <-stopCh:
+                                               return
+                                       default:
+                                       }
+
+                                       if idx%2 == 0 {
+                                               c.SendRequest(
+                                                       uint64(idx),
+                                                       &pb.BaseCommand{},
+                                                       func(*pb.BaseCommand, 
error) {},
+                                               )
+                                       } else {
+                                               _ = 
c.SendRequestNoWait(&pb.BaseCommand{})
+                                       }

Review Comment:
   The concurrent close regression only exercises `SendRequest` and 
`SendRequestNoWait`, but the fix also changes `WriteData`, which was the path 
described as able to enqueue after close selection started. Please include 
`WriteData` in this race test (with buffers whose release can be observed) so a 
regression in the data-write registration/drain path is covered.



##########
pulsar/internal/connection_test.go:
##########
@@ -0,0 +1,195 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package internal
+
+import (
+       "context"
+       "net/url"
+       "sync"
+       "testing"
+       "time"
+
+       pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto"
+       "github.com/apache/pulsar-client-go/pulsar/log"
+       "github.com/prometheus/client_golang/prometheus"
+       "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
+)
+
+func TestConnectionRejectRequestsAfterClose(t *testing.T) {
+       c := newTestConnection()
+
+       c.Close()
+
+       assertConnectionClosed(t, c)
+}
+
+func TestConnectionSendRequestRaceWithClose(t *testing.T) {
+       // Regression test for concurrent Add/Wait on WaitGroup during Close.
+       //
+       // Without proper synchronization between:
+       //   - registerIncomingRequest() calling WaitGroup.Add(1)
+       //   - Close() calling WaitGroup.Wait()
+       //
+       // Go 1.25+ may panic with:
+       //
+       //   sync: WaitGroup is reused before previous Wait has returned
+       //
+       // This test continuously issues requests while concurrently closing
+       // the connection to maximize the Add/Wait overlap window.
+
+       const (
+               numTrials     = 20
+               numGoroutines = 100
+       )
+
+       for trial := 0; trial < numTrials; trial++ {
+               c := newTestConnection()
+
+               startCh := make(chan struct{})
+               stopCh := make(chan struct{})
+
+               panicCh := make(chan any, numGoroutines)
+
+               var wg sync.WaitGroup
+
+               for i := 0; i < numGoroutines; i++ {
+                       wg.Add(1)
+
+                       go func(idx int) {
+                               defer wg.Done()
+
+                               defer func() {
+                                       if r := recover(); r != nil {
+                                               panicCh <- r
+                                       }
+                               }()
+
+                               <-startCh
+
+                               for {
+                                       select {
+                                       case <-stopCh:
+                                               return
+                                       default:
+                                       }
+
+                                       if idx%2 == 0 {
+                                               c.SendRequest(
+                                                       uint64(idx),
+                                                       &pb.BaseCommand{},
+                                                       func(*pb.BaseCommand, 
error) {},
+                                               )
+                                       } else {
+                                               _ = 
c.SendRequestNoWait(&pb.BaseCommand{})
+                                       }
+                               }
+                       }(i)
+               }
+
+               // Start all concurrent request producers together.
+               close(startCh)
+
+               // Allow requests to race with Close().
+               time.Sleep(10 * time.Millisecond)
+
+               c.Close()

Review Comment:
   This regression test never starts the connection run loop (and `Close()` 
itself does not call `failLeftRequestsWhenClose()`), so the 
`incomingRequestsWG.Wait()` path that panicked in #1483 is not exercised. As 
written, the test can pass even if `SendRequest` still calls `WaitGroup.Add` 
concurrently with `failLeftRequestsWhenClose`; please drive the run loop or 
invoke the close-drain path so the test actually covers the Add/Wait race.



-- 
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]

Reply via email to