Commit 92922c6
authored
Gracefully Shutdown Foreground Server on Interrupt (#2927)
Summary
This pull request is a second attempt at #2863: Gracefully shutting down foreground servers when receiving a SIGINT.
This PR takes greater care to fix tests which became flaky with the original change.
Fixes #1855.
Fixed Tests and Their Changes
osv_mcp_server_test.go
This file has a test that became flaky, specifically the Running OSV MCP server in the foreground" tests. This is the only e2e test that exercises the foreground server like a user would. I made the following changes to the test/code to ensure it passes:
Update the test to document how SIGINT should behave rather than using thv stop/rm.
Simplify the handling of context cancellation in runForeground. Previously, we had two goroutines doing parallel shutdown work: workloadManager.RunWorkload and runForeground. runForeground calls RunWorkload so it's natural to block within runForeground until RunWorload returns. Previously, the two shutdown routines could race on modifying/deleting the workload.
Screenshot 2025-12-07 at 3 32 00 PM
fetch_mcp_server_test.go
This file and likely others exercise --foreground indirectly. The fetch tests are structured to stop a server with a shared name in between tests. Previously, thv stop sent a SIGINT to the background process thv restart --foreground that was spawned by thv run. When the background process received that signal, the signal handler we removed called os.Exit causing the process to exit and no longer babysit the workload.
The problem and solution
thv restart was intentionally coded to ignore context cancellation because we wanted a timeout around the restart's initial startup. Because thv restart ignore all context cancellation, it effectively ignored all SIGINTs and the process would continually resuscitate the server with stale state we were attempting to change across tests. The solution preserves the timeout on startup but ensures post-startup context cancellation is respected.1 parent 5084b21 commit 92922c6
File tree
6 files changed
+139
-98
lines changed- cmd/thv
- app
- pkg
- runner
- workloads
- test/e2e
6 files changed
+139
-98
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | 10 | | |
12 | | - | |
13 | 11 | | |
14 | 12 | | |
15 | 13 | | |
| |||
126 | 124 | | |
127 | 125 | | |
128 | 126 | | |
129 | | - | |
| 127 | + | |
130 | 128 | | |
131 | 129 | | |
132 | 130 | | |
| |||
138 | 136 | | |
139 | 137 | | |
140 | 138 | | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | 139 | | |
149 | 140 | | |
150 | 141 | | |
| |||
304 | 295 | | |
305 | 296 | | |
306 | 297 | | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | 298 | | |
314 | 299 | | |
315 | 300 | | |
316 | 301 | | |
317 | 302 | | |
318 | 303 | | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
328 | 315 | | |
| 316 | + | |
| 317 | + | |
329 | 318 | | |
330 | 319 | | |
331 | 320 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | | - | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| 50 | + | |
| 51 | + | |
50 | 52 | | |
51 | | - | |
| 53 | + | |
52 | 54 | | |
53 | 55 | | |
54 | 56 | | |
| |||
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
62 | | - | |
| 64 | + | |
63 | 65 | | |
64 | 66 | | |
65 | 67 | | |
| 68 | + | |
66 | 69 | | |
67 | 70 | | |
68 | 71 | | |
69 | 72 | | |
70 | | - | |
| 73 | + | |
71 | 74 | | |
| 75 | + | |
| 76 | + | |
72 | 77 | | |
73 | 78 | | |
74 | 79 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | 11 | | |
13 | | - | |
14 | 12 | | |
15 | 13 | | |
16 | 14 | | |
| |||
317 | 315 | | |
318 | 316 | | |
319 | 317 | | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
320 | 321 | | |
321 | 322 | | |
322 | 323 | | |
323 | 324 | | |
324 | | - | |
| 325 | + | |
325 | 326 | | |
326 | 327 | | |
327 | 328 | | |
328 | 329 | | |
329 | | - | |
| 330 | + | |
330 | 331 | | |
331 | 332 | | |
332 | 333 | | |
| |||
335 | 336 | | |
336 | 337 | | |
337 | 338 | | |
338 | | - | |
| 339 | + | |
339 | 340 | | |
340 | 341 | | |
341 | 342 | | |
| |||
354 | 355 | | |
355 | 356 | | |
356 | 357 | | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | 358 | | |
362 | 359 | | |
363 | 360 | | |
| |||
399 | 396 | | |
400 | 397 | | |
401 | 398 | | |
402 | | - | |
403 | | - | |
| 399 | + | |
| 400 | + | |
404 | 401 | | |
405 | 402 | | |
406 | 403 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
872 | 872 | | |
873 | 873 | | |
874 | 874 | | |
875 | | - | |
| 875 | + | |
876 | 876 | | |
877 | 877 | | |
878 | 878 | | |
| |||
884 | 884 | | |
885 | 885 | | |
886 | 886 | | |
887 | | - | |
| 887 | + | |
888 | 888 | | |
889 | 889 | | |
890 | 890 | | |
| |||
943 | 943 | | |
944 | 944 | | |
945 | 945 | | |
946 | | - | |
947 | | - | |
948 | | - | |
949 | | - | |
| 946 | + | |
950 | 947 | | |
951 | 948 | | |
952 | | - | |
| 949 | + | |
953 | 950 | | |
954 | 951 | | |
955 | 952 | | |
956 | | - | |
| 953 | + | |
957 | 954 | | |
958 | 955 | | |
959 | 956 | | |
960 | 957 | | |
961 | | - | |
| 958 | + | |
962 | 959 | | |
963 | 960 | | |
964 | 961 | | |
965 | | - | |
| 962 | + | |
966 | 963 | | |
967 | 964 | | |
968 | 965 | | |
| 966 | + | |
969 | 967 | | |
970 | 968 | | |
971 | 969 | | |
972 | 970 | | |
973 | 971 | | |
974 | 972 | | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
975 | 995 | | |
976 | 996 | | |
977 | 997 | | |
978 | | - | |
| 998 | + | |
979 | 999 | | |
980 | 1000 | | |
981 | 1001 | | |
| |||
986 | 1006 | | |
987 | 1007 | | |
988 | 1008 | | |
989 | | - | |
| 1009 | + | |
990 | 1010 | | |
991 | 1011 | | |
992 | 1012 | | |
| |||
1015 | 1035 | | |
1016 | 1036 | | |
1017 | 1037 | | |
1018 | | - | |
| 1038 | + | |
1019 | 1039 | | |
1020 | 1040 | | |
1021 | 1041 | | |
| |||
1024 | 1044 | | |
1025 | 1045 | | |
1026 | 1046 | | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
1027 | 1061 | | |
1028 | | - | |
1029 | | - | |
1030 | | - | |
| 1062 | + | |
1031 | 1063 | | |
1032 | 1064 | | |
1033 | | - | |
| 1065 | + | |
| 1066 | + | |
1034 | 1067 | | |
1035 | 1068 | | |
1036 | | - | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
1037 | 1072 | | |
1038 | 1073 | | |
1039 | 1074 | | |
| |||
1057 | 1092 | | |
1058 | 1093 | | |
1059 | 1094 | | |
1060 | | - | |
| 1095 | + | |
1061 | 1096 | | |
1062 | 1097 | | |
1063 | 1098 | | |
| |||
1068 | 1103 | | |
1069 | 1104 | | |
1070 | 1105 | | |
1071 | | - | |
| 1106 | + | |
1072 | 1107 | | |
1073 | 1108 | | |
1074 | 1109 | | |
| |||
1107 | 1142 | | |
1108 | 1143 | | |
1109 | 1144 | | |
1110 | | - | |
| 1145 | + | |
1111 | 1146 | | |
1112 | 1147 | | |
1113 | 1148 | | |
| |||
1126 | 1161 | | |
1127 | 1162 | | |
1128 | 1163 | | |
1129 | | - | |
| 1164 | + | |
1130 | 1165 | | |
1131 | 1166 | | |
1132 | 1167 | | |
| |||
1135 | 1170 | | |
1136 | 1171 | | |
1137 | 1172 | | |
1138 | | - | |
1139 | | - | |
1140 | | - | |
1141 | | - | |
1142 | | - | |
| 1173 | + | |
1143 | 1174 | | |
1144 | 1175 | | |
1145 | 1176 | | |
| |||
0 commit comments