Copilot commented on code in PR #1484: URL: https://github.com/apache/pulsar-client-go/pull/1484#discussion_r3239698536
########## pulsar/internal/connection_test.go: ########## @@ -0,0 +1,218 @@ +// 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" + "sync/atomic" + "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 in registerIncomingRequest(), calling + // WaitGroup.Add(1) and checking state under c.mu.RLock(), a concurrent + // failLeftRequestsWhenClose() calling WaitGroup.Wait() could race with Add() + // in Go 1.25+, causing panic: "sync: WaitGroup is reused before previous Wait has returned" + // + // This test directly exercises the synchronization: + // 1. Many goroutines call registerIncomingRequest() to Add() to the WaitGroup + // 2. After they exit via Done(), failLeftRequestsWhenClose() calls Wait() + // 3. The test also verifies that all three methods (SendRequest, SendRequestNoWait, WriteData) + // properly use registerIncomingRequest() and reject calls after close + + const ( + numTrials = 10 + numGoroutines = 50 + ) + + for trial := 0; trial < numTrials; trial++ { + c := newTestConnection() + + startCh := make(chan struct{}) + stopCh := make(chan struct{}) + panicCh := make(chan any, 1) + + var wg sync.WaitGroup + var registerCount int32 + + // Producer goroutines that register requests + for i := 0; i < numGoroutines; i++ { + wg.Add(1) + + go func() { + defer wg.Done() + defer func() { + if r := recover(); r != nil { + panicCh <- r + } + }() + + <-startCh + + for { + select { + case <-stopCh: + return + default: + } + + // Call registerIncomingRequest() directly to exercise the WaitGroup Add/state check + if c.registerIncomingRequest() { + atomic.AddInt32(®isterCount, 1) + c.incomingRequestsWG.Done() + } + } + }() + } + + // Start producers + close(startCh) + + // Let producers run and accumulate adds + time.Sleep(20 * time.Millisecond) + + // Close the connection state + c.mu.Lock() + c.setStateClosed() + c.mu.Unlock() + + // Signal producers to stop + close(stopCh) + + // Wait for all producers to finish + wg.Wait() + + // Now call failLeftRequestsWhenClose() which calls Wait() on the WaitGroup + // With the race fixed, this should complete without panic Review Comment: This waits for all goroutines that can call registerIncomingRequest() to finish before failLeftRequestsWhenClose() invokes Wait(), so the test never exercises the Add/Wait overlap described in the regression. To cover the panic, start failLeftRequestsWhenClose() while producer goroutines are still running, then transition the connection to closed so new registrations are rejected and existing ones drain. -- 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]
