Skip to content

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

Closed
@conor-f

Description

@conor-f

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions