Skip to content
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

fix some write error handling #1595

Merged
merged 1 commit into from
Jun 11, 2024
Merged

Conversation

methane
Copy link
Member

@methane methane commented Jun 5, 2024

Description

interpolateParams() returned ErrInvalidConn without closing the connection.
Since database/sql doesn't understand ErrInvalidConn, there is a risk that database/sql reuse this connection and ErrInvalidConn is returned repeatedly.

This PR should be backported to 1.8.

Checklist

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary
  • Added myself / the copyright holder to the AUTHORS file

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling in connection management to ensure better resource cleanup and logging.

@methane methane added the bug label Jun 5, 2024
Copy link

coderabbitai bot commented Jun 5, 2024

Walkthrough

The recent updates enhance error handling in the mysqlConn struct by incorporating cleanup and logging mechanisms. Specifically, the interpolateParams and writePacket methods now invoke mc.cleanup() and, in the case of writePacket, also mc.log(err) upon encountering errors. This ensures better resource management and error reporting.

Changes

File Change Summary
connection.go Updated interpolateParams to call mc.cleanup() and return driver.ErrBadConn on buffer error.
packets.go Modified writePacket to call mc.cleanup() and mc.log(err) on write deadline error.

Poem

In code where packets flow and errors might,
A rabbit hops to set things right.
With cleanup swift and logs so clear,
Stability and grace appear.
🐇💻✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@@ -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.

@methane methane requested a review from shogo82148 June 5, 2024 07:42
@coveralls
Copy link

Coverage Status

coverage: 82.407% (-0.09%) from 82.494%
when pulling 7b8f376 on methane:fix-badconn
into 2f7015e on go-sql-driver:master.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2f7015e and 7b8f376.

Files selected for processing (2)
  • connection.go (1 hunks)
  • packets.go (1 hunks)
Additional context used
golangci-lint
connection.go

27-27: undefined: buffer (typecheck)


30-30: undefined: mysqlResult (typecheck)


31-31: undefined: Config (typecheck)


32-32: undefined: connector (typecheck)


36-36: undefined: clientFlag (typecheck)


37-37: undefined: statusFlag (typecheck)


46-46: undefined: atomicError (typecheck)


383-383: undefined: textRows (typecheck)


577-577: undefined: mysqlStmt (typecheck)


596-596: undefined: mysqlStmt (typecheck)


118-118: undefined: errBadConnNoWrite (typecheck)


130-130: undefined: ErrInvalidConn (typecheck)


141-141: undefined: mysqlTx (typecheck)


149-149: mc.writeCommandPacket undefined (type *mysqlConn has no field or method writeCommandPacket) (typecheck)


153-153: mc.clearResult undefined (type *mysqlConn has no field or method clearResult) (typecheck)


185-185: undefined: ErrInvalidConn (typecheck)


192-192: undefined: ErrInvalidConn (typecheck)


196-196: mc.writeCommandPacketStr undefined (type *mysqlConn has no field or method writeCommandPacketStr) (typecheck)


203-203: undefined: mysqlStmt (typecheck)


211-211: mc.readUntilEOF undefined (type *mysqlConn has no field or method readUntilEOF) (typecheck)


217-217: mc.readUntilEOF undefined (type *mysqlConn has no field or method readUntilEOF) (typecheck)


277-277: undefined: appendDateTime (typecheck)


285-285: undefined: statusNoBackslashEscapes (typecheck)


286-286: undefined: escapeBytesBackslash (typecheck)


288-288: undefined: escapeBytesQuotes (typecheck)


296-296: undefined: statusNoBackslashEscapes (typecheck)


297-297: undefined: escapeBytesBackslash (typecheck)


299-299: undefined: escapeBytesQuotes (typecheck)


305-305: undefined: statusNoBackslashEscapes (typecheck)


306-306: undefined: escapeStringBackslash (typecheck)


308-308: undefined: escapeStringQuotes (typecheck)


352-352: mc.clearResult undefined (type *mysqlConn has no field or method clearResult) (typecheck)


354-354: mc.writeCommandPacketStr undefined (type *mysqlConn has no field or method writeCommandPacketStr) (typecheck)


366-366: mc.readUntilEOF undefined (type *mysqlConn has no field or method readUntilEOF) (typecheck)


384-384: mc.clearResult undefined (type *mysqlConn has no field or method clearResult) (typecheck)


402-402: mc.writeCommandPacketStr undefined (type *mysqlConn has no field or method writeCommandPacketStr) (typecheck)


408-408: undefined: textRows (typecheck)


423-423: mc.readColumns undefined (type *mysqlConn has no field or method readColumns) (typecheck)


442-442: undefined: textRows (typecheck)


444-444: undefined: mysqlField (typecheck)


492-492: mc.writeCommandPacket undefined (type *mysqlConn has no field or method writeCommandPacket) (typecheck)


511-511: undefined: mapIsolationLevel (typecheck)


525-525: undefined: namedValueToValue (typecheck)


544-544: undefined: namedValueToValue (typecheck)


578-578: undefined: namedValueToValue (typecheck)


661-661: undefined: converter (typecheck)


688-688: undefined: connCheck (typecheck)

Additional comments not posted (1)
connection.go (1)

233-236: The changes in the interpolateParams method are appropriate and align with the PR's objectives to handle connection errors more effectively.

It's recommended to add a comment explaining why mc.cleanup() is called and driver.ErrBadConn is returned instead of ErrInvalidConn, to aid future maintainability.

Comment on lines +120 to +121
mc.cleanup()
mc.log(err)
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

Copy link
Contributor

@shogo82148 shogo82148 left a comment

Choose a reason for hiding this comment

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

LGTM

@methane methane merged commit 05325d8 into go-sql-driver:master Jun 11, 2024
32 checks passed
@methane methane deleted the fix-badconn branch June 11, 2024 13:34
methane added a commit to methane/mysql that referenced this pull request Jun 11, 2024
interpolateParams() returned ErrInvalidConn without closing the connection.
Since database/sql doesn't understand ErrInvalidConn, there is a risk
that database/sql reuse this connection and ErrInvalidConn is returned
repeatedly.
methane added a commit that referenced this pull request Jun 13, 2024
interpolateParams() returned ErrInvalidConn without closing the connection.

Since database/sql doesn't understand ErrInvalidConn, there is a risk
that database/sql reuse this connection and ErrInvalidConn is returned
repeatedly.

This PR is backport of #1595.
@coderabbitai coderabbitai bot mentioned this pull request Nov 21, 2024
5 tasks
@coderabbitai coderabbitai bot mentioned this pull request Jan 23, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants