Skip to content

[fix](be) Release workload group schedulers on shutdown#65112

Open
jacktengg wants to merge 1 commit into
apache:masterfrom
jacktengg:wt-memleak2
Open

[fix](be) Release workload group schedulers on shutdown#65112
jacktengg wants to merge 1 commit into
apache:masterfrom
jacktengg:wt-memleak2

Conversation

@jacktengg

@jacktengg jacktengg commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: close #xxx

Related PR: #xxx

Problem Summary:

There are smart pointer cycle references:

  ResourceContext
    -> shared_ptr<WorkloadGroup>
    -> WorkloadGroup::_task_sched
    -> HybridTaskScheduler
    -> TaskScheduler::_task_queue
    -> MultiCoreTaskQueue
    -> PriorityTaskQueue
    -> SubTaskQueue::_queue
    -> shared_ptr<PipelineTask>
    -> ScanLocalState / ScannerContext / ScanTask / operator local state
    -> shared_ptr<ResourceContext>

and

ResourceContext
    -> WorkloadGroup
    -> _scan_task_sched / _remote_scan_task_sched
    -> TaskExecutorSimplifiedScanScheduler
    -> queued scan task / split runner
    -> ScannerContext / ScanTask
    -> shared_ptr<ResourceContext>

Graceful shutdown with enable_graceful_exit_check could report ASAN/LSan leaks after exec env destroy. The leak stacks showed pending pipeline tasks in MultiCoreTaskQueue, scanner splits in TimeSharingTaskExecutor, memtable flush tasks in workload group ThreadPool, and QueryContext/ResourceContext retaining WorkloadGroup.

ExecEnv stopped workload group schedulers before fragment and load resources were released, and WorkloadGroupMgr::stop only stopped scheduler threads without destroying scheduler objects or their queued resources. Pending queues and executor task maps could therefore retain query/load resources until process exit. This change clears pending pipeline task queues on close, makes queue close and submit mutually exclusive, only marks tasks runnable after a successful enqueue, releases scheduler-owned queues after fragment/load resources have stopped.

Release note

None

Check List (For Author)

  • Test

    • Regression test
    • Unit Test
    • Manual test (add detailed scripts or steps below)
    • No need to test or manual test. Explain why:
      • This is a refactor/code format and no logic has been changed.
      • Previous test can cover this change.
      • No code files have been changed.
      • Other reason
  • Behavior changed:

    • No.
    • Yes.
  • Does this need documentation?

    • No.
    • Yes.

Check List (For Reviewer who merge this PR)

  • Confirm the release note
  • Confirm test cases
  • Confirm document
  • Add branch pick label

@hello-stephen

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@jacktengg

Copy link
Copy Markdown
Contributor Author

/review

@jacktengg

Copy link
Copy Markdown
Contributor Author

run buildall

yiguolei
yiguolei previously approved these changes Jul 1, 2026
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

PR approved by at least one committer and no changes requested.

@github-actions github-actions Bot added approved Indicates a PR has been approved by one committer. reviewed labels Jul 1, 2026
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

PR approved by anyone and no changes requested.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review result: request changes.

Critical checkpoint conclusions:

  • Goal/test proof: the PR targets graceful-exit retention of workload-group schedulers and queued resources, but the pipeline queue drain can still miss a shutdown-time enqueue, so the fix is incomplete.
  • Scope: the patch is small and focused on shutdown cleanup.
  • Concurrency/lifecycle: one blocking issue remains in PriorityTaskQueue::close() versus push() serialization. Shutdown ordering for the workload-group destructive stop was reviewed.
  • Config/compatibility/persistence: no new config, protocol, storage format, or persistence compatibility concern was found.
  • Parallel paths: pipeline scheduler, time-sharing scan scheduler, memtable flush pool, workload-group drop, and final ExecEnv destroy paths were checked.
  • Tests/style: no test or expected-output files are changed. I did not run BE tests locally in this review. Current PR checks show Clang Formatter passing; BE UT (macOS) is failing in GitHub checks.
  • User focus: no additional user-provided focus points.

Subagent conclusions: optimizer-rewrite proposed OPT-001, accepted as MAIN-001 and submitted inline. tests-session-config proposed TSC-001; I dismissed it with shutdown-order evidence because the known dynamic config entry points are stopped and joined before WorkloadGroupMgr::stop() performs the new destructive reset. Convergence round 1 ended with both live subagents replying NO_NEW_VALUABLE_FINDINGS for the final ledger/comment set.

Comment thread be/src/exec/pipeline/task_queue.cpp
@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-H: Total hot run time: 29537 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit f0aa02156d32496e658b9e7a8142cc2e0ac95b32, data reload: false

------ Round 1 ----------------------------------
============================================
q1	17594	4071	4033	4033
q2	2004	323	197	197
q3	10296	1432	830	830
q4	4698	471	336	336
q5	7539	847	578	578
q6	192	171	134	134
q7	765	835	626	626
q8	10299	1574	1573	1573
q9	6047	4456	4404	4404
q10	6828	1808	1551	1551
q11	505	344	315	315
q12	712	562	422	422
q13	18119	3379	2749	2749
q14	267	254	233	233
q15	q16	783	776	704	704
q17	987	1089	1041	1041
q18	7325	5761	5506	5506
q19	1168	1275	1001	1001
q20	732	671	560	560
q21	5583	2611	2446	2446
q22	428	349	298	298
Total cold run time: 102871 ms
Total hot run time: 29537 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	4334	4275	4275	4275
q2	286	314	207	207
q3	4598	5052	4384	4384
q4	2034	2135	1349	1349
q5	4427	4277	4337	4277
q6	233	173	126	126
q7	1732	2105	1621	1621
q8	2473	2146	2048	2048
q9	7855	7760	7812	7760
q10	4825	4747	4292	4292
q11	542	430	484	430
q12	742	776	538	538
q13	3176	3562	2979	2979
q14	300	301	280	280
q15	q16	765	759	644	644
q17	1351	1321	1324	1321
q18	7916	7288	6947	6947
q19	1083	1092	1121	1092
q20	2207	2203	1946	1946
q21	5203	4516	4293	4293
q22	509	450	401	401
Total cold run time: 56591 ms
Total hot run time: 51210 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 172926 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpcds-tools
TPC-DS sf100 test result on commit f0aa02156d32496e658b9e7a8142cc2e0ac95b32, data reload: false

query5	4314	623	489	489
query6	468	232	209	209
query7	4833	624	342	342
query8	337	197	171	171
query9	8760	3977	4007	3977
query10	485	331	294	294
query11	5938	2333	2122	2122
query12	153	101	113	101
query13	1282	626	448	448
query14	6225	5266	4933	4933
query14_1	4323	4302	4275	4275
query15	216	202	187	187
query16	1020	455	457	455
query17	1145	716	587	587
query18	2476	480	364	364
query19	209	205	160	160
query20	111	110	110	110
query21	233	157	130	130
query22	13497	13626	13392	13392
query23	17349	16410	16100	16100
query23_1	16268	16296	16223	16223
query24	7653	1794	1284	1284
query24_1	1321	1286	1270	1270
query25	564	462	406	406
query26	1324	375	211	211
query27	2585	608	372	372
query28	4476	2021	2009	2009
query29	1110	647	510	510
query30	342	260	229	229
query31	1122	1095	983	983
query32	113	63	62	62
query33	526	330	259	259
query34	1174	1122	652	652
query35	760	778	666	666
query36	1402	1383	1250	1250
query37	156	105	93	93
query38	1894	1741	1641	1641
query39	928	920	878	878
query39_1	881	876	882	876
query40	245	159	142	142
query41	64	63	65	63
query42	93	90	93	90
query43	319	323	279	279
query44	1408	775	779	775
query45	198	197	188	188
query46	1133	1168	729	729
query47	2405	2310	2264	2264
query48	373	414	300	300
query49	581	417	320	320
query50	1086	421	345	345
query51	4435	4369	4333	4333
query52	81	84	73	73
query53	270	270	199	199
query54	271	226	213	213
query55	73	69	66	66
query56	287	278	302	278
query57	1452	1435	1324	1324
query58	274	273	247	247
query59	1574	1631	1432	1432
query60	298	273	244	244
query61	153	154	150	150
query62	678	648	575	575
query63	248	207	207	207
query64	2534	743	630	630
query65	4833	4781	4763	4763
query66	1878	502	415	415
query67	29593	29508	28761	28761
query68	3161	1511	921	921
query69	396	294	274	274
query70	1060	1006	963	963
query71	337	319	309	309
query72	3094	2613	2350	2350
query73	832	800	438	438
query74	5094	4971	4773	4773
query75	2598	2578	2203	2203
query76	2323	1170	765	765
query77	343	387	286	286
query78	12469	12520	11837	11837
query79	1405	1152	740	740
query80	1281	531	470	470
query81	517	315	277	277
query82	584	158	121	121
query83	364	325	300	300
query84	286	161	127	127
query85	967	604	510	510
query86	425	300	279	279
query87	1821	1805	1753	1753
query88	3700	2832	2769	2769
query89	457	417	350	350
query90	1914	201	195	195
query91	198	181	160	160
query92	63	61	57	57
query93	1566	1572	956	956
query94	715	371	330	330
query95	782	495	561	495
query96	1046	788	364	364
query97	2699	2715	2592	2592
query98	212	214	197	197
query99	1186	1152	1029	1029
Total cold run time: 259046 ms
Total hot run time: 172926 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
ClickBench: Total hot run time: 25.21 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit f0aa02156d32496e658b9e7a8142cc2e0ac95b32, data reload: false

query1	0.00	0.00	0.00
query2	0.10	0.06	0.05
query3	0.26	0.14	0.14
query4	1.62	0.14	0.14
query5	0.24	0.25	0.22
query6	1.25	1.07	1.07
query7	0.04	0.01	0.01
query8	0.06	0.04	0.04
query9	0.37	0.32	0.30
query10	0.56	0.55	0.54
query11	0.20	0.15	0.15
query12	0.19	0.15	0.15
query13	0.47	0.46	0.47
query14	1.02	1.01	0.99
query15	0.61	0.60	0.58
query16	0.31	0.31	0.33
query17	1.03	1.08	1.11
query18	0.23	0.21	0.22
query19	2.08	1.98	2.00
query20	0.02	0.01	0.01
query21	15.45	0.21	0.13
query22	4.92	0.05	0.05
query23	16.12	0.31	0.13
query24	3.00	0.44	0.31
query25	0.10	0.06	0.04
query26	0.72	0.22	0.14
query27	0.03	0.04	0.03
query28	3.60	0.96	0.53
query29	12.48	4.37	3.44
query30	0.27	0.16	0.16
query31	2.79	0.61	0.31
query32	3.22	0.59	0.48
query33	3.18	3.26	3.24
query34	15.64	4.23	3.50
query35	3.52	3.51	3.51
query36	0.54	0.41	0.41
query37	0.09	0.07	0.06
query38	0.05	0.04	0.04
query39	0.04	0.03	0.03
query40	0.18	0.16	0.15
query41	0.08	0.03	0.03
query42	0.04	0.03	0.03
query43	0.04	0.04	0.03
Total cold run time: 96.76 s
Total hot run time: 25.21 s

@jacktengg

Copy link
Copy Markdown
Contributor Author

run nonConcurrent 10

Graceful shutdown with enable_graceful_exit_check could report ASAN/LSan leaks after exec env destroy. The leak stacks showed pending pipeline tasks in MultiCoreTaskQueue, scanner splits in TimeSharingTaskExecutor, memtable flush tasks in workload group ThreadPool, and QueryContext/ResourceContext retaining WorkloadGroup. ExecEnv stopped workload group schedulers before fragment and load resources were released, and WorkloadGroupMgr::stop only stopped scheduler threads without destroying scheduler objects or their queued resources. Pending queues and executor task maps could therefore retain query/load resources until process exit. This change clears pending pipeline task queues on close, makes queue close and submit mutually exclusive, only marks tasks runnable after a successful enqueue, releases scheduler-owned queues after fragment/load resources have stopped.
@jacktengg

Copy link
Copy Markdown
Contributor Author

/review

@jacktengg

Copy link
Copy Markdown
Contributor Author

run buildall

@github-actions github-actions Bot removed the approved Indicates a PR has been approved by one committer. label Jul 1, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review summary for PR #65112 at e111072f.

Opinion: no additional blocking issue found in the current head. I did not submit a duplicate for the existing PriorityTaskQueue::close()/push() thread; the current diff now checks _closed under _work_size_mutex before enqueueing.

Critical checkpoint conclusions:

  • Goal/test: The PR targets shutdown-time ASAN/LSan retention by draining pipeline queues, stopping workload group schedulers before FragmentMgr cleanup, and delaying scheduler object destruction until fragment/load resources stop submitting cleanup work. The current code matches that goal. No new test was added.
  • Scope: The changed files are focused on BE workload group scheduler shutdown, queue draining, and idempotent scheduler stop behavior.
  • Concurrency/lifecycle: PriorityTaskQueue::push() and close() now serialize submit/close with _work_size_mutex; queue-held tasks are released after dropping the queue lock. Scan scheduler and TimeSharingTaskExecutor stops are idempotent, and scheduler object reset is delayed until after FragmentMgr/load resources stop.
  • Parallel paths: Pipeline queues, local and remote scan scheduler variants, time-sharing scan executor cleanup, and memtable flush pool shutdown/reset were checked.
  • Config/compat/persistence: No new config, protocol, storage format, or EditLog compatibility impact.
  • Tests/CI: Local build/test was not runnable in this checkout because .worktree_initialized and thirdparty/installed are absent. git diff --check on the PR changed files passed. GitHub formatter/checkstyle passed. The macOS BE UT job failed before running tests because the runner had Java 25 while the job requires JDK 17; larger compile/BE UT contexts were still pending at review time.
  • User focus: No additional user-provided review focus.

Subagent conclusions: Initial optimizer-rewrite and tests-session-config subagent passes both reported NO_NEW_VALUABLE_FINDINGS; convergence round 1 ended with both live subagents again replying NO_NEW_VALUABLE_FINDINGS for this same ledger and proposed final comment set.

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-H: Total hot run time: 29302 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit e111072fe69bebbc704b16cc455b0b861493b9c3, data reload: false

------ Round 1 ----------------------------------
============================================
q1	17832	4047	3987	3987
q2	1997	306	194	194
q3	10311	1398	840	840
q4	4684	465	340	340
q5	7498	872	578	578
q6	204	169	134	134
q7	761	831	609	609
q8	10075	1546	1561	1546
q9	6009	4368	4399	4368
q10	6785	1815	1519	1519
q11	498	351	323	323
q12	745	541	430	430
q13	18085	3409	2751	2751
q14	274	271	231	231
q15	q16	784	773	703	703
q17	1391	1112	906	906
q18	6926	5806	5530	5530
q19	1489	1276	1045	1045
q20	794	698	531	531
q21	5920	2666	2440	2440
q22	430	359	297	297
Total cold run time: 103492 ms
Total hot run time: 29302 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	4323	4263	4238	4238
q2	283	313	205	205
q3	4655	4928	4444	4444
q4	2080	2151	1370	1370
q5	4433	4278	4314	4278
q6	228	177	125	125
q7	1973	1963	1650	1650
q8	2475	2142	2088	2088
q9	7812	7868	7926	7868
q10	4726	4693	4329	4329
q11	555	538	375	375
q12	759	755	560	560
q13	3296	3533	2959	2959
q14	303	314	271	271
q15	q16	705	737	669	669
q17	1333	1328	1306	1306
q18	8054	7168	7066	7066
q19	1088	1087	1128	1087
q20	2234	2201	1937	1937
q21	5256	4530	4452	4452
q22	518	446	401	401
Total cold run time: 57089 ms
Total hot run time: 51678 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 173310 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpcds-tools
TPC-DS sf100 test result on commit e111072fe69bebbc704b16cc455b0b861493b9c3, data reload: false

