Skip to content

Commit

Permalink
Merge pull request #566 from KimiNewt/fix-windows-eventloop-issues
Browse files Browse the repository at this point in the history
Fix Windows eventloop-related issues
  • Loading branch information
KimiNewt authored Jul 8, 2022
2 parents 0c18493 + a0dd59e commit 94c37ae
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/pyshark/capture/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import concurrent.futures
import sys
import logging
import warnings

from pyshark import ek_field_mapping
from pyshark.packet.packet import Packet
Expand Down Expand Up @@ -166,7 +167,17 @@ def _verify_capture_parameters(self):
def _setup_eventloop(self):
"""Sets up a new eventloop as the current one according to the OS."""
if os.name == "nt":
self.eventloop = asyncio.ProactorEventLoop()
current_eventloop = asyncio.get_event_loop_policy().get_event_loop()
if isinstance(current_eventloop, asyncio.ProactorEventLoop):
self.eventloop = current_eventloop
else:
# On Python before 3.8, Proactor is not the default eventloop type, so we have to create a new one.
# If there was an existing eventloop this can create issues, since we effectively disable it here.
if asyncio.Task.all_tasks():
warnings.warn("The running eventloop has tasks but pyshark must set a new eventloop to continue. "
"Existing tasks may not run.")
self.eventloop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(self.eventloop)
else:
try:
self.eventloop = asyncio.get_event_loop_policy().get_event_loop()
Expand Down Expand Up @@ -289,7 +300,7 @@ async def _go_through_packets_from_fd(self, fd, packet_callback, packet_count=No
break

def _create_stderr_handling_task(self, stderr):
self._stderr_handling_tasks.append(asyncio.create_task(self._handle_process_stderr_forever(stderr)))
self._stderr_handling_tasks.append(asyncio.ensure_future(self._handle_process_stderr_forever(stderr)))

async def _handle_process_stderr_forever(self, stderr):
while True:
Expand Down Expand Up @@ -380,7 +391,7 @@ def _setup_tshark_output_parser(self):
return tshark_xml.TsharkXmlParser(parse_summaries=self._only_summaries)

def close(self):
self.eventloop.create_task(self.close_async())
self.eventloop.run_until_complete(self.close_async())

async def close_async(self):
for process in self._running_processes.copy():
Expand Down

0 comments on commit 94c37ae

Please sign in to comment.