Skip to content

fix PING on compressed connections #1721

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 12, 2025
Merged

Conversation

methane
Copy link
Member

@methane methane commented Jun 12, 2025

Description

Add missing mc.syncSequence()

Fix #1718

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 sequence number handling for both compressed and uncompressed packets to ensure accurate tracking and synchronization.
    • Enhanced warning messages and debug output formatting for better clarity during troubleshooting.
    • Adjusted sequence reset logic for long data packet writes to improve reliability when sending large data.
  • Style

    • Debug and log outputs now include improved formatting for easier reading.

@methane methane requested a review from Copilot June 12, 2025 13:03
Copy link

coderabbitai bot commented Jun 12, 2025

Walkthrough

The changes unify and correct the handling of packet sequence numbers in both compressed and uncompressed MySQL protocol packet operations. Sequence number tracking is now consistent, with improved logging and synchronization after packet reads and writes, especially for compression scenarios. Debug output formatting is also improved.

Changes

File(s) Change Summary
compress.go Standardized use of compressSequence for compression packet sequence tracking; improved debug output.
packets.go Unified and clarified sequence number management for all packets; updated logging; adjusted sequence resets in command and long data packet writing; improved debug output formatting.

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant mysqlConn
    participant compIO
    participant MySQLServer

    App->>mysqlConn: Ping()
    mysqlConn->>compIO: writeCompressedPacket()
    compIO->>MySQLServer: Send compressed packet (with compressSequence)
    MySQLServer-->>compIO: Respond with compressed packet (with compressSequence)
    compIO->>mysqlConn: readCompressedPacket()
    mysqlConn->>App: Ping result

    App->>mysqlConn: QueryRow()
    mysqlConn->>compIO: writeCompressedPacket()
    compIO->>MySQLServer: Send compressed packet (with compressSequence)
    MySQLServer-->>compIO: Respond with compressed packet (with compressSequence)
    compIO->>mysqlConn: readCompressedPacket()
    mysqlConn->>App: Query result
Loading

Assessment against linked issues

