Skip to content

Commit

Permalink
fix: live query kill might use closed context (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbinz authored Apr 26, 2024
1 parent edbb170 commit c6b9a25
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,29 @@ func (c *Client) Live(ctx context.Context, query string, vars map[string]any) (<
select {

case <-c.connCtx.Done():
// no kill needed, because the connection is already closed
// No kill needed, because the connection is already closed.
return

case <-ctx.Done():
c.logger.DebugContext(ctx, "Context done, closing live query channel.", "key", key)
}

if _, err := c.Kill(c.connCtx, key); err != nil {
// Find the best context to kill the live query with.
var killCtx context.Context

switch {

case ctx.Err() == nil:
killCtx = ctx

case c.connCtx.Err() == nil:
killCtx = c.connCtx

default:
killCtx = context.Background()
}

if _, err := c.Kill(killCtx, key); err != nil {
c.logger.ErrorContext(c.connCtx, "Could not kill live query.", "key", key, "error", err)
}

Expand Down Expand Up @@ -245,10 +260,14 @@ func (c *Client) send(ctx context.Context, req request) (_ []byte, err error) {

req.ID = reqID

c.logger.DebugContext(ctx, "Sending request.", "request", req)
c.logger.DebugContext(ctx, "Sending request.",
"id", req.ID,
"method", req.Method,
"params", req.Params,
)

if err := c.write(ctx, req); err != nil {
return nil, fmt.Errorf("could not write to websocket: %w", err)
return nil, fmt.Errorf("failed to write to websocket: %w", err)
}

select {
Expand Down

0 comments on commit c6b9a25

Please sign in to comment.