pyshark icon indicating copy to clipboard operation
pyshark copied to clipboard

Exception: got Future <Future pending> attached to a different loop

Open ja-kh opened this issue 3 years ago • 4 comments

I try to dissect a byte object containing a captured frame with the InMemCapture and I get an exception on cleanup. I have tried to close the open tshark process by calling the close function but that raises a similar exception.

Both exceptions contain the message from asyncio module:

got Future <Future pending> attached to a different loop

I use the following versions on Windows 10 64bit

Python 3.9.4 pyshark 0.4.3

I made this minimal exampl:

import pyshark
rawpacket = b'\x00\x1b\x1b_\xddJ\x00\x1c\x06\x0c\xfbu\x81\x00\xc0\x00\x88\x92\x80\x00\x80\x80\x80\x80\xb5\x80\x03\xe8\x03\x89\x00\xdb\x03R\x80\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc5\x805\x00\n\xc7^\xa9'
c = pyshark.InMemCapture()
packets = c.parse_packets([rawpacket])
print(packets[0])

c.close() # Exception 

Without the close() an exception occures on exit

 Exception ignored in: <function Capture.__del__ at 0x000002166C6C2550>
 Traceback (most recent call last):
   File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\capture.py", line 445, in __del__
     self.close()
   File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\capture.py", line 436, in close
     self.eventloop.run_until_complete(self.close_async())
   File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\base_events.py", line 642, in run_until_complete
     return future.result()
   File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\inmem_capture.py", line 156, in close_async
     await super(InMemCapture, self).close_async()
   File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\capture.py", line 440, in close_async
     await self._cleanup_subprocess(process)
   File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\capture.py", line 421, in _cleanup_subprocess
     return await asyncio.wait_for(process.wait(), 1)
   File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\tasks.py", line 481, in wait_for
     return fut.result()
   File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\subprocess.py", line 135, in wait
     return await self._transport._wait()
   File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\base_subprocess.py", line 235, in _wait
     return await waiter
 RuntimeError: Task <Task pending name='Task-5' coro=<Process.wait() running at c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\subprocess.py:135> cb=[_release_waiter(<Future pendi...66C3FD8B0>()]>)() at c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\tasks.py:416]> got Future <Future pending> attached to a different loop

With close() an exception occures on the close() call itself

Traceback (most recent call last):
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\<user>\.vscode\extensions\ms-python.python-2021.2.633441544\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\<user>\.vscode\extensions\ms-python.python-2021.2.633441544\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
    run()
  File "c:\Users\<user>\.vscode\extensions\ms-python.python-2021.2.633441544\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\<user>\workspace\pynetanalyzer\pyshark_test.py", line 7, in <module>
    c.close()
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\capture.py", line 436, in close
    self.eventloop.run_until_complete(self.close_async())
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\inmem_capture.py", line 156, in close_async
    await super(InMemCapture, self).close_async()
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\capture.py", line 440, in close_async
    await self._cleanup_subprocess(process)
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\capture.py", line 421, in _cleanup_subprocess
    return await asyncio.wait_for(process.wait(), 1)
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\tasks.py", line 481, in wait_for
    return fut.result()
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\subprocess.py", line 135, in wait
    return await self._transport._wait()
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\base_subprocess.py", line 235, in _wait
    return await waiter
RuntimeError: Task <Task pending name='Task-5' coro=<Process.wait() running at c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\subprocess.py:135> cb=[_release_waiter(<Future pendi...2B4B401C0>()]>)() at c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\tasks.py:416]> got Future <Future pending> attached to a different loop
Error on reading from the event loop self pipe
loop: <ProactorEventLoop running=True closed=False debug=False>
Traceback (most recent call last):
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\proactor_events.py", line 777, in _loop_self_reading
    f = self._proactor.recv(self._ssock, 4096)
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\windows_events.py", line 445, in recv
    self._register_with_iocp(conn)
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\windows_events.py", line 718, in _register_with_iocp
    _overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0)
OSError: [WinError 87] Falscher Parameter
Exception ignored in: <function Capture.__del__ at 0x00000212B4B02550>
Traceback (most recent call last):
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\capture.py", line 445, in __del__
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\capture.py", line 436, in close
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\base_events.py", line 642, in run_until_complete
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\inmem_capture.py", line 156, in close_async
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\capture.py", line 440, in close_async
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\pyshark\capture\capture.py", line 421, in _cleanup_subprocess
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\tasks.py", line 481, in wait_for
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\subprocess.py", line 135, in wait
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\base_subprocess.py", line 235, in _wait
RuntimeError: Task <Task pending name='Task-7' coro=<Process.wait() running at c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\subprocess.py:135> cb=[_release_waiter(<Future pendi...2B177D550>()]>)() at c:\users\<user>\appdata\local\programs\python\python39\lib\asyncio\tasks.py:416]> got Future <Future pending> attached to a different loop

ja-kh avatar Apr 08 '21 13:04 ja-kh

Hi, I do the see the same issue when I try to run my below code. Any suggestions for this issue would be reallyhelpful.

import pyshark import asyncio

pcap_file = 'sip.pcap'

capture = pyshark.FileCapture(pcap_file) for packet in capture: try: if hasattr(packet, 'sip'): field_names = packet.sip._all_fields field_values = packet.sip._all_fields.values() sip_header = [field_value for field_value in field_values for field_name in field_names if field_name == 'sip.msg_hdr'] sip_body = [field_value for field_value in field_values for field_name in field_names if field_name == 'sip.msg_body'] print(f'SIP header: {sip_header}') print(f'SIP body {sip_body}') except OSError: pass except asyncio.TimeoutError: pass

loop: <ProactorEventLoop running=True closed=False debug=False> Traceback (most recent call last): File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 777, in _loop_self_reading f = self._proactor.recv(self._ssock, 4096) File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_events.py", line 445, in recv self._register_with_iocp(conn) File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_events.py", line 718, in _register_with_iocp _overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0) OSError: [WinError 87] The parameter is incorrect

BrightTA avatar Apr 30 '21 10:04 BrightTA

The pyshark InMemCapture class is going to be very useful in my project, but unfortunately I have the same error message as @ja-kh when closing the capture object.

Python 3.94, pyshark 0.4.3, Windows 10 64 bit.

The traceback from @BrightTA is clearly a different issue.

Traceback (most recent call last):
  File "C:\UserData\buzzy\dev\myproject\process_logs.py", line 366, in <module>
    app_globals.pycap.close()
  File "C:\UserData\buzzy\dev\myproject\venv\lib\site-packages\pyshark\capture\capture.py", line 436, in close
    self.eventloop.run_until_complete(self.close_async())
  File "C:\Users\buzzy\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "C:\UserData\buzzy\dev\myproject\venv\lib\site-packages\pyshark\capture\inmem_capture.py", line 156, in close_async
    await super(InMemCapture, self).close_async()
  File "C:\UserData\buzzy\dev\myproject\venv\lib\site-packages\pyshark\capture\capture.py", line 440, in close_async
    await self._cleanup_subprocess(process)
  File "C:\UserData\buzzy\dev\myproject\venv\lib\site-packages\pyshark\capture\capture.py", line 421, in _cleanup_subprocess
    return await asyncio.wait_for(process.wait(), 1)
  File "C:\Users\buzzy\AppData\Local\Programs\Python\Python39\lib\asyncio\tasks.py", line 476, in wait_for
    return fut.result()
  File "C:\Users\buzzy\AppData\Local\Programs\Python\Python39\lib\asyncio\subprocess.py", line 135, in wait
    return await self._transport._wait()
  File "C:\Users\buzzy\AppData\Local\Programs\Python\Python39\lib\asyncio\base_subprocess.py", line 235, in _wait
    return await waiter
RuntimeError: Task <Task pending name='Task-9' coro=<Process.wait() running at C:\Users\buzzy\AppData\Local\Programs\Python\Python39\lib\asyncio\subprocess.py:135> cb=[_release_waiter(<Future pendi...393700400>()]>)() at C:\Users\buzzy\AppData\Local\Programs\Python\Python39\lib\asyncio\tasks.py:414]> got Future <Future pending> attached to a different loop
Exception ignored in: <function Capture.__del__ at 0x000001F3912F7F70>
Traceback (most recent call last):
  File "C:\UserData\buzzy\dev\myproject\venv\lib\site-packages\pyshark\capture\capture.py", line 445, in __del__
  File "C:\UserData\buzzy\dev\myproject\venv\lib\site-packages\pyshark\capture\capture.py", line 436, in close
  File "C:\Users\buzzy\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
  File "C:\UserData\buzzy\dev\myproject\venv\lib\site-packages\pyshark\capture\inmem_capture.py", line 156, in close_async
  File "C:\UserData\buzzy\dev\myproject\venv\lib\site-packages\pyshark\capture\capture.py", line 440, in close_async
  File "C:\UserData\buzzy\dev\myproject\venv\lib\site-packages\pyshark\capture\capture.py", line 421, in _cleanup_subprocess
  File "C:\Users\buzzy\AppData\Local\Programs\Python\Python39\lib\asyncio\tasks.py", line 476, in wait_for
  File "C:\Users\buzzy\AppData\Local\Programs\Python\Python39\lib\asyncio\subprocess.py", line 135, in wait
  File "C:\Users\buzzy\AppData\Local\Programs\Python\Python39\lib\asyncio\base_subprocess.py", line 235, in _wait
RuntimeError: Task <Task pending name='Task-11' coro=<Process.wait() running at C:\Users\buzzy\AppData\Local\Programs\Python\Python39\lib\asyncio\subprocess.py:135> cb=[_release_waiter(<Future pendi...38EA78100>()]>)() at C:\Users\buzzy\AppData\Local\Programs\Python\Python39\lib\asyncio\tasks.py:414]> got Future <Future pending> attached to a different loop

buzzy-hopewell avatar May 19 '21 02:05 buzzy-hopewell

@ja-kh After inspecting pyshark's capture._setup_eventloop function, I was able to prevent the error messages we are seeing by modifying the code in your example to create a new asyncio.ProactorEventLoop, setting it as the current asyncio event loop, and initializing the InMemCapture object with it. I'm a beginner when it comes to async/await, so I don't feel qualified to submit a pull request, but I'm glad my program seems to work now without the messages!

import asyncio
import pyshark
rawpacket = b'\x00\x1b\x1b_\xddJ\x00\x1c\x06\x0c\xfbu\x81\x00\xc0\x00\x88\x92\x80\x00\x80\x80\x80\x80\xb5\x80\x03\xe8\x03\x89\x00\xdb\x03R\x80\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc5\x805\x00\n\xc7^\xa9'

loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
c = pyshark.InMemCapture(eventloop=loop)
packets = c.parse_packets([rawpacket])
print(packets[0])

c.close() # Exception no more

buzzy-hopewell avatar May 20 '21 21:05 buzzy-hopewell

@buzzy-hopewell what are \xfbu or \xc7^ in your byte string !? what does it mean, unless not hexadecimal ! i have strange sign in my byte too

mzalaki00 avatar Oct 31 '23 12:10 mzalaki00