|
5 | 5 | import concurrent.futures |
6 | 6 | import sys |
7 | 7 | import logging |
| 8 | +import warnings |
8 | 9 |
|
9 | 10 | from pyshark import ek_field_mapping |
10 | 11 | from pyshark.packet.packet import Packet |
@@ -166,7 +167,17 @@ def _verify_capture_parameters(self): |
166 | 167 | def _setup_eventloop(self): |
167 | 168 | """Sets up a new eventloop as the current one according to the OS.""" |
168 | 169 | if os.name == "nt": |
169 | | - self.eventloop = asyncio.ProactorEventLoop() |
| 170 | + current_eventloop = asyncio.get_event_loop_policy().get_event_loop() |
| 171 | + if isinstance(current_eventloop, asyncio.ProactorEventLoop): |
| 172 | + self.eventloop = current_eventloop |
| 173 | + else: |
| 174 | + # On Python before 3.8, Proactor is not the default eventloop type, so we have to create a new one. |
| 175 | + # If there was an existing eventloop this can create issues, since we effectively disable it here. |
| 176 | + if asyncio.Task.all_tasks(): |
| 177 | + warnings.warn("The running eventloop has tasks but pyshark must set a new eventloop to continue. " |
| 178 | + "Existing tasks may not run.") |
| 179 | + self.eventloop = asyncio.ProactorEventLoop() |
| 180 | + asyncio.set_event_loop(self.eventloop) |
170 | 181 | else: |
171 | 182 | try: |
172 | 183 | self.eventloop = asyncio.get_event_loop_policy().get_event_loop() |
@@ -289,7 +300,7 @@ async def _go_through_packets_from_fd(self, fd, packet_callback, packet_count=No |
289 | 300 | break |
290 | 301 |
|
291 | 302 | def _create_stderr_handling_task(self, stderr): |
292 | | - self._stderr_handling_tasks.append(asyncio.create_task(self._handle_process_stderr_forever(stderr))) |
| 303 | + self._stderr_handling_tasks.append(asyncio.ensure_future(self._handle_process_stderr_forever(stderr))) |
293 | 304 |
|
294 | 305 | async def _handle_process_stderr_forever(self, stderr): |
295 | 306 | while True: |
@@ -380,7 +391,7 @@ def _setup_tshark_output_parser(self): |
380 | 391 | return tshark_xml.TsharkXmlParser(parse_summaries=self._only_summaries) |
381 | 392 |
|
382 | 393 | def close(self): |
383 | | - self.eventloop.create_task(self.close_async()) |
| 394 | + self.eventloop.run_until_complete(self.close_async()) |
384 | 395 |
|
385 | 396 | async def close_async(self): |
386 | 397 | for process in self._running_processes.copy(): |
|
0 commit comments