Skip to content

Commit

Permalink
util (fd#alloc): limit the search range of free fds
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jun 13, 2022
1 parent bb3e0a3 commit 43be0e4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
5 changes: 3 additions & 2 deletions ble.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}
Expand Down
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions note.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 に影響が出ると思って敢えて対応はし
Expand Down
13 changes: 11 additions & 2 deletions src/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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++))
}

Expand Down

0 comments on commit 43be0e4

Please sign in to comment.