Skip to content

generic channel: Add send_warn / send_ignore helpers - #45311

Merged
jschwe merged 7 commits into
servo:mainfrom
jschwe:channel_send_ignore
Jun 14, 2026
Merged

jschwe merged 7 commits into
servo:mainfrom
jschwe:channel_send_ignore

Conversation

@jschwe

@jschwe jschwe commented Jun 2, 2026 •

Copy link
Copy Markdown
Member

As part of tackling #40744, we should reduce the amount of unwrap() we do on send / receive operations, since channel closures can happen (think e.g. the user closes a tab / window, while some request was still in-flight).
Obviously it depends on the specific channel usage if this should be expected or not, but I believe there are many cases in servo where we currently unwrap, but shouldn't be.
I'm thinking that adding helper functions like send_warn, might ease the transition, since it keeps the diff smaller and reduces the amount of boilerplate if let Err(e) .... or .inspect_err().
In cases where the channel being closed is fine send_or_ignore should probably be used, although when changing large parts of code perhaps defaulting to send_or_warn might be easier than figuring out if the warning message should be added or is just noise.

This PR is mainly a first example, to add APIs and get consuses. Porting can be done in focused follow-up PRs, perhaps as part of a less-complex issue.

Testing: This adds a helper method to ignore / log errors, which is something that can also be done manually already. Hence more of a refactor and no need for explicit tests.

///
/// In cases where channel closure is possible (because the receiver does not exist anymore),
/// this convenience method can be used to ignore the result and log the error as a warning.
fn send_warn(&self, message: T, error_context: &str) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I guess this could also be send_log if we wanted to make the log-level configurable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah that would me more evident (maybe send_with_log)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think I lean towards send_or_warn and send_or_log (if that's needed), rather than making all callers add a log level.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

i guess for now this is sufficient, and we can add something else later if we think something should be error or debug instead. Haven't seen anything yet where I felt that is necessary, but perhaps that would be the case when i start porting.

@jschwe
jschwe marked this pull request as ready for review June 3, 2026 08:42
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Jun 3, 2026
@Narfinger

Copy link
Copy Markdown
Contributor

I feel like this in general is a good idea in general and I feel like having the default be warn would be wise.
I wonder if we should, however, behave differently in some cases (perhaps debug build or similar) where we deliberately crash. Something that makes debugging easier.

@shubhamg13 shubhamg13 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thumps Up 👍

I would suggest little more evident names.

///
/// In cases where channel closure is possible (because the receiver does not exist anymore),
/// this convenience method can be used to ignore the result and log the error as a warning.
fn send_warn(&self, message: T, error_context: &str) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah that would me more evident (maybe send_with_log)

#[inline]
pub fn send_warn(&self, msg: T, error_context: &str) {
if let Err(error) = self.send(msg) {
log::warn!("{error_context}: {error}");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we use #[track_caller] we could try avoiding an error_context argument and use https://doc.rust-lang.org/stable/std/panic/struct.Location.html#method.caller to create that context instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I removed the context argument for now, since the caller should be sufficient in many cases. There are some cases where we have existing error messages with more context, but i guess we can decide that when porting if its worth preserving.

@jschwe
jschwe force-pushed the channel_send_ignore branch from 26d2a6a to 9e78b6a Compare June 13, 2026 09:42
jschwe added 4 commits June 13, 2026 11:55
Signed-off-by: Jonathan Schwender <[email protected]>
Signed-off-by: Jonathan Schwender <[email protected]>
Signed-off-by: Jonathan Schwender <[email protected]>
Signed-off-by: Jonathan Schwender <[email protected]>
@jschwe

jschwe commented Jun 13, 2026

Copy link
Copy Markdown
Member Author

Since there were no objections I guess this is ready for review now. I added oneshot and buffered channel support too, but skipped LazyCallback for now (need to think about that more).

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Jun 13, 2026
@jschwe jschwe added the S-awaiting-review There is new code that needs to be reviewed. label Jun 13, 2026
@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Jun 14, 2026
@jschwe
jschwe added this pull request to the merge queue Jun 14, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Jun 14, 2026
Merged via the queue into servo:main with commit 61a6fae Jun 14, 2026
36 checks passed
@jschwe
jschwe deleted the channel_send_ignore branch June 14, 2026 13:05
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Jun 14, 2026
TG199 pushed a commit to TG199/servo that referenced this pull request Jun 21, 2026
As part of tackling servo#40744, we should reduce the amount of `unwrap()` we
do on send / receive operations, since channel closures can happen
(think e.g. the user closes a tab / window, while some request was still
in-flight).
Obviously it depends on the specific channel usage if this should be
expected or not, but I believe there are many cases in servo where we
currently unwrap, but shouldn't be.
I'm thinking that adding helper functions like `send_warn`, might ease
the transition, since it keeps the diff smaller and reduces the amount
of boilerplate `if let Err(e) ....` or `.inspect_err()`.
In cases where the channel being closed is fine `send_or_ignore` should
probably be used, although when changing large parts of code perhaps
defaulting to `send_or_warn` might be easier than figuring out if the
warning message should be added or is just noise.

This PR is mainly a first example, to add APIs and get consuses. Porting
can be done in focused follow-up PRs, perhaps as part of a less-complex
issue.

Testing: This adds a helper method to ignore / log errors, which is
something that can also be done manually already. Hence more of a
refactor and no need for explicit tests.

---------

Signed-off-by: Jonathan Schwender <[email protected]>
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.

5 participants