Skip to content

Commit 06a7e3e

Browse files
authored
fixed error with missing sync() after parse/describe error (#42)
* Added test demonstrating a potential hang after errors found in github.com/xo/usql project. * fixed issue with missing sync() call on Parse/Describe() error made 0-length messages show up in log as intended
1 parent c51df91 commit 06a7e3e

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

connection.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,14 @@ func (v *connection) sendMessage(msg msgs.FrontEndMsg) error {
234234
if result == nil {
235235
if len(msgBytes) > 0 {
236236
_, result = v.conn.Write(msgBytes)
237-
238-
if result == nil {
239-
connectionLogger.Debug("-> " + msg.String())
240-
}
241237
}
242238
}
243239
}
244240

245241
if result != nil {
246242
connectionLogger.Error("-> FAILED SENDING "+msg.String()+": %v", result.Error())
243+
} else {
244+
connectionLogger.Debug("-> " + msg.String())
247245
}
248246

249247
return result
@@ -473,6 +471,10 @@ func (v *connection) authSendSHA512Password(extraAuthData []byte) error {
473471
return v.sendMessage(msg)
474472
}
475473

474+
func (v *connection) sync() error {
475+
return v.sendMessage(&msgs.FESyncMsg{})
476+
}
477+
476478
func (v *connection) lockSessionMutex() {
477479
v.sessMutex.Lock()
478480
}

driver_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,29 @@ func TestSTDINCopyWithStream(t *testing.T) {
677677
assertNoNext(t, rows)
678678
}
679679

680+
func TestHangAfterError(t *testing.T) {
681+
connDB := openConnection(t)
682+
defer closeConnection(t, connDB)
683+
684+
rows, err := connDB.QueryContext(ctx, "SELECT 1")
685+
defer rows.Close()
686+
687+
assertNoErr(t, err)
688+
assertNext(t, rows)
689+
assertNoNext(t, rows)
690+
691+
rows, err = connDB.QueryContext(ctx, "SELECT 1+'abcd'")
692+
693+
assertErr(t, err, "Invalid")
694+
695+
rows, err = connDB.QueryContext(ctx, "SELECT 2")
696+
defer rows.Close()
697+
698+
assertNoErr(t, err)
699+
assertNext(t, rows)
700+
assertNoNext(t, rows)
701+
}
702+
680703
var verticaUserName = flag.String("user", "dbadmin", "the user name to connect to Vertica")
681704
var verticaPassword = flag.String("password", os.Getenv("VERTICA_TEST_PASSWORD"), "Vertica password for this user")
682705
var verticaHostPort = flag.String("locator", "localhost:5433", "Vertica's host and port")

stmt.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func (s *stmt) Close() error {
8888
if s.parseState == parseStateParsed {
8989
closeMsg := &msgs.FECloseMsg{TargetType: msgs.CmdTargetTypeStatement, TargetName: s.preparedName}
9090

91-
s.conn.lockSessionMutex()
92-
defer s.conn.unlockSessionMutex()
91+
//s.conn.lockSessionMutex()
92+
//defer s.conn.unlockSessionMutex()
9393

9494
if err := s.conn.sendMessage(closeMsg); err != nil {
9595
return err
@@ -354,6 +354,7 @@ func (s *stmt) prepareAndDescribe() error {
354354

355355
switch msg := bMsg.(type) {
356356
case *msgs.BEErrorMsg:
357+
s.conn.sync()
357358
return msg.ToErrorType()
358359
case *msgs.BEParseCompleteMsg:
359360
s.parseState = parseStateParsed

0 commit comments

Comments
 (0)