Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: Cannot add child handler, the child watcher does not have a loop attached #353

Closed
conor-f opened this issue Jul 17, 2019 · 7 comments
Labels
old-auto-close Reopen if still relevant

Comments

@conor-f
Copy link

conor-f commented Jul 17, 2019

I get this error any time I try run a LiveCapture within a thread. Here is a minimal reproducible example:

import pyshark
import threading
import time

def start_capture(interface='mon0'):
    capture = pyshark.LiveCapture(interface)

    while True:
        for packet in capture.sniff_continuously(packet_count=5):
            print(packet)

        time.sleep(1)

t = threading.Thread(target=start_capture)
t.start()

while True:
    print('Still running...')
    time.sleep(10)

This produces the following stack trace:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "ex.py", line 9, in start_capture
    for packet in capture.sniff_continuously(packet_count=5):
  File "/usr/local/lib/python3.5/dist-packages/pyshark/capture/capture.py", line 227, in _packets_from_tshark_sync
    tshark_process = existing_process or self.eventloop.run_until_complete(self._get_tshark_process())
  File "/usr/lib/python3.5/asyncio/base_events.py", line 466, in run_until_complete
    return future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "/usr/local/lib/python3.5/dist-packages/pyshark/capture/live_capture.py", line 97, in _get_tshark_process
    stderr=self._stderr_output())
  File "/usr/lib/python3.5/asyncio/subprocess.py", line 212, in create_subprocess_exec
    stderr=stderr, **kwds)
  File "/usr/lib/python3.5/asyncio/base_events.py", line 1189, in subprocess_exec
    bufsize, **kwargs)
  File "/usr/lib/python3.5/asyncio/unix_events.py", line 194, in _make_subprocess_transport
    self._child_watcher_callback, transp)
  File "/usr/lib/python3.5/asyncio/unix_events.py", line 856, in add_child_handler
    "Cannot add child handler, "
RuntimeError: Cannot add child handler, the child watcher does not have a loop attached

Still Running...
Still Running...
Traceback (most recent call last):
  File "ex.py", line 19, in <module>
    time.sleep(10)
KeyboardInterrupt

I'm using python3.5 on a Raspberry Pi 3B+ with Raspbian 9 (Stretch). Using pyshark version 0.4.2.4

@kcexn
Copy link
Contributor

kcexn commented Jul 18, 2019

I've taken a look at this. This problem is a bit out of scope of #351 I think. As #351 has to do with how asyncio interacts with the python 3.5 interpreter, whereas this at first glance looks like it has more to do with the way asyncio interacts with threading.

Does this work in later versions of the interpreter? Have you tried it for example on python 3.7?

@conor-f
Copy link
Author

conor-f commented Jul 18, 2019

Thanks for taking the time to! I'm unfamiliar with the asyncio module so couldn't really look at it myself.

I haven't run this exact setup on python 3.7 but I only noticed it after I switched to 3.5.

@kcexn
Copy link
Contributor

kcexn commented Jul 18, 2019

What did you switch to 3.5 from? 2.7? Did it work in your previous setup?

@cmacfarl
Copy link

I have run into this exact same problem. Was there ever a workaround identified?

@cmacfarl
Copy link

A little more research reveals that a thread needs to have it's own event loop. In my case I wanted to run websockets on one end and a LiveCapture on the other. By letting pyshark's LiveCapture have the main context's event loop and doing the following within a thread on the websocket side it all works correctly.

    asyncio.set_event_loop(asyncio.new_event_loop())
    asyncio.get_event_loop().run_until_complete(listen())
    asyncio.get_event_loop().run_forever()

@SaiMun92
Copy link

SaiMun92 commented Mar 4, 2020

Hey there! I have the same exact problem. I am running my pyshark.LiveCapture("pcap file") function under a QThread class for Pyside2. QThread creates an event loop when the run() function is called. It works perfectly for Windows 10 OS but not in Ubuntu 16.04. It throws the above mentioned error
RuntimeError: Cannot add child handler, the child watcher does not have a loop attached.

Even after implementing @cmacfarl additional snippet of code, it still throws the same error. How do i solve this?

@peacecun
Copy link

Thanks for taking the time to! I'm unfamiliar with the asyncio module so couldn't really look at it myself.

I haven't run this exact setup on python 3.7 but I only noticed it after I switched to 3.5.

Do you have solution now? I'm having the same problem :((

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
old-auto-close Reopen if still relevant
Projects
None yet
Development

No branches or pull requests

6 participants