MySqlConnector
MySqlConnector copied to clipboard
Verify server ID before KILL QUERY to prevent cross-server cancellation
In layer 4 load-balanced MySQL environments, MySqlCommand.Cancel could potentially cancel queries on the wrong server if connection IDs are reused across different MySQL servers. The original implementation only used IP address connection targeting, but this may not be sufficient in complex load-balanced scenarios.
Solution
This PR adds server identity verification using MySQL's @@server_uuid and @@server_id system variables to ensure KILL QUERY commands target the correct server.
Key Changes
-
Server Identification Storage
- Added
ServerUuidandServerIdproperties toServerSession - Server UUID is preferred (more unique) over server ID
- Falls back to server ID for MySQL versions < 5.6
- Added
-
Connection Enhancement
- Added
GetServerIdentificationAsync()to query server identification during connection establishment - Queries
SELECT @@server_uuid, @@server_id(MySQL 5.6+) orSELECT @@server_id(older versions) - Robust error handling with graceful fallback
- Added
-
Cancellation Verification
- Modified
DoCancel()to verify server identity before executingKILL QUERY - Aborts cancellation with warning log if server identities don't match
- Maintains backward compatibility when no identification is available
- Modified
Example Scenarios
Same server (allowed):
Original connection: Server UUID=abc-123, Server ID=1
Kill connection: Server UUID=abc-123, Server ID=1
Result: ✓ KILL QUERY executed
Different server (blocked):
Original connection: Server UUID=abc-123, Server ID=1
Kill connection: Server UUID=def-456, Server ID=2
Result: ✗ Cancellation aborted, warning logged
Compatibility
- ✅ No breaking changes to existing APIs
- ✅ Backward compatible with older MySQL versions
- ✅ Graceful handling of unsupported server variables
- ✅ Existing code requires no modifications
Testing
- Added comprehensive unit tests for server identity verification logic
- Added integration tests for end-to-end cancellation behavior
- Validated backward compatibility with older MySQL versions
- Tested error handling and fallback scenarios
The implementation ensures KILL QUERY commands are safe and reliable in load-balanced MySQL environments while maintaining full backward compatibility.
Fixes #1574.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.