Skip to content

Conversation

@youngxhui
Copy link

Add recover at goroutine call sites to prevent panics inside goroutines from affecting the main process; also add assertion checks for type conversions to avoid panics caused by failed assertions

Copilot AI review requested due to automatic review settings December 18, 2025 03:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances error handling in the LSP server's readLoop by adding panic recovery at the goroutine level and converting an unsafe type assertion to a safe checked assertion for cancel request parameters.

Key changes:

  • Adds a deferred panic recovery handler around the readLoop goroutine execution to prevent crashes from propagating to the main process
  • Replaces the unsafe type assertion for CancelParams with a safe checked assertion to avoid panics from type conversion failures

Comment on lines +370 to +372
if v, ok := req.Params.(*lsproto.CancelParams); ok {
s.cancelRequest(v.Id)
}
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silently ignoring a failed type assertion for CancelParams could hide protocol violations or bugs in the message parsing logic. If req.Params is not of type *lsproto.CancelParams when Method is MethodCancelRequest, this represents a serious protocol error that should be logged or handled explicitly rather than silently ignored. Consider logging a warning or error when the type assertion fails.

Copilot uses AI. Check for mistakes.
Comment on lines +313 to +322
go func() {
defer func() {
if r := recover(); r != nil {
stack := debug.Stack()
s.logger.Errorf("panic in readLoop: %v\n%s", r, string(stack))
readLoopErr <- fmt.Errorf("panic in readLoop: %v", r)
}
}()
readLoopErr <- s.readLoop(ctx)
}()
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The panic recovery mechanism conflicts with the project's coding guidelines which state "Do not remove any debug assertions or panic calls. Existing assertions are never too strict or incorrect." This guideline suggests that panics are intentional indicators of programming errors that should be caught during development. If readLoop contains panics, they likely indicate bugs that should be fixed rather than recovered from. Consider whether masking panics with recovery is the appropriate solution, or whether the underlying causes of potential panics should be addressed instead.

Copilot generated this review using guidance from repository custom instructions.
@jakebailey
Copy link
Member

Why add this? When does readLoop fail? When would a cancel request's params not be the expected kind?

@youngxhui youngxhui closed this Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants