bpo-32314: Fix asyncio.run() to cancel runinng tasks on shutdown (#5262) · python/cpython@a4afcdf (original) (raw)

`@@ -228,14 +228,9 @@ def init(self):

`

228

228

`self._coroutine_origin_tracking_enabled = False

`

229

229

`self._coroutine_origin_tracking_saved_depth = None

`

230

230

``

231

``

`-

if hasattr(sys, 'get_asyncgen_hooks'):

`

232

``

`-

Python >= 3.6

`

233

``

`-

A weak set of all asynchronous generators that are

`

234

``

`-

being iterated by the loop.

`

235

``

`-

self._asyncgens = weakref.WeakSet()

`

236

``

`-

else:

`

237

``

`-

self._asyncgens = None

`

238

``

-

``

231

`+

A weak set of all asynchronous generators that are

`

``

232

`+

being iterated by the loop.

`

``

233

`+

self._asyncgens = weakref.WeakSet()

`

239

234

`` # Set to True when loop.shutdown_asyncgens is called.

``

240

235

`self._asyncgens_shutdown_called = False

`

241

236

``

`@@ -354,7 +349,7 @@ async def shutdown_asyncgens(self):

`

354

349

`"""Shutdown all active asynchronous generators."""

`

355

350

`self._asyncgens_shutdown_called = True

`

356

351

``

357

``

`-

if self._asyncgens is None or not len(self._asyncgens):

`

``

352

`+

if not len(self._asyncgens):

`

358

353

`# If Python version is <3.6 or we don't have any asynchronous

`

359

354

`# generators alive.

`

360

355

`return

`

`@@ -386,10 +381,10 @@ def run_forever(self):

`

386

381

`'Cannot run the event loop while another loop is running')

`

387

382

`self._set_coroutine_origin_tracking(self._debug)

`

388

383

`self._thread_id = threading.get_ident()

`

389

``

`-

if self._asyncgens is not None:

`

390

``

`-

old_agen_hooks = sys.get_asyncgen_hooks()

`

391

``

`-

sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,

`

392

``

`-

finalizer=self._asyncgen_finalizer_hook)

`

``

384

+

``

385

`+

old_agen_hooks = sys.get_asyncgen_hooks()

`

``

386

`+

sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,

`

``

387

`+

finalizer=self._asyncgen_finalizer_hook)

`

393

388

`try:

`

394

389

`events._set_running_loop(self)

`

395

390

`while True:

`

`@@ -401,8 +396,7 @@ def run_forever(self):

`

401

396

`self._thread_id = None

`

402

397

`events._set_running_loop(None)

`

403

398

`self._set_coroutine_origin_tracking(False)

`

404

``

`-

if self._asyncgens is not None:

`

405

``

`-

sys.set_asyncgen_hooks(*old_agen_hooks)

`

``

399

`+

sys.set_asyncgen_hooks(*old_agen_hooks)

`

406

400

``

407

401

`def run_until_complete(self, future):

`

408

402

`"""Run until the Future is done.

`

`@@ -1374,6 +1368,7 @@ def call_exception_handler(self, context):

`

1374

1368

` - 'message': Error message;

`

1375

1369

` - 'exception' (optional): Exception object;

`

1376

1370

` - 'future' (optional): Future instance;

`

``

1371

`+

`

1377

1372

` - 'handle' (optional): Handle instance;

`

1378

1373

` - 'protocol' (optional): Protocol instance;

`

1379

1374

` - 'transport' (optional): Transport instance;

`