Copilot commented on code in PR #3023:
URL: https://github.com/apache/dubbo-go/pull/3023#discussion_r2342719689


##########
filter/accesslog/filter.go:
##########
@@ -84,16 +87,45 @@ func init() {
  * AccessLogFilter is designed to be singleton
  */
 type Filter struct {
-       logChan chan Data
+       logChan   chan Data
+       fileCache map[string]*os.File
+       fileLock  sync.RWMutex
 }
 
 func newFilter() filter.Filter {
        if accessLogFilter == nil {
                once.Do(func() {
-                       accessLogFilter = &Filter{logChan: make(chan Data, 
LogMaxBuffer)}
+                       shutdownCtx, shutdownCancel = 
context.WithCancel(context.Background())
+                       accessLogFilter = &Filter{
+                               logChan:   make(chan Data, LogMaxBuffer),
+                               fileCache: make(map[string]*os.File),
+                       }
                        go func() {
-                               for accessLogData := range 
accessLogFilter.logChan {
-                                       
accessLogFilter.writeLogToFile(accessLogData)
+                               defer func() {
+                                       // Drain remaining log data on shutdown
+                                       for {
+                                               select {
+                                               case accessLogData, ok := 
<-accessLogFilter.logChan:
+                                                       if !ok {
+                                                               return
+                                                       }
+                                                       
accessLogFilter.writeLogToFile(accessLogData)
+                                               default:
+                                                       return
+                                               }
+                                       }
+                               }()

Review Comment:
   The defer function's drain loop can cause a deadlock if writeLogToFile 
blocks indefinitely. Consider adding a timeout or context cancellation to 
prevent the goroutine from hanging during shutdown.



##########
filter/accesslog/memory_leak_test.go:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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 accesslog
+
+import (
+       "context"
+       "os"
+       "runtime"
+       "sync"
+       "testing"
+       "time"
+)
+
+import (
+       "github.com/stretchr/testify/assert"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/protocol/base"
+       invocation_impl "dubbo.apache.org/dubbo-go/v3/protocol/invocation"
+       "dubbo.apache.org/dubbo-go/v3/protocol/result"
+)
+
+// TestAccessLogFilterGoroutineShutdown tests that the goroutine is properly 
shut down
+func TestAccessLogFilterGoroutineShutdown(t *testing.T) {
+       // Reset global state
+       once.Do(func() {}) // Trigger once
+       accessLogFilter = nil
+       shutdownCancel = nil
+       shutdownCtx = nil
+       shutdownOnce = sync.Once{}
+       once = sync.Once{}
+
+       // Count goroutines before
+       initialGoroutines := runtime.NumGoroutine()
+
+       // Create filter (this should start the goroutine)
+       filter := newFilter()
+       assert.NotNil(t, filter)
+
+       // Give the goroutine time to start
+       time.Sleep(100 * time.Millisecond)
+       postCreateGoroutines := runtime.NumGoroutine()
+
+       // Should have at least one more goroutine
+       assert.Greater(t, postCreateGoroutines, initialGoroutines)
+
+       // Shutdown the filter
+       Shutdown()
+
+       // Give goroutine time to exit
+       time.Sleep(100 * time.Millisecond)
+       runtime.GC() // Force garbage collection
+
+       postShutdownGoroutines := runtime.NumGoroutine()
+
+       // Goroutine count should be back to original or less
+       assert.LessOrEqual(t, postShutdownGoroutines, initialGoroutines+1,
+               "Goroutines should be cleaned up after shutdown")
+}
+
+// TestAccessLogFilterFileHandleManagement tests proper file handle management
+func TestAccessLogFilterFileHandleManagement(t *testing.T) {
+       // Reset global state
+       once.Do(func() {}) // Trigger once
+       accessLogFilter = nil
+       shutdownCancel = nil
+       shutdownCtx = nil
+       shutdownOnce = sync.Once{}
+       once = sync.Once{}

Review Comment:
   Duplicate global state manipulation pattern from the previous test. This 
code duplication makes tests brittle and harder to maintain. Extract this setup 
into a helper function.



##########
filter/accesslog/memory_leak_test.go:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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 accesslog
+
+import (
+       "context"
+       "os"
+       "runtime"
+       "sync"
+       "testing"
+       "time"
+)
+
+import (
+       "github.com/stretchr/testify/assert"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/protocol/base"
+       invocation_impl "dubbo.apache.org/dubbo-go/v3/protocol/invocation"
+       "dubbo.apache.org/dubbo-go/v3/protocol/result"
+)
+
+// TestAccessLogFilterGoroutineShutdown tests that the goroutine is properly 
shut down
+func TestAccessLogFilterGoroutineShutdown(t *testing.T) {
+       // Reset global state
+       once.Do(func() {}) // Trigger once
+       accessLogFilter = nil
+       shutdownCancel = nil
+       shutdownCtx = nil
+       shutdownOnce = sync.Once{}
+       once = sync.Once{}

Review Comment:
   Manipulating global variables and sync.Once instances in tests creates 
fragile test dependencies and can lead to race conditions. Consider using 
dependency injection or test-specific instances instead of resetting global 
state.



-- 
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: notifications-unsubscr...@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org
For additional commands, e-mail: notifications-h...@dubbo.apache.org

Reply via email to