Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/trio/_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class NeedHandshakeError(Exception):


class _Once:
__slots__ = ("_afn", "_args", "_done", "started")
__slots__ = ("_afn", "_args", "_done", "_failure", "started")

def __init__(
self,
Expand All @@ -234,16 +234,26 @@ def __init__(
self._args = args
self.started = False
self._done = _sync.Event()
self._failure: BaseException | None = None

async def ensure(self, *, checkpoint: bool) -> None:
if not self.started:
self.started = True
await self._afn(*self._args)
try:
await self._afn(*self._args)
except BaseException as exc:
self._failure = exc
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bad idea, because a) the stack frames for exc will be mutated, so you will have weird stack traces for raise ... from self._failure and b) this will lead to a refcycle I believe.

self._done.set()
raise
self._done.set()
elif not checkpoint and self._done.is_set():
if self._failure is not None:
raise trio.BrokenResourceError from self._failure
return
else:
await self._done.wait()
if self._failure is not None:
raise trio.BrokenResourceError from self._failure

@property
def done(self) -> bool:
Expand Down
Loading