Python-多线程:分段错误
Python-mutithreading:Segmentation fault
有时候我运行这个程序的时候会提示Segmentation fault
start sniffing
Fatal Python error: Segmentation fault
Thread 0x00007f5e3bfff700 (most recent call first):
File "/usr/local/lib/python3.8/dist-packages/iptc/ip4tc.py", line 1610 in commit
File "/usr/local/lib/python3.8/bin/python-sudo.sh: line 2: 3087 Segmentation fault sudo /usr/bin/python3 "$@"
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
我的代码:
table = iptc.Table(iptc.Table.FILTER)
chain = iptc.Chain(table, "INPUT")
def Callback(packet):
if packet[IP].proto == 17:
if packet[UDP].dport == 62201:
sp = packet[IP].src
thread_add = threading.Thread(target=add_rules, args=(sp, ))
thread_add.start()
def add_rules(sp):
global chain, table
rule = iptc.Rule()
rule.src = sp
target = iptc.Target(rule, "ACCEPT")
rule.target = target
chain.insert_rule(rule)
print('rule ' + sp + ' insert! {}'.format(time.time()))
time.sleep(30)
chain.delete_rule(rule)
print('time out delete ' + sp + ' this rule! {}'.format(time.time()))
while 1:
# 使用sniff抓包
print('start sniffing')
msg = sniff(filter='dst port 62201', prn=Callback)
time.sleep(0.03)
server.close()
print('Sever closed')
可能的原因是您正在使用的库或您使用它的方式。存在一个明显的危险:您使用多个线程在同一条链上工作,table 但您没有使用互斥锁来保护对它们的访问。
尝试这样的事情:
table = iptc.Table(iptc.Table.FILTER)
chain = iptc.Chain(table, "INPUT")
mutex = threading.Lock()
def add_rules(sp):
with mutex:
rule = iptc.Rule()
rule.src = sp
target = iptc.Target(rule, "ACCEPT")
rule.target = target
chain.insert_rule(rule)
print('rule ' + sp + ' insert! {}'.format(time.time()))
time.sleep(30)
with mutex:
chain.delete_rule(rule)
print('time out delete ' + sp + ' this rule! {}'.format(time.time()))
有时候我运行这个程序的时候会提示Segmentation fault
start sniffing
Fatal Python error: Segmentation fault
Thread 0x00007f5e3bfff700 (most recent call first):
File "/usr/local/lib/python3.8/dist-packages/iptc/ip4tc.py", line 1610 in commit
File "/usr/local/lib/python3.8/bin/python-sudo.sh: line 2: 3087 Segmentation fault sudo /usr/bin/python3 "$@"
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
我的代码:
table = iptc.Table(iptc.Table.FILTER)
chain = iptc.Chain(table, "INPUT")
def Callback(packet):
if packet[IP].proto == 17:
if packet[UDP].dport == 62201:
sp = packet[IP].src
thread_add = threading.Thread(target=add_rules, args=(sp, ))
thread_add.start()
def add_rules(sp):
global chain, table
rule = iptc.Rule()
rule.src = sp
target = iptc.Target(rule, "ACCEPT")
rule.target = target
chain.insert_rule(rule)
print('rule ' + sp + ' insert! {}'.format(time.time()))
time.sleep(30)
chain.delete_rule(rule)
print('time out delete ' + sp + ' this rule! {}'.format(time.time()))
while 1:
# 使用sniff抓包
print('start sniffing')
msg = sniff(filter='dst port 62201', prn=Callback)
time.sleep(0.03)
server.close()
print('Sever closed')
可能的原因是您正在使用的库或您使用它的方式。存在一个明显的危险:您使用多个线程在同一条链上工作,table 但您没有使用互斥锁来保护对它们的访问。
尝试这样的事情:
table = iptc.Table(iptc.Table.FILTER)
chain = iptc.Chain(table, "INPUT")
mutex = threading.Lock()
def add_rules(sp):
with mutex:
rule = iptc.Rule()
rule.src = sp
target = iptc.Target(rule, "ACCEPT")
rule.target = target
chain.insert_rule(rule)
print('rule ' + sp + ' insert! {}'.format(time.time()))
time.sleep(30)
with mutex:
chain.delete_rule(rule)
print('time out delete ' + sp + ' this rule! {}'.format(time.time()))