title: Go mysql driver hangs on connect
body: <p>I met some weird situation with such simple code:</p>
  
  <pre><code>func InitDatabase(dataSourceName string) {
  
      var err error
      DBCon, err = sql.Open("mysql", dataSourceName)
  
      if err != nil {
          log.Panic(err)
      }
  
      log.Println("Ping...")
  
      if err = DBCon.Ping(); err != nil { // &lt;- it hangs here
          log.Println(":(")
      }
  
      log.Println(":)")
  }
  </code></pre>
  
  <p>I debugged and found, that it locks goroutine in this line (fd_poll_runtime.go, line #85), looks like it even doesn't connect:</p>
  
  <pre><code>res := runtime_pollWait(pd.runtimeCtx, mode)
  </code></pre>
  
  <p>It can wait here for ages, my mac becomes very hot.</p>
  
  <p>I made a small change:</p>
  
  <pre><code>ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  defer cancel()
  if err = DBCon.PingContext(ctx); err != nil {
      log.Println(":(")
  }
  
  log.Println(":)")
  </code></pre>
  
  <p>And it still hangs and timeout doesn't work.</p>
  
  <p>Problem is, that it worked yesterday (and, funny, it works on my friend's machine, same params) and now it doesn't even after reboot and after reinstalling mysql (which works perfectly in terminal/goland/mysqlworkbench).</p>
  
  <p>Go version 1.9, mysql driver: "github.com/go-sql-driver/mysql", mysql version: 5.7.20 Homebrew, macos version: High Sierra 10.13.1, I host mysql on localhost, port 3306.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>It finally worked. I copied init code to the clean project and it worked there. I started to comment out code, to find where is the problem. I found that I have this code in the imported package. Even if code from this package executes after database ping, but init code executes before and somehow it blocks <code>db.Ping()</code>:</p>
  
  <pre><code>func init() {
      for i := 1; i &lt;= maxWorkers; i++ {
          go func(i int) {
              for {
                  select {
                  case j, ok := &lt;-jobs:
                      if ok {
                          j.invoke()
                      } else {
                          // loop
                      }
                  default:
                      // loop
                  }
              }
          }(i)
      }
  }
  </code></pre>
  
  <p>I rewrote method name and executed it after database ping and everything worked. </p>
  
  <p>I don't know why it happened, but if you have the answer, feel free to post it here.</p>