Skip to content

Reset connection deadline before connection is released #1608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jkaflik
Copy link
Contributor

@jkaflik jkaflik commented Jul 17, 2025

Summary

It's not well well-thought-out fix attempt for #1607.

Requires deep dive into edge cases.

Checklist

Delete items not relevant to your PR:

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG
  • For significant changes, documentation in https://github.com/ClickHouse/clickhouse-docs was updated with further explanations or tutorials

Copy link

@Copilot 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 adjusts when the connection deadline is cleared by removing the early defer reset and instead resetting the deadline inside the streaming goroutine just before the connection is released.

  • Drop deferred reset of c.conn.SetDeadline immediately after setting context deadline.
  • Add explicit c.conn.SetDeadline(time.Time{}) in the goroutine after both streams close.
  • Shift responsibility for clearing the deadline to just before release.

@@ -44,7 +44,6 @@ func (c *connect) query(ctx context.Context, release nativeTransportRelease, que
// context level deadlines override any read deadline
if deadline, ok := ctx.Deadline(); ok {
c.conn.SetDeadline(deadline)
defer c.conn.SetDeadline(time.Time{})
}

if err = c.sendQuery(body, &options); err != nil {
Copy link
Preview

Copilot AI Jul 17, 2025

Choose a reason for hiding this comment

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

If a deadline was set on the connection, it isn't cleared when sendQuery returns an error, which can leave a stale deadline on subsequent operations. Consider using defer immediately after setting the deadline or explicitly clearing it in this error path.

Suggested change
if err = c.sendQuery(body, &options); err != nil {
if err = c.sendQuery(body, &options); err != nil {
c.conn.SetDeadline(time.Time{}) // Clear stale deadline

Copilot uses AI. Check for mistakes.

@@ -80,6 +79,8 @@ func (c *connect) query(ctx context.Context, release nativeTransportRelease, que
}
close(stream)
close(errors)

c.conn.SetDeadline(time.Time{})
Copy link
Preview

Copilot AI Jul 17, 2025

Choose a reason for hiding this comment

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

This unconditional reset will clear any pre-existing deadlines—even if none were set for this query—which may unintentionally override other timeouts. Track whether you applied a context deadline and guard the reset accordingly.

Suggested change
c.conn.SetDeadline(time.Time{})
if _, ok := ctx.Deadline(); !ok {
c.conn.SetDeadline(time.Time{})
}

Copilot uses AI. Check for mistakes.

@@ -80,6 +79,8 @@ func (c *connect) query(ctx context.Context, release nativeTransportRelease, que
}
close(stream)
close(errors)

c.conn.SetDeadline(time.Time{})
Copy link
Member

Choose a reason for hiding this comment

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

I think we might need to do this for SetReadDeadline too. As the SetDeadline is an override on the context. I am unsure what we're using exactly in our case.

p.s. thanks for jumping on this so quickly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants