pyshark
pyshark copied to clipboard
Linux OSError: Too many open files when running capture in a loop
Describe the bug Getting exception "OSError: [Errno 24] Too many open files" when running sniff in a loop.
To Reproduce Run the code: `import pyshark import time
def find_my_key(timeout = 3600):
found = False
end_time = time.time() + timeout
while time.time() < end_time:
cap = pyshark.LiveCapture(interface='eth1', use_json=True, include_raw=True)
cap.sniff(packet_count=200, timeout=3)
for pkt in cap:
if 'mykey' in pkt:
found = True
cap.clear()
cap.close()
if found:
return True
else:
return False
if name == 'main': find_my_key() `
Expected behavior It is possible to sniff packets in a loop if closing/cleaning the capture properly at each iteration.
Versions (please complete the following information):
- OS: Ubuntu 20.04
- pyshark version: 0.5.3 (python 3.8.10)
- tshark version: TShark (Wireshark) 3.2.3 (Git v3.2.3 packaged as 3.2.3-1)
Hi @felipesetti, I'm also experiencing this issue. do you have any workaround?
Hi @bivas6 ,
As I suspect it is about the number of file descriptors that remain open, I am triggering every capture iteration in a different Process (using the multiprocessing library). After each capture, I terminate the process, ensuring each capture has its own process workspace. I haven't seen the issue again since then.
I hope it can help you somehow.