Skip to content

Commit

Permalink
Fix Panic: Write After Handler Closed
Browse files Browse the repository at this point in the history
I think we get this issue (#319) because we check whether the context is cancelled
before generating an AI response and then try to return the response.
However, in between the initial check and actually having the response
to send the context could have been cancelled.

So check if the context is done. before sending the response.

Fix #319
  • Loading branch information
jlewi committed Nov 4, 2024
1 parent fde802c commit 93f7480
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions app/pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,20 @@ func (a *Agent) StreamGenerate(ctx context.Context, stream *connect.BidiStream[v
}

log.V(logs.Debug).Info("Sending response", zap.Object("response", response))
if err := stream.Send(response); err != nil {
log.Error(err, "Failed to send response")
// TODO(jeremy): Should we be using connect codes and routines? e.g.
// connect.NewError(
statusChan <- status.Newf(codes.Internal, "failed to send response; %v", err)

select {
case <-ctx.Done():
log.Info("Context cancelled before AI Response returned")
statusChan <- status.New(codes.Canceled, "Stream context canceled")
return
default:
if err := stream.Send(response); err != nil {
log.Error(err, "Failed to send response")
// TODO(jeremy): Should we be using connect codes and routines? e.g.
// connect.NewError(
statusChan <- status.Newf(codes.Internal, "failed to send response; %v", err)
return
}
}

case <-ctx.Done():
Expand Down

0 comments on commit 93f7480

Please sign in to comment.