大网数据广告活动
服务器运维问答

Another app is currently holding the xtables lock

· 52CE 我爱测

I've been using a bash shell script to set complex iptables rules for years. However, on Debian Stretch when I tried to use a script, it became sluggish and left iptables in a bad state. When I tried to do an iptables -L -v it ca…

问题原文

原帖链接 · BurningKrome · CC BY-SA 4.0

I've been using a bash shell script to set complex iptables rules for years. However, on Debian Stretch when I tried to use a script, it became sluggish and left iptables in a bad state.

When I tried to do an iptables -L -v it came back with an error...

Another app is currently holding the xtables lock; still -9s 0us time ahead to have a chance to grab the lock...

Google'ing led me to this bug which suggests using the "-w" switch. The man page doesn't really clear up how this switch might affect the issue.

My script uses a loop for admin convenience, which causes it to make a lot of calls to iptables.

# This actually sets the allowed incoming iptables lines
setincoming() {
    for port in ${2}; do
        for ip in ${1}; do
            if  [ `echo "$ip" | grep -P "(^|\s)[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(/[0-9]*)*(\s*)$"` ]; then
                iptables -I INPUT  -p tcp  -s $ip --dport $port -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
            else
                ip6tables -I INPUT  -p tcp  -s $ip --dport $port -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
            fi
        done
    done
}

#####
# APIs
setincoming "123.123.123.10   123.123.123.11   fe80::xxxx:xxx:xxxx:xxxx" "4200 4300"

Can anyone help me understand how "-w" is used to fix this issue?

EDIT: For clarification I did, of course, look at the man page - and tried using the switches -w and combined as -w -W1 but this had no effect on the issue (neither fixed it nor changed the symptoms).

SO, I'm still at a loss as to how to resolve.

已采纳解答

原帖链接 · billyw · CC BY-SA 4.0

The -w option simply causes iptables to wait for the xtables lock rather than exit upon initial failure to obtain it. The option can work around race conditions where multiple processes are competing to obtain and release the xtables lock. If another process is not releasing the lock, or if too many processes are competing for the lock, this option may not help.

As Tomek recommended, I would modify your script to take advantage of ipset. It will certainly be more optimized since it relies on a hash table instead of going through your iptables rules serially. It will likely resolve your locking problem as well. Something along the lines of:

# Create ipset and connect it with iptables
ipset create foo hash:ip,port
iptables -I INPUT -m set --match-set foo src -j ACCEPT

# Add allowances as needed
ipset add foo 123.123.123.10,4200
ipset add foo 123.123.123.11,tcp:4300 # Specify protocol
ipset add foo 123.123.123.12,4400 timeout 600 # Set a timeout for this to disappear
ipset add foo 123.123.123.13,4500 -exist # Do not error if this already exists

Since you included both IPv4 and IPv6 in your examples, I'll also mention that you can use the family parameter when creating the ipset to support IPv6 in particular.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-10-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。