Skip to content

Write to closed Unix socket on auth failure causes "broken pipe" error #389

Closed
@daniel-nichter

Description

@daniel-nichter

If connecting to MySQL using a socket file and authentication fails, the driver prints an error like

[MySQL] 2015/12/06 10:18:22 packets.go:118: write unix ->/var/lib/mysql.sock: write: broken pipe

to STDERR because the driver tries to send COM_QUIT on auth failure but MySQL has already closed the connection: https://dev.mysql.com/doc/internals/en/authentication-fails.html

The error is superficial [1], but I've become tired of people asking me "what does it mean?" :-) so I'm attaching a PR that fixes it by not sending COM_QUIT until successfully authenticated, else just close the network connection and do the usual internal cleanup (unset vars, etc.).

[1] Superficial to developers, but to users it raises questions because STDERR keeps us up at night.

Reproduce like:

package main

import (
    "log"
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)

func main() {
    db, err := sql.Open("mysql", "daniel:wrong@unix(/usr/local/var/mysql/mysql.sock)/")
    if err != nil {
        log.Fatal(err)
    }
    if err := db.Ping(); err != nil {
        log.Fatal(err)
    }
}

Output:

[MySQL] 2015/12/06 12:12:40 packets.go:118: write unix ->/usr/local/var/mysql/mysql.sock: write: broken pipe
2015/12/06 12:12:40 Error 1045: Access denied for user 'daniel'@'localhost' (using password: YES)

Using latest master @ d512f20. Iirc, this issue has always existed in the diver. Not affected by Go version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions