Commit f6ca7019 authored by yangjianbo's avatar yangjianbo
Browse files

fix(oauth): SessionStore.Stop() 添加 sync.Once 防重入保护 (P1-05)



oauth 和 openai 包的 SessionStore.Stop() 直接调用 close(stopCh),
重复调用会导致 panic。使用 sync.Once 包裹确保幂等安全。

新增单元测试覆盖连续调用和 50 goroutine 并发调用场景。
Co-Authored-By: default avatarClaude Opus 4.6 <noreply@anthropic.com>
parent a84604dc
...@@ -50,6 +50,7 @@ type OAuthSession struct { ...@@ -50,6 +50,7 @@ type OAuthSession struct {
type SessionStore struct { type SessionStore struct {
mu sync.RWMutex mu sync.RWMutex
sessions map[string]*OAuthSession sessions map[string]*OAuthSession
stopOnce sync.Once
stopCh chan struct{} stopCh chan struct{}
} }
...@@ -65,7 +66,9 @@ func NewSessionStore() *SessionStore { ...@@ -65,7 +66,9 @@ func NewSessionStore() *SessionStore {
// Stop stops the cleanup goroutine // Stop stops the cleanup goroutine
func (s *SessionStore) Stop() { func (s *SessionStore) Stop() {
close(s.stopCh) s.stopOnce.Do(func() {
close(s.stopCh)
})
} }
// Set stores a session // Set stores a session
......
package oauth
import (
"sync"
"testing"
"time"
)
func TestSessionStore_Stop_Idempotent(t *testing.T) {
store := NewSessionStore()
store.Stop()
store.Stop()
select {
case <-store.stopCh:
// ok
case <-time.After(time.Second):
t.Fatal("stopCh 未关闭")
}
}
func TestSessionStore_Stop_Concurrent(t *testing.T) {
store := NewSessionStore()
var wg sync.WaitGroup
for range 50 {
wg.Add(1)
go func() {
defer wg.Done()
store.Stop()
}()
}
wg.Wait()
select {
case <-store.stopCh:
// ok
case <-time.After(time.Second):
t.Fatal("stopCh 未关闭")
}
}
...@@ -47,6 +47,7 @@ type OAuthSession struct { ...@@ -47,6 +47,7 @@ type OAuthSession struct {
type SessionStore struct { type SessionStore struct {
mu sync.RWMutex mu sync.RWMutex
sessions map[string]*OAuthSession sessions map[string]*OAuthSession
stopOnce sync.Once
stopCh chan struct{} stopCh chan struct{}
} }
...@@ -92,7 +93,9 @@ func (s *SessionStore) Delete(sessionID string) { ...@@ -92,7 +93,9 @@ func (s *SessionStore) Delete(sessionID string) {
// Stop stops the cleanup goroutine // Stop stops the cleanup goroutine
func (s *SessionStore) Stop() { func (s *SessionStore) Stop() {
close(s.stopCh) s.stopOnce.Do(func() {
close(s.stopCh)
})
} }
// cleanup removes expired sessions periodically // cleanup removes expired sessions periodically
......
package openai
import (
"sync"
"testing"
"time"
)
func TestSessionStore_Stop_Idempotent(t *testing.T) {
store := NewSessionStore()
store.Stop()
store.Stop()
select {
case <-store.stopCh:
// ok
case <-time.After(time.Second):
t.Fatal("stopCh 未关闭")
}
}
func TestSessionStore_Stop_Concurrent(t *testing.T) {
store := NewSessionStore()
var wg sync.WaitGroup
for range 50 {
wg.Add(1)
go func() {
defer wg.Done()
store.Stop()
}()
}
wg.Wait()
select {
case <-store.stopCh:
// ok
case <-time.After(time.Second):
t.Fatal("stopCh 未关闭")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment