Skip to content

Commit 95b59c5

Browse files
committed
fix(manager): Add panic recovery in token manager goroutine
- Implemented panic recovery in the Start method of entraidTokenManager to prevent crashes and ensure listener is notified of errors.
1 parent a83b5f9 commit 95b59c5

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

internal/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package internal
22

33
// IsClosed checks if a channel is closed.
44
// Returns true only if the channel is actually closed, not just if it has data available.
5-
//
5+
//
66
// WARNING: This function will consume one value from the channel if it has pending data.
77
// Use with caution on channels where consuming data might cause issues.
88
func IsClosed(ch <-chan struct{}) bool {
@@ -15,4 +15,4 @@ func IsClosed(ch <-chan struct{}) bool {
1515
// Channel is open but has no data available
1616
return false
1717
}
18-
}
18+
}

manager/entraid_manager.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ func (e *entraidTokenManager) Start(listener TokenListener) (StopFunc, error) {
148148
e.listener = listener
149149

150150
go func(listener TokenListener, closed <-chan struct{}) {
151+
// Add panic recovery to prevent crashes
152+
defer func() {
153+
if r := recover(); r != nil {
154+
// Attempt to notify listener of panic, but don't panic again if that fails
155+
func() {
156+
defer func() { _ = recover() }()
157+
listener.OnError(fmt.Errorf("token manager goroutine panic: %v", r))
158+
}()
159+
}
160+
}()
151161
maxDelay := e.retryOptions.MaxDelay
152162
initialDelay := e.retryOptions.InitialDelay
153163

@@ -223,6 +233,7 @@ func (e *entraidTokenManager) stop() (err error) {
223233
err = fmt.Errorf("failed to stop token manager: %s", r)
224234
}
225235
}()
236+
226237
if e.ctxCancel != nil {
227238
e.ctxCancel()
228239
}

0 commit comments

Comments
 (0)