Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection not closed when database name is incorrect #173

Open
jatinn opened this issue Feb 16, 2024 · 1 comment
Open

connection not closed when database name is incorrect #173

jatinn opened this issue Feb 16, 2024 · 1 comment

Comments

@jatinn
Copy link

jatinn commented Feb 16, 2024

Opened same issue in original over at denisenkom#798 and found this fork which seems to be more active however upon updating the package looks like the issue exists with this package as well

Describe the bug

If a database name is provided that does not exist, we get back an error however even though db.Close() is executed the network connection still remains open.

To Reproduce

package main

import (
	"context"
	"database/sql"
	"flag"
	"fmt"
	"os"
	"os/signal"

	_ "github.com/microsoft/go-mssqldb"
)

func main() {
	var (
		username = "sa"
		password = "Password123"
		hostname = "127.0.0.1"
		port     = 1433
		database = flag.String("db", "master", "database name")
		count    = flag.Int("count", 1, "number of calls to make to the database")
	)

	flag.Parse()

	dsn := fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s", username, password, hostname, port, *database)

	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
	defer stop()

	for i := 0; i < *count; i++ {
		if err := doQuery(ctx, dsn); err != nil {
			fmt.Println("error running query", i+1, err.Error())
		} else {
			fmt.Printf("run %d done\n", i+1)
		}
	}
	fmt.Println("waiting for ctrl+c to exit")
	<-ctx.Done()
}

func doQuery(ctx context.Context, dsn string) error {
	db, err := sql.Open("sqlserver", dsn)
	if err != nil {
		return fmt.Errorf("error open db %w", err)
	}
	defer db.Close()

	row := db.QueryRowContext(ctx, "SELECT 1")
	var result int
	if err := row.Scan(&result); err != nil {
		return fmt.Errorf("error scanning row %w", err)
	}

	return nil
}

If you provide a database name that exists then should see the following, before sending the exit signal if you look for connections to the database it will be in TIME_WAIT

❯ go run main.go -db exists
run 1 done
waiting for ctrl+c to exit

# new terminal
❯ netstat -an | grep '1.1433'
tcp4       0      0  127.0.0.1.60859        127.0.0.1.1433         TIME_WAIT

However if you provide a database name that does not exist then the connection remains ESTABLISHED even though it should have been closed.

❯ go run main.go -db unknown
error running query 1 error scanning row mssql: login error: Cannot open database "unknown" that was requested by the login. Using the user default database "master" instead.
waiting for ctrl+c to exit

# new terminal
❯ netstat -an | grep '1.1433'
tcp4       0      0  127.0.0.1.1433         127.0.0.1.60894        ESTABLISHED
tcp4       0      0  127.0.0.1.60894        127.0.0.1.1433         ESTABLISHED

Expected behavior
The network connection should be closed

Further technical details

SQL Server version: docker image mcr.microsoft.com/mssql/server:2022-latest
Operating system: macOS 13.6.3

@simonoff
Copy link

I have figths with a open connections for a while in my app and found that such case also applicable for the cases when user don't have access to DB.

parMaster added a commit to parMaster/go-mssqldb that referenced this issue Oct 9, 2024
parMaster added a commit to parMaster/go-mssqldb that referenced this issue Oct 11, 2024
shueybubbles pushed a commit that referenced this issue Oct 14, 2024
…224)

* connection not closed when database name is incorrect #173 fix

* test for leaked connections (connection not closed when database name is incorrect #173

* Checking the number of open connections from local_net_address only

* using sql.NullString for localNetAddr

* handling local_net_address==NULL correctly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants