Skip to content

Keep EventSource alive until all subscriptions are closed#545

Merged
ranile merged 3 commits intomasterfrom
event-source-footgun-fix
Apr 1, 2026
Merged

Keep EventSource alive until all subscriptions are closed#545
ranile merged 3 commits intomasterfrom
event-source-footgun-fix

Conversation

@ranile
Copy link
Copy Markdown
Owner

@ranile ranile commented Mar 27, 2026

Update EventSourceSubscription to hold an Rc<EventSource> to prevent premature closure of the EventSource.

While I would've preferred to hold a &EventSource in EventSourceSubscription, that would force EventSourceSubscription to not be 'static, which is needed for spawn_locals and borrows would make lifetimes a nightmare to deal with

fixes #495

@ranile ranile requested a review from Madoshakalaka March 27, 2026 14:47
//!
//! // The EventSource can be dropped while subscriptions are still active.
//! // The underlying connection stays alive as long as any subscription exists.
//! drop(es);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I really don't like that this is possible at compile time but I don't see another way to fix the bigger issue of silent EventSource closures without lifetime pains

Copy link
Copy Markdown
Collaborator

@Madoshakalaka Madoshakalaka left a comment

Choose a reason for hiding this comment

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

Solid changes. However, dropping an EventSource no longer closes the connection or notifies subscribers. Users who relied on drop(es) (or letting es go out of scope) to signal teardown to subscribers now need to call es.close() instead.

This is a behavioral breaking change even though the API surface is nearly identical. It should be called out in a CHANGELOG entry, something like:

Breaking: EventSource no longer closes the underlying connection on drop. The connection now stays alive as long as any EventSourceSubscription exists. Call EventSource::close() to shut down immediately.

@ranile
Copy link
Copy Markdown
Owner Author

ranile commented Apr 1, 2026

This is a behavioral breaking change even though the API surface is nearly identical. It should be called out in a CHANGELOG entry, something like:

Breaking: EventSource no longer closes the underlying connection on drop. The connection now stays alive as long as any EventSourceSubscription exists. Call EventSource::close() to shut down immediately.

Done. The root version is not updated in the CHANGELOG as that is done by another PR

@ranile ranile requested a review from Madoshakalaka April 1, 2026 09:03
Copy link
Copy Markdown
Collaborator

@Madoshakalaka Madoshakalaka left a comment

Choose a reason for hiding this comment

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

Looking good!

@ranile ranile merged commit c85421d into master Apr 1, 2026
20 checks passed
@ranile ranile deleted the event-source-footgun-fix branch April 1, 2026 10:31
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.

Footgun in gloo-net's EventSource

2 participants