{"id":"492505617e152d68","repo":"go-sql-driver/mysql","slug":"commands-out-of-sync-did-you-run-multiple-stateme","errorCode":null,"errorMessage":"commands out of sync. Did you run multiple statements at once?","messagePattern":"commands out of sync\\. Did you run multiple statements at once\\?","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"errors.go","lineNumber":29,"sourceCode":"import (\n\t\"errors\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n)\n\n// Various errors the driver might return. Can change between driver versions.\nvar (\n\tErrInvalidConn       = errors.New(\"invalid connection\")\n\tErrMalformPkt        = errors.New(\"malformed packet\")\n\tErrNoTLS             = errors.New(\"TLS requested but server does not support TLS\")\n\tErrCleartextPassword = errors.New(\"this user requires clear text authentication. If you still want to use it, please add 'allowCleartextPasswords=1' to your DSN\")\n\tErrNativePassword    = errors.New(\"this user requires mysql native password authentication\")\n\tErrOldPassword       = errors.New(\"this user requires old password authentication. If you still want to use it, please add 'allowOldPasswords=1' to your DSN. See also https://github.com/go-sql-driver/mysql/wiki/old_passwords\")\n\tErrUnknownPlugin     = errors.New(\"this authentication plugin is not supported\")\n\tErrOldProtocol       = errors.New(\"MySQL server does not support required protocol 41+\")\n\tErrPktSync           = errors.New(\"commands out of sync. You can't run this command now\")\n\tErrPktSyncMul        = errors.New(\"commands out of sync. Did you run multiple statements at once?\")\n\tErrPktTooLarge       = errors.New(\"packet for query is too large. Try adjusting the `Config.MaxAllowedPacket`\")\n\tErrBusyBuffer        = errors.New(\"busy buffer\")\n\n\t// errBadConnNoWrite is used for connection errors where nothing was sent to the database yet.\n\t// If this happens first in a function starting a database interaction, it should be replaced by driver.ErrBadConn\n\t// to trigger a resend. Use mc.markBadConn(err) to do this.\n\t// See https://github.com/go-sql-driver/mysql/pull/302\n\terrBadConnNoWrite = errors.New(\"bad connection\")\n)\n\nvar defaultLogger = Logger(log.New(os.Stderr, \"[mysql] \", log.Ldate|log.Ltime))\n\n// Logger is used to log critical error messages.\ntype Logger interface {\n\tPrint(v ...any)\n}\n\n// NopLogger is a nop implementation of the Logger interface.","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/c426bd93799de0f0e094c8f0582872c529d0ed0a/errors.go#L11-L47","documentation":"ErrPktSyncMul is returned by readPacket (packets.go:74) when a sequence-number mismatch is detected while assembling a large (multi-part, >16MB) packet. Because the payload spans several packets, a desync mid-stream means the reassembled data would be corrupt, so the connection is closed and this sentinel returned.","triggerScenarios":"A query whose response exceeds maxPacketSize (16 MiB) is split across packets; if a sequence number is wrong mid-assembly (packets.go:72, prevData already non-empty), readPacket closes the connection and returns ErrPktSyncMul. Often co-occurs with multiStatements=1 results read incorrectly, or with a proxy corrupting large streams.","commonSituations":"Selecting very large rows/LOBs through a buggy proxy; using multiStatements and not iterating all result sets; concurrent access producing interleaved packets on a shared connection.","solutions":["Avoid sharing a connection across goroutines; drain and close rows/result sets properly.","If multiStatements is enabled, iterate every result with rows.NextResultSet() until false.","Page large reads so responses stay under a single packet, or rule out a corrupting intermediary (load balancer, sidecar)."],"exampleFix":"// before\ndb.QueryContext(ctx, \"SELECT ...; SELECT ...\", ) // multiStatements, results not drained\n// after\nrows, _ := db.QueryContext(ctx, q)\nfor rows.Next() { ... }\nfor rows.NextResultSet() {\n    for rows.Next() { ... }\n}\nrows.Close()","handlingStrategy":"try-catch","validationCode":"// If multiStatements=true, always drain all result sets:\nfor rows.NextResultSet() { for rows.Next() {} }","typeGuard":null,"tryCatchPattern":"if errors.Is(err, mysql.ErrPktSyncMul) {\n    // large-packet desync; connection is closed, surface and reconnect\n}","preventionTips":["Disable multiStatements unless you fully drain results.","Page large reads to stay under one packet.","Rule out a corrupting intermediary."],"tags":["protocol","concurrency","data"],"analyzedSha":"c426bd93799de0f0e094c8f0582872c529d0ed0a","analyzedAt":"2026-08-04T21:52:59.219Z","schemaVersion":2}