Skip to content

query returns incorrect result when context done in the middle #50089

@D3Hunter

Description

@D3Hunter

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

suppose we have this patch added to help reproduce

diff --git a/pkg/executor/table_reader.go b/pkg/executor/table_reader.go
index 4d87861c95..fddbfe284a 100644
--- a/pkg/executor/table_reader.go
+++ b/pkg/executor/table_reader.go
@@ -250,6 +250,9 @@ func (e *TableReaderExecutor) Next(ctx context.Context, req *chunk.Chunk) error
                }
                return tableName
        }), e.ranges)
+       if e.columns[0].Name.L == "a" {
+               time.Sleep(5 * time.Second)
+       }
        if err := e.resultHandler.nextChunk(ctx, req); err != nil {
                return err
        }

then run this test

func TestIncorrectQuery(t *testing.T) {
	store := testkit.CreateMockStore(t)
	tk := testkit.NewTestKit(t, store)
	tk.MustExec("use test")
	tk.MustExec("create table t(a int)")
	tk.MustExec("insert into t values(1)")

	ctx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancelFunc()
	ctx = util.WithInternalSourceType(ctx, "scheduler")
	rs, err := tk.Session().ExecuteInternal(ctx, "select * from test.t")
	rows, err2 := session.ResultSetToStringSlice(ctx, tk.Session(), rs)
	t.Log("result&err: ", len(rows), err, err2)
}

might need to run it for a few times

    scheduler_test.go:720: result&err:  0 <nil> <nil>

here we don't return context error, if context cancelled in the middle the query returns incorrect result.
Query from normal client don't have issue, as we don't cancel context except when kill where it's handled.

case <-ctx.Done():
// We select the ctx.Done() in the thread of `Next` instead of in the worker to avoid the cost of `WithCancel`.
if atomic.CompareAndSwapUint32(&it.closed, 0, 1) {
close(it.finishCh)
}
exit = true
return

2. What did you expect to see? (Required)

got err: context deadline exceed

3. What did you see instead (Required)

no error, result set is empty

4. What is your TiDB version? (Required)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions