@@ -28,7 +28,7 @@ print_log() {
2828 log -p i -t " $TAG " " $1 "
2929}
3030
31- get_sui_pids () {
31+ get_sui_root_pids () {
3232 if command -v pidof > /dev/null 2>&1 ; then
3333 pidof sui 2> /dev/null
3434 return
@@ -37,8 +37,60 @@ get_sui_pids() {
3737 ps -A 2> /dev/null | awk ' $NF == "sui" { print $2 }'
3838}
3939
40- is_sui_running () {
41- [ -n " $( get_sui_pids) " ]
40+ get_sui_shell_pids () {
41+ if command -v pidof > /dev/null 2>&1 ; then
42+ pidof sui_shell 2> /dev/null
43+ return
44+ fi
45+
46+ ps -A 2> /dev/null | awk ' $NF == "sui_shell" { print $2 }'
47+ }
48+
49+ count_pids () {
50+ pids=" $1 "
51+ if [ -z " $pids " ]; then
52+ echo 0
53+ return
54+ fi
55+ set -- $pids
56+ echo $#
57+ }
58+
59+ collect_sui_state () {
60+ sui_root_pids=" $( get_sui_root_pids) "
61+ sui_shell_pids=" $( get_sui_shell_pids) "
62+ sui_root_count=" $( count_pids " $sui_root_pids " ) "
63+ sui_shell_count=" $( count_pids " $sui_shell_pids " ) "
64+ }
65+
66+ is_sui_pair_healthy () {
67+ collect_sui_state
68+ [ " $sui_root_count " -eq 1 ] && [ " $sui_shell_count " -eq 1 ]
69+ }
70+
71+ kill_pid_list () {
72+ pids=" $1 "
73+ signal=" $2 "
74+
75+ if [ -z " $pids " ]; then
76+ return 0
77+ fi
78+
79+ # shellcheck disable=SC2086
80+ kill " $signal " $pids 2> /dev/null
81+ }
82+
83+ stop_sui_pair () {
84+ collect_sui_state
85+
86+ kill_pid_list " $sui_shell_pids " -TERM
87+ kill_pid_list " $sui_root_pids " -TERM
88+ sleep 1
89+
90+ collect_sui_state
91+ kill_pid_list " $sui_shell_pids " -KILL
92+ kill_pid_list " $sui_root_pids " -KILL
93+ sleep 1
4294}
4395
4496read_metadata () {
@@ -210,7 +262,7 @@ backoff_max=60
210262interval=5
211263
212264while true ; do
213- if is_sui_running ; then
265+ if is_sui_pair_healthy ; then
214266 if [ " $metadata_ready " -eq 0 ] && refresh_metadata; then
215267 metadata_ready=1
216268 fi
@@ -219,7 +271,13 @@ while true; do
219271 continue
220272 fi
221273
222- print_log " Sui daemon is not running, restarting..."
274+ case " $sui_root_count :$sui_shell_count " in
275+ 0:0) print_log " Sui root and shell servers are not running, restarting..." ;;
276+ 0:* ) print_log " Sui root server is not running, restarting pair..." ;;
277+ * :0) print_log " Sui shell server is not running, restarting pair..." ;;
278+ * ) print_log " Sui process pair is inconsistent (root=$sui_root_pids shell=$sui_shell_pids ), restarting..." ;;
279+ esac
280+ stop_sui_pair
223281 if [ " $metadata_ready " -eq 1 ] || refresh_metadata; then
224282 metadata_ready=1
225283 start_sui
@@ -228,9 +286,8 @@ while true; do
228286 fi
229287 sleep 2
230288
231- if is_sui_running; then
232- pids=" $( get_sui_pids) "
233- print_log " Sui daemon is running (pid: $pids )"
289+ if is_sui_pair_healthy; then
290+ print_log " Sui process pair is running (root=$sui_root_pids shell=$sui_shell_pids )"
234291 backoff=1
235292 sleep " $interval "
236293 continue
0 commit comments