@@ -40,7 +40,14 @@ func checkPrepare(ctx context.Context, w workload.Workloader) {
4040func execute (timeoutCtx context.Context , w workload.Workloader , action string , threads , index int ) error {
4141 count := totalCount / threads
4242
43- ctx := w .InitThread (context .Background (), index )
43+ // For prepare and cleanup operations, use background context to avoid timeout constraints
44+ // Only check and run phases should be limited by timeout
45+ var ctx context.Context
46+ if action == "prepare" || action == "cleanup" {
47+ ctx = w .InitThread (context .Background (), index )
48+ } else {
49+ ctx = w .InitThread (timeoutCtx , index )
50+ }
4451 defer w .CleanupThread (ctx , index )
4552
4653 switch action {
@@ -58,21 +65,37 @@ func execute(timeoutCtx context.Context, w workload.Workloader, action string, t
5865 return w .Check (ctx , index )
5966 }
6067
68+ // This loop is only reached for "run" action since other actions return earlier
6169 for i := 0 ; i < count || count <= 0 ; i ++ {
70+ // Check if timeout has occurred before starting next query
71+ select {
72+ case <- timeoutCtx .Done ():
73+ if ! silence {
74+ fmt .Printf ("[%s] %s worker %d stopped due to timeout after %d iterations\n " ,
75+ time .Now ().Format ("2006-01-02 15:04:05" ), action , index , i )
76+ }
77+ return nil
78+ default :
79+ }
80+
6281 err := w .Run (ctx , index )
6382 if err != nil {
83+ // Check if the error is due to timeout/cancellation
84+ if timeoutCtx .Err () != nil {
85+ if ! silence {
86+ fmt .Printf ("[%s] %s worker %d stopped due to timeout: %v\n " ,
87+ time .Now ().Format ("2006-01-02 15:04:05" ), action , index , err )
88+ }
89+ return nil // Don't treat timeout as an error
90+ }
91+
6492 if ! silence {
6593 fmt .Printf ("[%s] execute %s failed, err %v\n " , time .Now ().Format ("2006-01-02 15:04:05" ), action , err )
6694 }
6795 if ! ignoreError {
6896 return err
6997 }
7098 }
71- select {
72- case <- timeoutCtx .Done ():
73- return nil
74- default :
75- }
7699 }
77100
78101 return nil
0 commit comments