Skip to content

Add dynamic port binding #10697

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

twhittock-disguise
Copy link

What do these changes do?

As per #10665 the TCPSite has a public port property which is updated with the bound port number if it was dynamically allocated

Are there changes in behavior for the user?

Users are now able to rely on the port property and name property on TCPSite having the correct port number if they were relying on port 0 dynamic allocation.

Is it a substantial burden for the maintainers to support this?

Port 0 is a standard feature in all supported OSs, and leverages the OS capabilities, rather than any specific logic in aiohttp, so the burden should be small.

Related issue number

Fixes #10665

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES/ folder
    • name it <issue_or_pr_num>.<type>.rst (e.g. 588.bugfix.rst)

    • if you don't have an issue number, change it to the pull request
      number after creating the PR

      • .bugfix: A bug fix for something the maintainers deemed an
        improper undesired behavior that got corrected to match
        pre-agreed expectations.
      • .feature: A new behavior, public APIs. That sort of stuff.
      • .deprecation: A declaration of future API removals and breaking
        changes in behavior.
      • .breaking: When something public is removed in a breaking way.
        Could be deprecated in an earlier release.
      • .doc: Notable updates to the documentation structure or build
        process.
      • .packaging: Notes for downstreams about unobvious side effects
        and tooling. Changes in the test invocation considerations and
        runtime assumptions.
      • .contrib: Stuff that affects the contributor experience. e.g.
        Running tests, building the docs, setting up the development
        environment.
      • .misc: Changes that are hard to assign to any of the above
        categories.
    • Make sure to use full sentences with correct case and punctuation,
      for example:

      Fixed issue with non-ascii contents in doctest text files
      -- by :user:`contributor-gh-handle`.

      Use the past tense or the present tense a non-imperative mood,
      referring to what's changed compared to the last released version
      of this project.

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Apr 5, 2025
@twhittock-disguise
Copy link
Author

It was unclear if & where any tests should be added.

When I attempted to build the docs locally, the reference to the new port attribute wasn't picked up properly, and I couldn't work out why.

Copy link

codspeed-hq bot commented Apr 5, 2025

CodSpeed Performance Report

Merging #10697 will not alter performance

Comparing twhittock-disguise:dynamic-ports (c042770) with master (6db713e)

Summary

✅ 51 untouched benchmarks

Copy link

codecov bot commented Apr 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.71%. Comparing base (6db713e) to head (c042770).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #10697   +/-   ##
=======================================
  Coverage   98.71%   98.71%           
=======================================
  Files         125      125           
  Lines       37445    37459   +14     
  Branches     2075     2076    +1     
=======================================
+ Hits        36963    36977   +14     
  Misses        335      335           
  Partials      147      147           
Flag Coverage Δ
CI-GHA 98.59% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.25% <100.00%> (+<0.01%) ⬆️
OS-Windows 96.19% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.36% <100.00%> (-0.01%) ⬇️
Py-3.10.11 97.27% <100.00%> (-0.01%) ⬇️
Py-3.10.16 97.82% <100.00%> (+<0.01%) ⬆️
Py-3.11.11 97.86% <100.00%> (-0.05%) ⬇️
Py-3.11.9 97.36% <100.00%> (+<0.01%) ⬆️
Py-3.12.9 98.36% <100.00%> (+<0.01%) ⬆️
Py-3.13.2 98.34% <100.00%> (+<0.01%) ⬆️
Py-3.9.13 97.14% <100.00%> (+<0.01%) ⬆️
Py-3.9.21 97.67% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.16 76.54% <21.42%> (-15.81%) ⬇️
VM-macos 97.36% <100.00%> (-0.01%) ⬇️
VM-ubuntu 98.25% <100.00%> (+<0.01%) ⬆️
VM-windows 96.19% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Dreamsorcerer Dreamsorcerer added the backport-3.12 Trigger automatic backporting to the 3.12 release branch by Patchback robot label Apr 5, 2025
@Dreamsorcerer
Copy link
Member

the reference to the new port attribute wasn't picked up properly, and I couldn't work out why.

Because it's not been added to the reference, so there's nothing to link to.

It was unclear if & where any tests should be added.

test_web_runner.py would seem like the obvious place. A test validating the port property works is probably what we want.

@twhittock-disguise
Copy link
Author

Thanks, I had thought the reference docs were generated from the class docstrings.

@twhittock-disguise
Copy link
Author

I have no clue why unrelated tests started failing / hanging after I added a test. If anyone understands this, please let me know and I'll fix it. The added test completes correctly locally, runinng all tests never completed for me locally, even when running in the master branch. Presumably because there's some part of the development setup I'm unaware of.

twhittock-disguise and others added 2 commits April 11, 2025 15:04
Co-authored-by: J. Nick Koston <nick+github@koston.org>
Copy link
Member

@webknjaz webknjaz left a comment

Choose a reason for hiding this comment

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

Some tests use a "free port" lookup helper. Now that this is implemented, it should be possible to update them too.

if self._port == 0:
# Port 0 means bind to any port, so we need to set the attribute
# to the port the server was actually bound to.
self._port = self._server.sockets[0].getsockname()[1]
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't reassign this but track the bound port separately. This is so starting and stopping the TCP site multiple times would be able to get a new port allocated each time.


await site.start()
assert site.port != 0
assert site.name.startswith("http://0.0.0.0:")
Copy link
Member

Choose a reason for hiding this comment

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

Since we know the port, this could just compare the entire string..

@@ -113,6 +113,11 @@ def name(self) -> str:
host = "0.0.0.0" if not self._host else self._host
return str(URL.build(scheme=scheme, host=host, port=self._port))

@property
def port(self) -> int:
"""Return the port number the server is bound to, useful for the dynamically allocated port (0)."""
Copy link
Member

Choose a reason for hiding this comment

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

Please, avoid having the docstring title line exceed 72 chars. You can have a longer paragraph below.
Also, referencing a magic number is not useful to readers unfamiliar with it. Use the ephemeral terminology so people could at least google it. Although, I would probably not even mention it since the underlying implementation details aren't really important to the end-users who'd just use this unconditionally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-3.12 Trigger automatic backporting to the 3.12 release branch by Patchback robot bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve support for dynamic port allocations
4 participants