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 issue 1567 #1570

Merged
merged 4 commits into from Mar 22, 2024
Merged

Fix issue 1567 #1570

merged 4 commits into from Mar 22, 2024

Conversation

shogo82148
Copy link
Contributor

@shogo82148 shogo82148 commented Mar 22, 2024

Description

closes #1567

When TLS is enabled, mc.netConn is rewritten after the TLS handshak as detailed here:

mc.netConn = tlsConn

Therefore, mc.netConn should not be accessed within the watcher goroutine.
Instead, mc.rawConn should be initialized prior to invoking mc.startWatcher, and mc.rawConn should be used in lieu of mc.netConn.

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

  • Refactor
    • Improved variable naming for better code readability and maintenance.
    • Enhanced network connection handling logic.
  • New Features
    • Updated TCP connection handling to better support TCP Keepalives.
  • Tests
    • Added a new test to address and verify the fix for a specific issue related to TLS, connection pooling, and round trip time estimation.

Copy link

coderabbitai bot commented Mar 22, 2024

Walkthrough

The changes involve improvements to the MySQL driver's connection handling, particularly in network connection logic, TCP connection keepalives, and the addition of a new test function for a specific issue. These modifications aim to enhance stability, address data race conditions, and improve error handling within the driver.

Changes

File(s) Change Summary
connection.go, packets.go Renamed variables and updated network connection handling logic.
connector.go Set mc.rawConn to mc.netConn before enabling TCP Keepalives.
driver_test.go Added a new test function for issue 1567 related to TLS, connection pooling, and RTT estimation.

Assessment against linked issues

Objective Addressed Explanation
Data race between mysqlConn.cleanup() and writeHandshakeResponsePacket (#1567)
Investigate root cause of the data race issue and ensure thread safety (#1567)
Verify compatibility with driver version v1.8.1-0.20240317050433-65395d853d2c, Go 1.20.14, and MySQL 8 (#1567)
Enhance MySQL connection handling stability to prevent similar issues (#1567)
Review and refactor affected methods to eliminate data race scenario (#1567) It is unclear if methods other than those mentioned have been reviewed or refactored.

Related issues

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-tests 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 tests 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 tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@coveralls
Copy link

Coverage Status

coverage: 82.462% (+0.5%) from 82.003%
when pulling 73abfe6 on shogo82148:fix-issue-1567
into d86c452 on go-sql-driver:master.

@shogo82148 shogo82148 requested a review from methane March 22, 2024 15:20
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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between d86c452 and 73abfe6.
Files selected for processing (4)
  • connection.go (1 hunks)
  • connector.go (1 hunks)
  • driver_test.go (2 hunks)
  • packets.go (1 hunks)
Additional comments: 4
connector.go (1)
  • 108-108: The change correctly assigns mc.netConn to mc.rawConn to address the data race issue by ensuring consistent connection handling post-TLS handshake. Please ensure that mc.rawConn is used consistently in all relevant parts of the codebase to prevent data races. Additionally, assess any potential side effects of this change, especially in error handling and connection cleanup.
Verification successful

The verification process did not find any issues with the use of mc.rawConn or potential side effects in error handling and connection cleanup based on the provided search criteria. It's important to ensure that mc.rawConn is used consistently in all relevant parts of the codebase to prevent data races, and any potential side effects, especially in error handling and connection cleanup, are carefully assessed. Given the information available, no issues were detected.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify consistent use of mc.rawConn in relevant parts of the codebase.
rg --type go 'mc\.rawConn' | grep -v 'mc.rawConn = mc.netConn'
# Assess potential side effects in error handling and connection cleanup.
rg --type go 'cleanup\(\)' | grep -B 3 -A 3 'mc\.rawConn'

Length of output: 232

connection.go (1)
  • 156-160: The renaming of the variable from nc to conn within the cleanup method improves code readability and clarity. Please ensure that the renamed variable is used consistently within the method and verify that there are no leftover references to the old variable name that could lead to errors.
Verification successful

The analysis of the provided context confirms that the renaming of the variable from nc to conn within the cleanup method and throughout connection.go has been applied consistently. There are no leftover references to the old variable name, indicating that the change has been implemented correctly and aligns with the goal of improving code readability and clarity.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify consistent use of the renamed variable within the method.
rg --type go 'nc' 'connection.go' | grep -B 3 -A 3 'cleanup\(\)'
# Ensure there are no leftover references to the old variable name.
rg --type go 'nc' 'connection.go'

Length of output: 3417

packets.go (1)
  • 351-356: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [1-1]

While the PR summary mentions changes related to the handling of mc.rawConn and mc.netConn, the provided code does not explicitly show these changes. It's important to ensure that the removal of the assignment between mc.rawConn and mc.netConn does not introduce any unintended side effects, especially in the context of TLS connections. Please verify that the network connection handling logic, particularly after the TLS handshake, remains correct and secure.

driver_test.go (1)
  • 3583-3612: The test function TestIssue1567 is designed to address a critical data race issue when TLS is enabled, specifically between mysqlConn.cleanup() and writeHandshakeResponsePacket. The test simulates network latency and connection timeouts to trigger the race condition. However, there are a few improvements and considerations to be made:
  1. The test disables connection pooling by setting MaxIdleConns to 0. This is a good practice for this test scenario as it ensures that each PingContext call creates a new connection, which is necessary to reproduce the race condition.
  2. The test uses a random timeout for the context passed to PingContext, based on the estimated round-trip time (RTT) to the server. This approach effectively simulates various network conditions. However, it's important to ensure that the RTT estimation is accurate and that the range of timeouts is appropriate for triggering the race condition.
  3. The loop count is reduced to 10 when running in short mode. This is reasonable for quick checks, but it's important to run the full test suite with a higher count (1000) to thoroughly test the fix.
  4. The test does not check the error returned by PingContext. While the primary goal is to trigger the race condition rather than check for successful pings, it might be useful to log or handle errors to ensure that the test setup is correct and that the database is reachable.

Overall, the test function seems well-designed to reproduce and verify the fix for the reported data race issue. It would be beneficial to add error handling for the PingContext call to ensure the test's reliability.

Consider adding error handling for the PingContext call to ensure that any unexpected issues during the test execution are caught and can be investigated.

@shogo82148 shogo82148 merged commit d7ddb8b into go-sql-driver:master Mar 22, 2024
32 checks passed
shogo82148 added a commit to shogo82148/mysql that referenced this pull request Mar 23, 2024
(cherry picked from commit d7ddb8b)
shogo82148 added a commit that referenced this pull request Mar 24, 2024
(cherry picked from commit d7ddb8b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Data race between mysqlConn.cleanup() and writeHandshakeResponsePacket
3 participants