Skip to content

PEP 748: tlslib - context & socket#4960

Open
Julien00859 wants to merge 4 commits into
python:mainfrom
Julien00859:Julien00859/tlslib-context
Open

PEP 748: tlslib - context & socket#4960
Julien00859 wants to merge 4 commits into
python:mainfrom
Julien00859:Julien00859/tlslib-context

Conversation

@Julien00859
Copy link
Copy Markdown

PEP 748: create_connection and create_server instead of connect

Prior discussions at:

That commit is the solution to my struggle binding an Ipv6 localhost address when I first tested tlslib. At the time the library was always binding the socket using family=AF_INET. The solution we implemented in tlslib is to perform a DNS request on the user-provided address to determine the family to use (family=AF_INET6 in my case).

I'm still unhappy with this API server-side. "connect" is the wrong verb, it should be "bind". It is not obvious if other sockets are supported (e.g. Unix Domain Socket).

create_connection and create_server make so much more sense to me, at least as long as we don't have a wrap_socket function.

PEP 748: shutdown(show: 0|1|2) instead of close(force: bool)

Prior discussions:

Work in progress in siotls:

The messages I wrote on the forum explain the problems I have with close(force: bool), which mainly boils down to "it feels too TLS 1.2-ish". I spent many days exploring ideas to have a clean API to terminate the TLS connection a way that is safe in both TLS 1.2 (synchronous termination) and TLS 1.3 (asynchronous and IMO better). Reusing socket's shutdown terminology feels right to me. It works well for TLS 1.3 and is fine for TLS 1.2.

There are other things in the current Buffer & Socket spec that I find are leaking openssl's API, and that I expect won't work very well with other TLS libraries (including my own) but I'm not ready at this time to make new proposals.

Ease reading the diff of the next commits.
The `connect()` function is poorly named server-side as it actually
`bind()` the socket. Server-side the function lacks a `family` parameter
to support IPv6 without a DNS lookup.

Actually all three `connect()`, `bind()` and `listen()` socket function
are pretty low-level. The high-level `create_connection` (for `connect`)
and `create_server` (for `bind()` + `listen()`) are more pythonic. Wrap
the latter and not the former, also to remove the need of a `listen()`
method on the created socket (which is useless client-side).
@Julien00859 Julien00859 requested a review from ncoghlan as a code owner May 6, 2026 23:02
@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented May 6, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@read-the-docs-community
Copy link
Copy Markdown

Documentation build overview

📚 pep-previews | 🛠️ Build #32574488 | 📁 Comparing 3d40e2f against latest (c093d5f)

  🔍 Preview build  

709 files changed · ± 709 modified

± Modified

@hugovk hugovk changed the title PEP748 tlslib - context & socket PEP 748: tlslib - context & socket May 8, 2026
@hugovk
Copy link
Copy Markdown
Member

hugovk commented May 8, 2026

cc PEP authors @jvdprng and @woodruffw.

Copy link
Copy Markdown

@jvdprng jvdprng left a comment

Choose a reason for hiding this comment

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

Thanks for writing this up!

I feel like this pulls a lot of socket API surface into the PEP. Couldn't we stick to a minimum API surface that leaves a bunch of socket options to the backends? E.g., something like:

  class ClientContext(Protocol):
      def connect(
          self,
          address: tuple[str | None, int],
          *,
          server_hostname: str | None = None,
          timeout: float | None = None,
      ) -> TLSSocket: ...

  class ServerContext(Protocol):
      def create_server(
          self,
          address: tuple[str | None, int],
          *,
      ) -> TLSListener: ...

I'm not sure whether splitting out 'listening sockets' and 'connected sockets' is a good idea, but it would solve some of the current blurriness around listen/accept. In general, I'm hesitant to pull in a bunch of socket stuff without discussing this on the PEP boards first.

If we do want to go down this route, we should probably also specify in the PEP that we want to focus on INET sockets. I've left some comments regarding cleaning up types in that case.

The shutdown changes are definitely needed, but I feel like the current PR overspecifies a little.

Comment thread peps/pep-0748.rst
Comment on lines +357 to +360
timeout=GLOBAL_DEFAULT,
source_address=None,
*,
all_errors=False,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Needs typing information

Comment thread peps/pep-0748.rst
Comment on lines +396 to +399
family=AF_INET,
backlog=None,
reuse_port=False,
dualstack_ipv6=False,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Needs typing information

Comment thread peps/pep-0748.rst
self,
address: tuple[str | None, int],
*,
family=AF_INET,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we want to set AF_INET as the default here? We could have None that lets the backend choose based on the address?

Comment thread peps/pep-0748.rst

@abstractmethod
def connect(self, address: tuple[str | None, int]) -> TLSSocket:
def create_connection(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should we add a server_hostname parameter here to support 'connect by IP, verify as DNS name'?

Comment thread peps/pep-0748.rst
received from the other side.
In either case, this method should return WantWriteError if sending the
close_notify alert currently fails."""
def shutdown(self, how: Literal[0, 1, 2]) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'm worried that the current shutdown / close descriptions may be overspecified for the PEP. Should we focus on describing the observable behavior?

  • shutdown(SHUT_WR) gracefully closes the TLS sending side
  • shutdown(SHUT_RD) and shutdown(SHUT_RDWR) may discard unread peer data and are unsafe unless truncation is not an issue
  • close() should only fully close the transport when safe, otherwise it should raise WantReadError/WantWriteError as appropriate

Also, should we define a specific enum for how? Using literals might be brittle.

Comment thread peps/pep-0748.rst
backlog=None,
reuse_port=False,
dualstack_ipv6=False,
) -> TLSSocket:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we want stronger typing for the TLS sockets? Both 'listening sockets' and 'connected sockets' are still the same type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants