Skip to content

Deadlock on DDL #809

@joelebaron

Description

@joelebaron

Describe the bug
When running a sql script with many (more than 6) statements that do not return output, I get a deadlock.

Exception message: fatal error: all goroutines are asleep - deadlock!
Stack trace:
goroutine 1 [select]:
github.com/golang-sql/sqlexp.(*ReturnMessage).Message(0xc000056108, {0x175c4a0, 0x1a13ac0})
	C:/Users/joe/go/pkg/mod/github.com/golang-sql/sqlexp@v0.1.0/messages.go:27 +0xe7
main.main()
	c:/scripts/GoTest/goTest.go:67 +0x67b

goroutine 7 [select]:
database/sql.(*DB).connectionOpener(0xc0000209c0, {0x175c3f8, 0xc0002c2050})
	C:/Program Files/Go/src/database/sql/sql.go:1261 +0xbe
created by database/sql.OpenDB in goroutine 1
	C:/Program Files/Go/src/database/sql/sql.go:841 +0x305

goroutine 18 [chan receive]:
database/sql.(*Tx).awaitDone(0xc000096000)
	C:/Program Files/Go/src/database/sql/sql.go:2212 +0x31
created by database/sql.(*DB).beginDC in goroutine 1
	C:/Program Files/Go/src/database/sql/sql.go:1925 +0x4d0

goroutine 13 [chan send]:
github.com/denisenkom/go-mssqldb.processSingleResponse({0x175c3f8, 0xc0002c22d0}, 0xc00017e5a0, 0xc0002d85b0, {0x0, 0x0, 0xc000056108})
	C:/Users/optima.josephl/go/pkg/mod/github.com/denisenkom/go-mssqldb@v0.12.3/token.go:735 +0x1d7c
created by github.com/denisenkom/go-mssqldb.startReading in goroutine 1
	C:/Users/optima.josephl/go/pkg/mod/github.com/denisenkom/go-mssqldb@v0.12.3/token.go:824 +0x1ce

goroutine 14 [select]:
database/sql.(*Rows).awaitDone(0xc00017e780, {0x175c4a0, 0x1a13ac0}, {0x175c3f8, 0xc000092000}, {0x175c3f8, 0xc0002c23c0})
	C:/Program Files/Go/src/database/sql/sql.go:3009 +0x14c
created by database/sql.(*Rows).initContextClose in goroutine 1
	C:/Program Files/Go/src/database/sql/sql.go:2996 +0x2e5

To Reproduce

package main

import (
	"context"
	"database/sql"
	"fmt"
	"log"
	"strings"

	mssql "github.com/denisenkom/go-mssqldb"
	"github.com/golang-sql/sqlexp"
)

const (
	msgQuery = `
	DROP TABLE IF EXISTS #joe1;
	DROP TABLE IF EXISTS #joe1;
	DROP TABLE IF EXISTS #joe1;
	DROP TABLE IF EXISTS #joe1;
	DROP TABLE IF EXISTS #joe1;
	DROP TABLE IF EXISTS #joe1;
	DROP TABLE IF EXISTS #joe1;
	DROP TABLE IF EXISTS #joe1;
	DROP TABLE IF EXISTS #joe1;
	`
)

// This example shows the usage of sqlexp/Messages
func main() {

	connString := "sqlserver://localhost?database=master&trusted_connection=true"

	// Create a new connector object by calling NewConnector
	connector, err := mssql.NewConnector(connString)
	if err != nil {
		log.Println(err)
		return
	}

	// Pass connector to sql.OpenDB to get a sql.DB object
	db := sql.OpenDB(connector)
	db.SetMaxOpenConns(10)
	defer db.Close()
	retmsg := &sqlexp.ReturnMessage{}

	tx, _ := db.Begin()
	defer tx.Rollback()

	ctx := context.Background()

	rows, err := tx.QueryContext(ctx, msgQuery, retmsg)
	if err != nil {
		log.Fatalf("QueryContext failed: %v", err)
	}
	active := true
	for active {
		msg := retmsg.Message(ctx)
		switch m := msg.(type) {
		case sqlexp.MsgNotice:
			fmt.Println(m.Message)
		case sqlexp.MsgNext:
			inresult := true
			for inresult {
				inresult = rows.Next()
				if inresult {
					cols, err := rows.Columns()
					if len(cols) == 0 {
						continue
					}

					columnPointers := make([]any, len(cols))
					for i := range columnPointers {
						var col any
						columnPointers[i] = &col // Each element in columnPointers holds a pointer to the column value
					}

					if err != nil {
						log.Fatalf("Columns failed: %v", err)
					}
					fmt.Println(strings.Join(cols, ","))

					if err = rows.Scan(columnPointers...); err == nil {
						values := make([]string, len(cols))
						for i, _ := range cols {
							values[i] = fmt.Sprintf("%v", *(columnPointers[i].(*any)))
						}
						fmt.Println(strings.Join(values, ","))

					}
				}
			}
		case sqlexp.MsgNextResultSet:
			active = rows.NextResultSet()
		case sqlexp.MsgError:
			fmt.Println("Error:", m.Error)
		case sqlexp.MsgRowsAffected:
			fmt.Println("Rows affected:", m.Count)
		}
	}
	tx.Commit()
}

Expected behavior
A clear and concise description of what you expected to happen.

Further technical details

SQL Server version: (e.g. SQL Server 2019)
Operating system: (e.g. Windows 10)
Table schema: N/A

Additional context
Tested with an without the transaction. Same result

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions