Skip to content

fix(events): ownership-safe SSE client handles (fixes #483) - #512

Merged
mathieucarbou merged 7 commits into
mainfrom
fix/483
Nov 15, 2026
Merged

mathieucarbou merged 7 commits into
mainfrom
fix/483

Conversation

@mathieucarbou

@mathieucarbou mathieucarbou commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

AsyncEventSource clients were stored as unique_ptr and exposed to applications only as raw pointers inside callbacks, making it impossible to safely target one connection later (close one session, send to a subset) without use-after-free risk.

Rework client lifetime after the design suggested in review of #483:

  • AsyncEventSourceClient now derives from enable_shared_from_this and the server stores shared_ptr handles; all AsyncTCP callbacks pin the client for the duration of the call so no callback can free the object while its frame is still live.
  • Client construction/registration moves from the constructor (where shared_from_this() is illegal) to AsyncEventSourceResponse::_respond() via _addClient(); the request is pinned for the connect callbacks.
  • New app API: onConnectSafe()/onDisconnectSafe() deliver a stable AsyncEventSourceClientPtr handle that may be stored past the callback and used as a connection identifier; onConnectWithRequest() adds the originating request for session association; getClients() returns a safe snapshot. The classic onConnect()/onDisconnect() signatures are unchanged (wrapped into the shared slot, last registered wins).
  • close() iterates a snapshot instead of the live list (fixes iterator invalidation on synchronous disconnect), ~AsyncEventSource detaches surviving clients, and an app-dropped last handle now closes the underlying connection safely.

Changed tested on a hosted build.

@mathieucarbou

Copy link
Copy Markdown
Member Author

@willmmiles I saw we still had a user report in SSE code in this PR: #483
This PR was closed because the solution wrong but I saw you left a comment on how we could fix the use reported bug.
I created this PR: it is backward compatible but provides 3 new callbacks allowing the reception of a shared client.

@mathieucarbou
mathieucarbou force-pushed the fix/483 branch 2 times, most recently from 42e87eb to 8744fa3 Compare November 13, 2026 12:16
AsyncEventSource clients were stored as unique_ptr and exposed to
applications only as raw pointers inside callbacks, making it impossible
to safely target one connection later (close one session, send to a
subset) without use-after-free risk.

Rework client lifetime after the design suggested in review of #483:
- AsyncEventSourceClient now derives from enable_shared_from_this and
  the server stores shared_ptr handles; all AsyncTCP callbacks pin the
  client for the duration of the call so no callback can free the
  object while its frame is still live.
- Client construction/registration moves from the constructor (where
  shared_from_this() is illegal) to AsyncEventSourceResponse::_respond()
  via _addClient(); the request is pinned for the connect callbacks.
- New app API: onConnectSafe()/onDisconnectSafe() deliver a stable
  AsyncEventSourceClientPtr handle that may be stored past the callback
  and used as a connection identifier; onConnectWithRequest() adds the
  originating request for session association; getClients() returns a
  safe snapshot. The classic onConnect()/onDisconnect() signatures are
  unchanged (wrapped into the shared slot, last registered wins).
- close() iterates a snapshot instead of the live list (fixes iterator
  invalidation on synchronous disconnect), ~AsyncEventSource detaches
  surviving clients, and an app-dropped last handle now closes the
  underlying connection safely.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
willmmiles and others added 6 commits November 14, 2026 14:27
…le form

The setters adapt the user's callable directly into the stored form, so it
is type-erased once rather than wrapped in a second std::function.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The AsyncClient handle could be deleted by the async thread while a user
task is calling through to it.  Keeping the object allocated ensures
that it is safe to interact with as long as the user is holding a
reference to the AsyncEventSourceClient.  The AsyncClient itself
guarantees ordering across asynchronous operations so this is always
safe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Listed first, another task could close it through getClients() or close(),
running the disconnect callback before the connect callback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mathieucarbou

Copy link
Copy Markdown
Member Author

changes approved!
@willmmiles : i will let you approve & merge ?
thanks!

@mathieucarbou mathieucarbou added Type: Improvement and removed Type: Bug Something isn't working labels Nov 14, 2026
@mathieucarbou
mathieucarbou merged commit f4169cd into main Nov 15, 2026
39 checks passed
@mathieucarbou
mathieucarbou deleted the fix/483 branch November 15, 2026 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants