pyshark icon indicating copy to clipboard operation
pyshark copied to clipboard

Linux OSError: Too many open files when running capture in a loop

Open felipesetti opened this issue 2 years ago • 2 comments

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)

felipesetti avatar Dec 20 '22 09:12 felipesetti

Hi @felipesetti, I'm also experiencing this issue. do you have any workaround?

bivas6 avatar Feb 12 '23 10:02 bivas6

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.

felipesetti avatar Feb 21 '23 13:02 felipesetti