Objective Addressed Explanation
Fix packet sequence number handling for compressed connections after Ping (Issue #1718)
Ensure queries after Ping with compress=true return expected rows (Issue #1718)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Poem

A hop, a skip, a sequence true,
Compression bugs are now askew!
Ping and query, side by side,
No more rows that try to hide.
With numbers neat and packets right,
The code now hops without a fright!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 using 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

@methane methane added the bug label Jun 12, 2025
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 pull request addresses the "fix PING on compressed connections" issue by adding missing mc.syncSequence() calls and adjusting packet sequence handling for both compressed and uncompressed connections. The key changes include:

  • Merging sequence synchronization logic in readPacket.
  • Inserting mc.syncSequence() calls after writing command packets.
  • Updating debug logging to improve clarity and correct sequence comparisons in both packets.go and compress.go.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packets.go Adjusted sequence handling, added mc.syncSequence() calls, and improved logging in writePacket and writeCommandPacket functions.
compress.go Corrected the debug check to compare against mc.compressSequence and refined logging in writeCompressedPacket.
Comments suppressed due to low confidence (3)

packets.go:67

  • For compressed connections, the original logic updated mc.compressSequence separately. Since compress.go relies on mc.compressSequence for debug checks, please verify that not updating it in readPacket (and instead always updating mc.sequence) does not introduce inconsistencies for compressed flows.
if seq != mc.sequence {

packets.go:1032

  • [nitpick] Resetting the packet sequence immediately after sending a COM_LONG_DATA packet changes the previous reset behavior; please confirm that this aligns with the protocol requirements for handling long data packets.
stmt.mc.resetSequence()

compress.go:116

  • The debug check now correctly compares against c.mc.compressSequence; however, ensure that the synchronization of this field is maintained across all packet reads to avoid potential mismatches.
if debug && compressionSequence != c.mc.compressSequence {

@@ -146,7 +141,7 @@ func (mc *mysqlConn) writePacket(data []byte) error {

// Write packet
if debug {
fmt.Printf("writePacket: size=%v seq=%v", size, mc.sequence)
fmt.Fprintf(os.Stderr, "writePacket: size=%v seq=%v\n", size, mc.sequence)
Copy link
Preview

Copilot AI Jun 12, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider using a centralized logging mechanism instead of direct writes to os.Stderr to ensure consistency across the project.

Copilot uses AI. Check for mistakes.

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 is only for debugging. debug is const and its value is false.

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: 0

🧹 Nitpick comments (5)
compress.go (3)

108-111: Debug log references the wrong sequence counter

After switching to compressSequence, the debug message still prints c.mc.sequence, which is now unrelated to the compressed-frame sequence. This will confuse anyone hunting sync issues.

- fmt.Printf("uncompress cmplen=%v uncomplen=%v pkt_cmp_seq=%v expected_cmp_seq=%v\n",
-     comprLength, uncompressedLength, compressionSequence, c.mc.sequence)
+ fmt.Printf("uncompress cmplen=%v uncomplen=%v pkt_cmp_seq=%v expected_cmp_seq=%v\n",
+     comprLength, uncompressedLength, compressionSequence, c.mc.compressSequence)

116-121: Missing newline in warning + minor typo

  1. The warning lacks a trailing \n, so it sticks to the next log line.
  2. “cmpress” is misspelled.
- fmt.Printf("WARN: unexpected cmpress seq nr: expected %v, got %v",
+ fmt.Printf("WARN: unexpected compress seq nr: expected %v, got %v\n",

200-204: Stick to one stream for debug output

Other debug prints were just redirected to os.Stderr; this one is still using the default Stdout. Redirect it as well for uniform logging and to avoid polluting normal output.

- fmt.Printf(
+ fmt.Fprintf(os.Stderr,
packets.go (2)

448-451: Call syncSequence() only on successful write

If writePacket fails we still invoke syncSequence(), which may desynchronise counters for a connection that is about to be discarded anyway.

-err = mc.writePacket(data)
-mc.syncSequence()
-return err
+if err := mc.writePacket(data); err != nil {
+    return err
+}
+mc.syncSequence()
+return nil

491-494: Same conditional-call issue as above

Apply the same guard around syncSequence() for the uint32 variant to avoid post-error side-effects.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between af56fba and 22cee6a.

📒 Files selected for processing (2)
  • compress.go (2 hunks)
  • packets.go (6 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packets.go (1)
errors.go (1)
  • ErrPktSyncMul (29-29)
⏰ Context from checks skipped due to timeout of 90000ms (20)
  • GitHub Check: test (windows-latest, 1.24, 9.0)
  • GitHub Check: test (windows-latest, 1.24, mariadb-10.11)
  • GitHub Check: test (ubuntu-latest, 1.22, 9.0)
  • GitHub Check: test (windows-latest, 1.24, 8.0)
  • GitHub Check: test (macos-latest, 1.24, mariadb-10.5)
  • GitHub Check: test (windows-latest, 1.24, mariadb-10.6)
  • GitHub Check: test (windows-latest, 1.24, 8.4)
  • GitHub Check: test (macos-latest, 1.24, mariadb-10.11)
  • GitHub Check: test (windows-latest, 1.24, 5.7)
  • GitHub Check: test (macos-latest, 1.24, mariadb-11.2)
  • GitHub Check: test (macos-latest, 1.24, 8.0)
  • GitHub Check: test (macos-latest, 1.24, 5.7)
  • GitHub Check: test (ubuntu-latest, 1.24, mariadb-10.11)
  • GitHub Check: test (ubuntu-latest, 1.24, mariadb-10.5)
  • GitHub Check: test (ubuntu-latest, 1.24, mariadb-11.4)
  • GitHub Check: test (ubuntu-latest, 1.24, mariadb-11.2)
  • GitHub Check: test (ubuntu-latest, 1.24, 8.0)
  • GitHub Check: test (ubuntu-latest, 1.24, mariadb-11.1)
  • GitHub Check: test (ubuntu-latest, 1.24, 8.4)
  • GitHub Check: Analyze (go)
🔇 Additional comments (1)
packets.go (1)

66-80: Sequence-mismatch silently ignored on compressed links

invalidSequence is no longer set when mc.compress == true.
A real out-of-sync condition on the payload sequence will now be tolerated silently on compressed connections, potentially masking corruption.

Please double-check the MySQL spec and confirm this is really safe; if not, keep the old behaviour or add a comment explaining why it’s harmless.

@methane methane merged commit bf7afb7 into go-sql-driver:master Jun 12, 2025
38 checks passed
methane added a commit to methane/mysql that referenced this pull request Jun 12, 2025
@coveralls
Copy link

Coverage Status

coverage: 82.508% (+0.1%) from 82.413%
when pulling 22cee6a on methane:fix-ping
into af56fba on go-sql-driver:master.

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.

With compress=true, cannot query DB after calling Ping
2 participants