Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-115957: Close coroutine if the TaskGroup is inactive #116009

Merged
merged 3 commits into from
Mar 6, 2024

Conversation

Jason-Y-Z
Copy link
Contributor

@Jason-Y-Z Jason-Y-Z commented Feb 27, 2024

This PR addresses the issue where when we call TaskGroup.create_task() with an inactive TaskGroup, we still get a RuntimeWarning for the coroutine. This change makes sure that the coroutine is closed in this case and updated the tests accordingly.


📚 Documentation preview 📚: https://cpython-previews--116009.org.readthedocs.build/

@arthur-tacca
Copy link

Thanks for doing this! I was dangerously close to doing it myself even though I really don't have time, just for the kudos of being a "Python contributor", so I'm really glad you did 😄

My comments:

  • You have described this as affecting "...if the task group has been closed" but it also affects task groups that have not yet been entered and those that are shutting down (but not yet closed). Perhaps the wording should be something like "when create_task is called on a TaskGroup that is not active (because it has not yet been entered, is shutting down or has closed)"
  • I could be mistaken but I think that test would pass even without your change? It looks like it tests that asyncio.TaskGroup.create_task() will raise RuntimeError if the task group has closed, but it already did that. To test the actual change it would need to check that the coroutine has been closed (by calling coro.send(None) and checking that gives RuntimeError: cannot reuse already awaited coroutine).

@Jason-Y-Z
Copy link
Contributor Author

Thanks for doing this! I was dangerously close to doing it myself even though I really don't have time, just for the kudos of being a "Python contributor", so I'm really glad you did 😄

My comments:

  • You have described this as affecting "...if the task group has been closed" but it also affects task groups that have not yet been entered and those that are shutting down (but not yet closed). Perhaps the wording should be something like "when create_task is called on a TaskGroup that is not active (because it has not yet been entered, is shutting down or has closed)"
  • I could be mistaken but I think that test would pass even without your change? It looks like it tests that asyncio.TaskGroup.create_task() will raise RuntimeError if the task group has closed, but it already did that. To test the actual change it would need to check that the coroutine has been closed (by calling coro.send(None) and checking that gives RuntimeError: cannot reuse already awaited coroutine).

Thanks for the comments Arthur! Yeah that makes sense, I will update the wording there.

Regarding the test, it raises a RuntimeWarning without the change for me. Maybe we have slightly different configs? But yeah let me try more specifically sending on the coroutine as well. Thanks again!

@arthur-tacca
Copy link

Sorry, to be honest I haven't actually run the tests (or for that matter cloned the CPython repo at all), I was just reading the test code. If the test suite automatically fails if there's a warning then you can ignore that comment.

@Jason-Y-Z Jason-Y-Z changed the title gh-115957: Close coroutine if the TaskGroup has been closed gh-115957: Close coroutine if the TaskGroup is inactive Feb 28, 2024
@Jason-Y-Z
Copy link
Contributor Author

Sorry for tagging but a gentle nudge for reviewing please 🙏 @gvanrossum

@gvanrossum
Copy link
Member

Sorry for tagging but a gentle nudge for reviewing please 🙏 @gvanrossum

Yeah, it's in my queue.

@Jason-Y-Z
Copy link
Contributor Author

Sorry for tagging but a gentle nudge for reviewing please 🙏 @gvanrossum

Yeah, it's in my queue.

Got it, thank you 🙏

@Jason-Y-Z Jason-Y-Z force-pushed the fix-issue-115957 branch 2 times, most recently from 542d7c2 to e7c3a5e Compare March 2, 2024 11:30
Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the tests you changed and added still pass even if I comment out the three coro.close() calls added to taskgroups.py. While they print the RuntimeWarning message, they don't fail. Could you try to detect the warnings in at least one of the tests?

Comment on lines 338 to 344
.. versionchanged:: 3.13

Close the given coroutine if the task group is not active.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that the convention is that a description of the new/changed behavior is added to the main text of the documentation (in this case the two lines right above) and then the versionchanged directive is used to clarify when this behavior was added or changed. In this case that would be a little redundant but I still feel it's appropriate.

For example, the main text could include information about when a task group is considered inactive (e.g. not yet entered, already finished, or in the process of shutting down).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation! I've updated the main text

@@ -185,6 +185,11 @@ Other Language Changes

(Contributed by Sebastian Pipping in :gh:`115623`.)

* When :func:`asyncio.TaskGroup.create_task` is called on an inactive
:class:`asyncio.TaskGroup`, the given coroutine will be closed (which
prevents a :exc:`RuntimeWarning`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd clarify that the RuntimeWarning is about "coroutine 'blank' was never awaited".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comments! I've updated the wording

@bedevere-app
Copy link

bedevere-app bot commented Mar 5, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@terryjreedy terryjreedy added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Mar 5, 2024
@terryjreedy terryjreedy changed the title gh-115957: Close coroutine if the TaskGroup is inactive gh-115957: Close coroutine if the TaskGroup is inactive Mar 5, 2024
@Jason-Y-Z
Copy link
Contributor Author

It looks like the tests you changed and added still pass even if I comment out the three coro.close() calls added to taskgroups.py. While they print the RuntimeWarning message, they don't fail. Could you try to detect the warnings in at least one of the tests?

Thanks for the detailed explanations @gvanrossum ! I have updated one of the tests so that it now explicitly captures and asserts no warning. I've tried it on my machine (MacOS) and it is now failing without the change. Please let me know if there is any other improvement I can apply. Thanks!

I have made the requested changes; please review again

@bedevere-app
Copy link

bedevere-app bot commented Mar 5, 2024

Thanks for making the requested changes!

@gvanrossum: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested a review from gvanrossum March 5, 2024 09:26
Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good! The tests pass in CI, one of the tests fails if I undo the changes in taskgroups.py only, so I will merge.

@gvanrossum
Copy link
Member

PS. Next time please don't force push. We squash changes when we merge into main anyway, and the force push makes it harder to re-review the code.

@gvanrossum gvanrossum merged commit ce0ae1d into python:main Mar 6, 2024
32 of 35 checks passed
@Jason-Y-Z
Copy link
Contributor Author

Thanks @gvanrossum for the review and for the note! I will remember not to force push next time 👌

adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants