@@ -372,15 +372,15 @@ func (h *Handler) doQuery(
372372 bindings map [string ]* querypb.BindVariable ,
373373 callback func (* sqltypes.Result , bool ) error ,
374374 qFlags * sql.QueryFlags ,
375- ) (string , error ) {
376- sqlCtx , err := h .sm .NewContext (ctx , c , query )
375+ ) (remainder string , err error ) {
376+ var sqlCtx * sql.Context
377+ sqlCtx , err = h .sm .NewContext (ctx , c , query )
377378 if err != nil {
378379 return "" , err
379380 }
380381
381382 start := time .Now ()
382383
383- var remainder string
384384 var prequery string
385385 if parsed == nil {
386386 _ , inPreparedCache := h .e .PreparedDataCache .GetCachedStmt (sqlCtx .Session .ID (), query )
@@ -411,21 +411,24 @@ func (h *Handler) doQuery(
411411 sqlCtx .GetLogger ().Debugf ("Starting query" )
412412
413413 finish := observeQuery (sqlCtx , query )
414- defer finish (err )
414+ defer func () {
415+ finish (err )
416+ }()
415417
416418 sqlCtx .GetLogger ().Tracef ("beginning execution" )
417419
418420 // TODO: it would be nice to put this logic in the engine, not the handler, but we don't want the process to be
419421 // marked done until we're done spooling rows over the wire
420422 sqlCtx , err = sqlCtx .ProcessList .BeginQuery (sqlCtx , query )
421- defer func () {
422- if err != nil && ctx != nil {
423- sqlCtx .ProcessList .EndQuery (sqlCtx )
424- }
425- }()
423+ if err != nil {
424+ return remainder , err
425+ }
426+ defer sqlCtx .ProcessList .EndQuery (sqlCtx )
426427
428+ var schema sql.Schema
429+ var rowIter sql.RowIter
427430 qFlags .Set (sql .QFlagDeferProjections )
428- schema , rowIter , qFlags , err : = queryExec (sqlCtx , query , parsed , analyzedPlan , bindings , qFlags )
431+ schema , rowIter , qFlags , err = queryExec (sqlCtx , query , parsed , analyzedPlan , bindings , qFlags )
429432 if err != nil {
430433 sqlCtx .GetLogger ().WithError (err ).Warn ("error running query" )
431434 if verboseErrorLogging {
0 commit comments