query5	4301	636	475	475
query6	452	229	196	196
query7	4836	595	346	346
query8	335	191	171	171
query9	8739	3982	3960	3960
query10	472	343	302	302
query11	5920	2353	2161	2161
query12	162	104	100	100
query13	1311	607	424	424
query14	6267	5332	4974	4974
query14_1	4306	4310	4297	4297
query15	219	203	186	186
query16	983	487	437	437
query17	1131	743	595	595
query18	2514	484	360	360
query19	210	195	151	151
query20	110	111	109	109
query21	237	160	136	136
query22	13627	13690	13432	13432
query23	17423	16694	16142	16142
query23_1	16379	16249	16230	16230
query24	7537	1783	1314	1314
query24_1	1326	1318	1311	1311
query25	578	475	390	390
query26	1324	372	231	231
query27	2545	606	373	373
query28	4452	1987	1984	1984
query29	1113	603	492	492
query30	334	258	227	227
query31	1109	1112	983	983
query32	108	60	57	57
query33	506	325	247	247
query34	1179	1135	631	631
query35	768	783	661	661
query36	1403	1387	1208	1208
query37	159	103	92	92
query38	1877	1708	1632	1632
query39	929	922	900	900
query39_1	857	894	875	875
query40	244	156	147	147
query41	66	61	61	61
query42	96	89	91	89
query43	323	320	277	277
query44	1410	786	767	767
query45	194	196	182	182
query46	1053	1194	763	763
query47	2391	2348	2213	2213
query48	407	391	299	299
query49	583	445	319	319
query50	1073	432	342	342
query51	4396	4401	4464	4401
query52	87	86	73	73
query53	256	274	200	200
query54	275	224	206	206
query55	75	69	72	69
query56	278	294	280	280
query57	1439	1435	1356	1356
query58	282	261	253	253
query59	1598	1648	1434	1434
query60	309	272	247	247
query61	150	150	152	150
query62	696	648	590	590
query63	245	200	208	200
query64	2516	765	596	596
query65	4876	4808	4801	4801
query66	1868	501	377	377
query67	29773	29631	29593	29593
query68	3226	1517	1012	1012
query69	405	299	265	265
query70	1089	994	973	973
query71	330	342	303	303
query72	2912	2617	2276	2276
query73	869	760	429	429
query74	5070	4986	4761	4761
query75	2619	2585	2250	2250
query76	2338	1187	774	774
query77	359	394	286	286
query78	12379	12424	11752	11752
query79	1295	1115	743	743
query80	634	542	449	449
query81	463	327	288	288
query82	450	161	118	118
query83	408	309	289	289
query84	285	166	131	131
query85	995	609	522	522
query86	364	286	284	284
query87	1818	1831	1767	1767
query88	3700	2795	2757	2757
query89	453	413	362	362
query90	1872	194	193	193
query91	205	197	159	159
query92	63	61	54	54
query93	1574	1546	947	947
query94	520	370	322	322
query95	799	588	486	486
query96	1074	793	353	353
query97	2710	2703	2570	2570
query98	222	207	198	198
query99	1176	1127	1035	1035
Total cold run time: 258030 ms
Total hot run time: 173310 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
ClickBench: Total hot run time: 25.34 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit e111072fe69bebbc704b16cc455b0b861493b9c3, data reload: false

query1	0.01	0.01	0.00
query2	0.10	0.05	0.04
query3	0.26	0.14	0.14
query4	1.61	0.14	0.14
query5	0.23	0.21	0.23
query6	1.24	1.08	1.08
query7	0.03	0.01	0.01
query8	0.06	0.04	0.03
query9	0.36	0.30	0.31
query10	0.53	0.58	0.59
query11	0.20	0.15	0.14
query12	0.19	0.14	0.16
query13	0.48	0.46	0.48
query14	1.01	1.01	1.00
query15	0.60	0.59	0.59
query16	0.33	0.31	0.33
query17	1.11	1.10	1.11
query18	0.22	0.20	0.21
query19	2.06	2.02	2.05
query20	0.02	0.01	0.02
query21	15.43	0.22	0.13
query22	4.91	0.05	0.05
query23	16.13	0.32	0.12
query24	2.94	0.41	0.32
query25	0.12	0.05	0.04
query26	0.73	0.21	0.15
query27	0.04	0.04	0.04
query28	3.60	0.90	0.52
query29	12.49	4.32	3.46
query30	0.27	0.16	0.16
query31	2.77	0.60	0.32
query32	3.22	0.61	0.49
query33	3.21	3.21	3.23
query34	15.73	4.21	3.48
query35	3.48	3.54	3.56
query36	0.56	0.44	0.44
query37	0.09	0.07	0.07
query38	0.05	0.04	0.03
query39	0.04	0.03	0.03
query40	0.18	0.17	0.15
query41	0.10	0.03	0.02
query42	0.04	0.03	0.03
query43	0.05	0.04	0.04
Total cold run time: 96.83 s
Total hot run time: 25.34 s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants