diff --git a/ble.pp b/ble.pp index 118d9ad5..d5599046 100644 --- a/ble.pp +++ b/ble.pp @@ -335,8 +335,9 @@ function ble/variable#copy-state { # From src/util.sh (ble/fd#is-open and ble/fd#alloc/.nextfd) function ble/base/xtrace/.fdcheck { builtin : >&"$1"; } 2>/dev/null function ble/base/xtrace/.fdnext { - (($1=${_ble_util_openat_nextfd:-30},1)) - while ble/base/xtrace/.fdcheck "${!1}"; do (($1++,1)); done + local __init=${_ble_util_openat_nextfd:=${bleopt_openat_base:-30}} + for (($1=__init;$1<__init+1024;$1++)); do ble/base/xtrace/.fdcheck "${!1}" || break; done + (($1<__init+1024)) || { (($1=__init,_ble_util_openat_nextfd++)); builtin eval "exec ${!1}>&-"; } || ((1)) } function ble/base/xtrace/adjust { local level=${#_ble_bash_xtrace[@]} diff --git a/docs/ChangeLog.md b/docs/ChangeLog.md index eb45dc8a..c42cc4bf 100644 --- a/docs/ChangeLog.md +++ b/docs/ChangeLog.md @@ -378,6 +378,7 @@ - benchmark: improve determination of the base time `#D1737` ad866c1 - main: support `bleopt debug_xtrace` (requested by SuperSandro2000) `#D1810` XXXXXXX - test: clean up check failures by `make check` and `make scan` `#D1812` XXXXXXX +- util (`fd#alloc`): limit the search range of free fds `#D1813` XXXXXXX ## Contrib diff --git a/note.txt b/note.txt index 113d60c9..bb4d75b0 100644 --- a/note.txt +++ b/note.txt @@ -6394,6 +6394,11 @@ bash_tips 2022-06-13 + * util (fd#alloc): fd の上限に達した時にどうするか。無限ループになるのではないか [#D1813] + + 取り敢えず 1024 の探索上限を入れた。探索範囲に見つからなかった時には、元々 + の _ble_util_openat_nextfd の上に開く事にする。 + * test: CI にテストを載せる上で既知の false error は全て潰しておく必要がある [#D1812] 今まで CI テストを実行すると clone stats に影響が出ると思って敢えて対応はし diff --git a/src/util.sh b/src/util.sh index 66d6d1ec..21cb07e8 100644 --- a/src/util.sh +++ b/src/util.sh @@ -3285,16 +3285,25 @@ function ble/fd#is-open { builtin : >&"$1"; } 2>/dev/null _ble_util_openat_nextfd= function ble/fd#alloc/.nextfd { [[ $_ble_util_openat_nextfd ]] || - _ble_util_openat_nextfd=$bleopt_openat_base + _ble_util_openat_nextfd=${bleopt_openat_base:-30} # Note: Bash 3.1 では exec fd>&- で明示的に閉じても駄目。 # 開いた後に読み取りプロセスで読み取りに失敗する。 # なので開いていない fd を探す必要がある。#D0992 # Note: 指定された fd が開いているかどうかを # 可搬に高速に判定する方法を見つけたので # 常に開いていない fd を探索する。#D1318 - while ble/fd#is-open "$_ble_util_openat_nextfd"; do + # Note: fd が枯渇すると探索が無限ループになるので fd 探索範囲の上限を 1024 に + # 制限する。もし見つからない場合には初期値の fd を上書きする。 + local _ble_local_init=$_ble_util_openat_nextfd + local _ble_local_limit=$((_ble_local_init+1024)) + while ((_ble_util_openat_nextfd<_ble_local_limit)) && + ble/fd#is-open "$_ble_util_openat_nextfd"; do ((_ble_util_openat_nextfd++)) done + if ((_ble_util_openat_nextfd>=_ble_local_lim)); then + _ble_util_openat_nextfd=$_ble_local_init + builtin eval "exec $_ble_util_openat_nextfd>&-" + fi (($1=_ble_util_openat_nextfd++)) }