@@ -3,6 +3,8 @@ package sqlserver
33import  (
44	"context" 
55	"database/sql" 
6+ 	"net" 
7+ 	"strconv" 
68	"time" 
79
810	regexp "github.com/wasilibs/go-re2" 
@@ -52,7 +54,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
5254		}
5355
5456		if  verify  {
55- 			isVerified , err  :=  ping (paramsUnsafe )
57+ 			isVerified , err  :=  ping (ctx ,  paramsUnsafe )
5658
5759			s1 .Verified  =  isVerified 
5860
@@ -71,7 +73,20 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
7173	return  results , nil 
7274}
7375
74- var  ping  =  func (config  msdsn.Config ) (bool , error ) {
76+ var  ping  =  func (ctx  context.Context , config  msdsn.Config ) (bool , error ) {
77+ 	// TCP connectivity check to prevent indefinite hangs 
78+ 	address  :=  net .JoinHostPort (config .Host , strconv .Itoa (int (config .Port )))
79+ 
80+ 	dialer  :=  & net.Dialer {
81+ 		Timeout : 3  *  time .Second ,
82+ 	}
83+ 
84+ 	tcpConn , err  :=  dialer .DialContext (ctx , "tcp" , address ) // respects context timeout 
85+ 	if  err  !=  nil  {
86+ 		return  false , err 
87+ 	}
88+ 	defer  tcpConn .Close ()
89+ 
7590	cleanConfig  :=  msdsn.Config {}
7691	cleanConfig .Host  =  config .Host 
7792	cleanConfig .Port  =  config .Port 
@@ -97,7 +112,7 @@ var ping = func(config msdsn.Config) (bool, error) {
97112		_  =  conn .Close ()
98113	}()
99114
100- 	err  =  conn .Ping () 
115+ 	err  =  conn .PingContext ( ctx )  // this doesn't seem to respect the context timeout 
101116	if  err  !=  nil  {
102117		return  false , err 
103118	}
0 commit comments