Skip to content

fix some write error handling #1595

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

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,10 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
buf, err := mc.buf.takeCompleteBuffer()
if err != nil {
// can not take the buffer. Something must be wrong with the connection
mc.log(err)
return "", ErrInvalidConn
mc.cleanup()
// interpolateParams would be called before sending any query.
// So its safe to retry.
return "", driver.ErrBadConn
}
buf = buf[:0]
argPos := 0
Expand Down
2 changes: 2 additions & 0 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func (mc *mysqlConn) writePacket(data []byte) error {
// Write packet
if mc.writeTimeout > 0 {
if err := mc.netConn.SetWriteDeadline(time.Now().Add(mc.writeTimeout)); err != nil {
mc.cleanup()
mc.log(err)
Copy link
Member Author

Choose a reason for hiding this comment

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

This also reduces risk of repeated error. Although SetWriteDeadline() won't fail in real world.

Comment on lines +120 to +121
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The search results indicate that ErrInvalidConn is still widely used across multiple files. To maintain consistency in error handling, consider updating all occurrences of ErrInvalidConn to driver.ErrBadConn.

  • packets.go
  • connection.go
  • statement.go
  • transaction.go
  • packets_test.go
  • errors.go
  • connection_test.go
Analysis chain

Proper cleanup and logging before returning errors enhance robustness.

However, consider updating the error returned from ErrInvalidConn to driver.ErrBadConn to align with the changes in connection.go as mentioned in the PR description. This ensures consistency across the codebase.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify if `ErrInvalidConn` is still used elsewhere in error handling.

# Test: Search for `ErrInvalidConn` usage. Expect: No occurrences except in deprecated or commented sections.
rg --type go 'ErrInvalidConn'

Length of output: 1253

return err
}
}
Expand Down
Loading