Skip to content

Commit 8d623c1

Browse files
committed
util.bgproc: increase frequency of bgproc termination check
1 parent 610fab3 commit 8d623c1

File tree

4 files changed

+88
-20
lines changed

4 files changed

+88
-20
lines changed

docs/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
- complete: add `bleopt complete_source_sabbrev_{opts,ignore}` (motivated by mozirilla213) `#D2013` f95eb0cc `#D2016` 45c76746
142142
- util.bgproc: separate `ble/util/bgproc` from `histdb` (motivated by bkerin) `#D2017` 7803305f
143143
- util.bgproc: fix use of `ble/util/idle` in bash-3 `#D2026` xxxxxxxx
144+
- util.bgproc: increase frequency of bgproc termination check (motivated by bkerin) `#D2027` xxxxxxxx
144145
- menu-complete: support selection by index (requested by bkerin) `#D2023` b91b8bc8
145146

146147
## Changes

lib/util.bgproc.sh

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -274,23 +274,11 @@ function ble/util/bgproc#start {
274274
return "$_ble_local_ext"
275275
}
276276

277-
function ble/util/bgproc#stop/.wait {
278-
local pid=$1 msec=$2
279-
while
280-
kill -0 "$pid" 2>/dev/null || return 0
281-
((msec>0))
282-
do
283-
ble/util/msleep "$((msec>1000?1000:msec))"
284-
((msec-=1000))
285-
done
286-
return 1
287-
}
288277
function ble/util/bgproc#stop/.kill {
289278
local pid=$1 i close_timeout=$2
290-
ble/util/bgproc#stop/.wait "$pid" "$close_timeout" && return 0
291-
kill -- "$pid"
292-
ble/util/bgproc#stop/.wait "$pid" 10000 && return 0
293-
kill -9 "$pid"
279+
ble/util/conditional-sync '' '((1))' 1000 progressive-weight:pid="$pid":no-wait-pid:timeout="$close_timeout"
280+
kill -0 "$pid" || return 0
281+
ble/util/conditional-sync '' '((1))' 1000 progressive-weight:pid="$pid":no-wait-pid:SIGKILL:timeout=10000
294282
}
295283

296284
## @fn ble/util/bgproc#stop prefix

note.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6726,6 +6726,65 @@ bash_tips
67266726

67276727
2023-04-02
67286728

6729+
* bgproc: シェルの終了時に即座に強制終了するオプションをつける? (motivated by bkerin) [#D2028]
6730+
https://github.com/akinomyoga/ble.sh/discussions/309#discussioncomment-5498921
6731+
6732+
然し実際にそれが良い事なのか分からない。というか現状の実装ではそうはなって
6733+
いないのだったか? → child session はちゃんとすぐに終了する。然し端末が閉じ
6734+
ない。これは stderr, stdout, stdin の何れかが未だ開いたままになっているのが
6735+
原因と思われる。
6736+
6737+
.kill ではちゃんと全部閉じている筈と思ったがよく考えたらスタートしたプロセ
6738+
ス自体が stderr を保持しているのが原因の気がする。
6739+
6740+
そのまま強制終了するというのもどうかという気がするが、それならばそれで良い。
6741+
6742+
* done: 或いは終了チェックの interval をより短くする。これは
6743+
ble/util/bgproc#stop/.wait で使っている 1000 という値を設定できる様にすれ
6744+
ば良い。然し、これを変更したとして実際に終了にかかる時間がそんなに速くな
6745+
るのだろうか。
6746+
6747+
うーん。或いは conditional-sync の progressive-weight を使う? と思ったが
6748+
conditional-sync は新しいサブシェルを待つ物なので単に或る条件を待つという
6749+
事には使えない。conditional-sync を拡張して任意の条件を待つ事ができるよう
6750+
にする可能性も考えたが、
6751+
6752+
a うーん。或いはコマンドを開始するのではなくて既存のプロセスを待つという
6753+
機能を持たせる? この時に conditional-sync に足りていないのは kill を二
6754+
段階で行う機能である。SIGKILL を送る様にするオプションは実装しているが、
6755+
kill で駄目だった時に段階的に SIGKILL を送るという機能は実装していない。
6756+
そういう機能も追加して良いかもしれない。
6757+
6758+
うーん。取り敢えず conditional-sync を2回呼び出して二回目で SIGKILL を
6759+
指定すれば良いのだと気づいたので conditional-sync 自体に段階的に kill
6760+
を実行する機能は実装しなくて良かった。
6761+
6762+
b 或いはコマンドが空の時には指定された条件だけを待つという振る舞いにする。
6763+
この場合にはコマンドの kill までは実行しない。
6764+
6765+
然しもし原因が .kill の interval による物なのだとしても、
6766+
stdout/stderr/stdin を封じている限りは生きているというのは .kill が原因な
6767+
のではないのでは? 然し、プロセスが瞬間的に終了する事ができなかった場合に
6768+
は .kill による自動 kill が起動する迄は bgproc が保持している stderr が開
6769+
いたままである。然し、その場合には timeout になるまで相当待たなければなら
6770+
ない (既定では 10sec である。或いは SIGKILL まで更に 10sec ある)。
6771+
6772+
* ok: と思ったがもしかすると .kill 自体が fd を保持しているから bg
6773+
process が終了しない可能性? と思ったがそれも変だ。.kill 自体は fd を閉
6774+
じてから呼び出している筈 → 実際に確認した。
6775+
6776+
? ok: ところで set -m して起動した bgproc のなかで更にパイプやサブシェルな
6777+
どを立ち上げた時に更に別の PGID になっているとその別の PGID までは kill
6778+
しきれないのでは。なので set +m を中で実行しなければならないのでは。
6779+
6780+
→と思ったが、実験してみた限りでは set -m が実際に効果を持つのはそれを呼
6781+
び出したサブシェルだけの様である。なので更に入れ子で呼び出された物に関し
6782+
ては敢えて set -m を実行しない限りはちゃんと元の pgid に属する様になって
6783+
いる。
6784+
6785+
即座に終了する方法について考えたがこれは単に kill-timeout を 0 に設定すれば
6786+
良いのでは。結局向こうで起こっている事が何か分からないのでどうしようもない。
6787+
67296788
* make: インストール時に元のコメントなどの情報への pointer をつける (suggested by bkerin) [#D2027]
67306789
https://github.com/akinomyoga/ble.sh/discussions/309#discussioncomment-5498921
67316790

src/util.sh

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3638,7 +3638,10 @@ function ble/util/conditional-sync/.kill {
36383638
## COMMAND ends.
36393639
##
36403640
## @param[in] command
3641-
## The command that is evaluated in a subshell
3641+
## The command that is evaluated in a subshell. If an empty string is
3642+
## specified, only the CONDITION is checked for the synchronization and any
3643+
## background subshell is not started.
3644+
##
36423645
## @param[in,opt] condition
36433646
## The command to test the condition to continue to run the command. The
36443647
## default condition is "! ble/decode/has-input". The following local
@@ -3667,6 +3670,15 @@ function ble/util/conditional-sync/.kill {
36673670
## The processes are killed by SIGKILL. When this is unspecified, the
36683671
## processes are killed by SIGTERM.
36693672
##
3673+
## @opt pid=PID
3674+
## When COMMAND is empty, the function waits for the exit of the process
3675+
## specified by PID. If a negative integer is specified, it is treated
3676+
## as PGID. When the condition is unsatisfied or the timeout has been
3677+
## reached, the specified process will be killed.
3678+
##
3679+
## @opt no-wait-pid
3680+
## Do not wait for the exit status of the background process
3681+
##
36703682
function ble/util/conditional-sync {
36713683
local __ble_command=$1
36723684
local __ble_continue=${2:-'! ble/decode/has-input'}
@@ -3680,10 +3692,18 @@ function ble/util/conditional-sync {
36803692
local __ble_weight_max=$__ble_weight __ble_weight=1
36813693

36823694
local sync_elapsed=0
3683-
[[ $__ble_timeout ]] && ((__ble_timeout<=0)) && return 142
3695+
if [[ $__ble_timeout ]] && ((__ble_timeout<=0)); then return 142; fi
36843696
builtin eval -- "$__ble_continue" || return 148
36853697
(
3686-
builtin eval -- "$__ble_command" & local __ble_pid=$!
3698+
local __ble_pid=
3699+
if [[ $__ble_command ]]; then
3700+
builtin eval -- "$__ble_command" & __ble_pid=$!
3701+
else
3702+
local ret
3703+
ble/opts#extract-last-optarg "$__ble_opts" pid
3704+
__ble_pid=$ret
3705+
ble/util/unlocal ret
3706+
fi
36873707
while
36883708
# check timeout
36893709
if [[ $__ble_timeout ]]; then
@@ -3699,14 +3719,14 @@ function ble/util/conditional-sync {
36993719
((sync_elapsed+=__ble_weight))
37003720
[[ :$__ble_opts: == *:progressive-weight:* ]] &&
37013721
((__ble_weight<<=1,__ble_weight>__ble_weight_max&&(__ble_weight=__ble_weight_max)))
3702-
builtin kill -0 "$__ble_pid" &>/dev/null
3722+
[[ ! $__ble_pid ]] || builtin kill -0 "$__ble_pid" &>/dev/null
37033723
do
37043724
if ! builtin eval -- "$__ble_continue"; then
37053725
ble/util/conditional-sync/.kill
37063726
return 148
37073727
fi
37083728
done
3709-
wait "$__ble_pid"
3729+
[[ ! $__ble_pid || :$__ble_opts: == *:no-wait-pid:* ]] || wait "$__ble_pid"
37103730
)
37113731
}
37123732

0 commit comments

Comments
 (0)