This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: asyncio: document failure mode for loop.call_soon_threadsafe
Type: Stage: resolved
Components: asyncio, Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: asvetlov, docs@python, lukasz.langa, mark.dickinson, meowmeowmeowcat, miss-islington, yselivanov
Priority: normal Keywords: patch

Created on 2021-06-25 12:00 by mark.dickinson, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27688 merged meowmeowmeowcat, 2021-08-09 16:54
PR 27804 merged miss-islington, 2021-08-17 22:55
PR 27805 merged miss-islington, 2021-08-17 22:55
Messages (6)
msg396523 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-06-25 12:00
`loop.call_soon_threadsafe` raises `RuntimeError` when the event loop has been closed, but that fact doesn't seem to be documented. It would be useful to document it so that that it's clear that that behaviour is part of the API, and can be depended on.

Doc link: I'm looking at https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.call_soon_threadsafe

My use-case is that I have a background thread that's making use of `loop.call_soon_threadsafe` to place callbacks onto the main thread's event loop. The main thread at some point closes that event loop (e.g., as part of controlled application shutdown, or in the tearDown of a unit test). The background thread isn't in a position to know whether the event loop has been closed or not, and obviously checking that using the `is_closed` attribute runs into race condition issues. So I'd like to catch the potential exception from the `loop.call_soon_threadsafe`, but to do that I need to know what exception type to catch.

It would probably also make sense to document the failure mode for `loop.call_soon`.
msg399279 - (view) Author: meowmeowcat (meowmeowmeowcat) * Date: 2021-08-09 16:57
Thanks! I've opened a PR for this issue.
msg399802 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-17 22:55
New changeset 3240bc62f4e0afa09964f3afc845697f0a0806b9 by meowmeowmeowcat in branch 'main':
bpo-44508: [Doc] Document failure mode for loop.call_soon_threadsafe (GH-27688)
https://github.com/python/cpython/commit/3240bc62f4e0afa09964f3afc845697f0a0806b9
msg399806 - (view) Author: miss-islington (miss-islington) Date: 2021-08-17 23:17
New changeset bbb10761b64095425f7f8dcb3bc1f0d8bdee528a by Miss Islington (bot) in branch '3.10':
bpo-44508: [Doc] Document failure mode for loop.call_soon_threadsafe (GH-27688)
https://github.com/python/cpython/commit/bbb10761b64095425f7f8dcb3bc1f0d8bdee528a
msg399807 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-17 23:18
New changeset e2320c6a34d44a0efa7ecc693c3a6215014b7b6e by Miss Islington (bot) in branch '3.9':
bpo-44508: [Doc] Document failure mode for loop.call_soon_threadsafe (GH-27688) (GH-27805)
https://github.com/python/cpython/commit/e2320c6a34d44a0efa7ecc693c3a6215014b7b6e
msg399808 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-17 23:19
Thanks for reporting, Mark, and for the patch, meowmeowcat! ✨ 🍰 ✨
History
Date User Action Args
2022-04-11 14:59:47adminsetgithub: 88674
2021-08-17 23:19:05lukasz.langasetstatus: open -> closed
resolution: fixed
messages: + msg399808

stage: patch review -> resolved
2021-08-17 23:18:26lukasz.langasetmessages: + msg399807
2021-08-17 23:17:32miss-islingtonsetmessages: + msg399806
2021-08-17 22:55:15miss-islingtonsetpull_requests: + pull_request26271
2021-08-17 22:55:11miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26270
2021-08-17 22:55:08lukasz.langasetnosy: + lukasz.langa
messages: + msg399802
2021-08-09 16:57:05meowmeowmeowcatsetmessages: + msg399279
2021-08-09 16:54:41meowmeowmeowcatsetkeywords: + patch
nosy: + meowmeowmeowcat

pull_requests: + pull_request26174
stage: patch review
2021-06-25 12:00:15mark.dickinsoncreate