Skip to content

Servo should use unwrap() less #40744

Open
1 of 1 issue completed
Open

Description

@mrobinson

We should gradually phase out our use of unwrap() for graceful error handling or expect(). expect() should only be used for cases where an internal invariant has been violated. All values that go through or come from external crates should never have unwrap() or expect() called on them.

Reasoning: A web engine is an incredibly complicated piece of software that deals with unreliable, and often malicious, input. As much as possible, all operations should be considered fallible and failures handled as best as possible.

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. added theissue type on Nov 19, 2025
  2. added
    E-very-complex
    B-high-value
    on Nov 19, 2025
  3. TimvdLippe commented on Nov 20, 2025

    @TimvdLippe
    Contributor
  4. jschwe commented on Nov 20, 2025

    @jschwe
    Member

    unwrap() caused the Cloudflare outage this week: https://blog.cloudflare.com/18-november-2025-outage/

    IMHO that's not entirely fair to say, since the unwrap could have just as well been an expect, and the unwrap was quite far down the chain of issues.

    Back on topic, probably we should discuss what this means in practice, for common occurrences of unwrapping:

    • What about lock poisoning?
    • What about closed channels, if the channel was expected to live for the program lifetime?

    In both cases, servo is probably crashing already, so at the very least those places might not be the highest impact to start removing unwrap()s from.

  5. mrobinson commented on Nov 20, 2025

    @mrobinson
    MemberAuthor

    What about closed channels, if the channel was expected to live for the program lifetime?

    I think in this particular case, we need to properly handle when one process or thread panics and needs to be restarted. That's one of the big benefits of isolating things to begin with.

    I'm not too sure what we should do about lock poisoning.

  6. jdm commented on Nov 20, 2025

    @jdm
    Member

    We could switch to parking_lot everywhere which doesn't use lock poisoning.

  7. simonwuelker commented on Nov 20, 2025

    @simonwuelker
    Member

    expect() should only be used for cases where an internal invariant has been violated. All values that go through or come from external crates should never have unwrap() or expect() called on them.

    This makes sense for release builds but I think we should keep panicking if cfg!(debug_assertions) is enabled whenever possible.

  8. added
    good first issue
    on Nov 21, 2025
  9. nicoburns commented on Nov 22, 2025

    @nicoburns
    Contributor

    .expect() isn't a straightforward win. It can make bugs much harder to track down if they do occur as it doesn't produce a stack trace, there is no actual enforcement that the reason written in the expect() is valid, and it increases binary size as the assertion message is included in the final binary.

  10. EKQRCalamity commented on Nov 24, 2025

    @EKQRCalamity

    Hey, I would like to at least partially work on contributing to this issue, but had some questions before I just start with what I think is right.
    So basically my questions sum up to the following:

    • What are the factors going into the decision of keeping an unwrap call for the time being?
    • What parts of the codebase should I focus on/which are higher priority?

    Since this would be my first contribution to a big open source project, I very much hope I haven't done something outrageous by asking these questions here. If there is a more appropriate channel, I would appreciate nudging me in the right direction.

  11. atouchet commented on Nov 24, 2025

    @atouchet
    Contributor

    Are the labels on this issue correct? Both very-complex and good first issue seems a bit contradictory.

  12. removed
    good first issue
    on Nov 24, 2025
  13. 9 remaining items

  14. Taym95 commented on Nov 28, 2025

    @Taym95
    Member

    It can make bugs much harder to track down if they do occur as it doesn't produce a stack trace

    What do you mean with this? It seems to me thatexpect panics like unwrap does, and that therefore it also produces a backtrace.

    We also should be careful about blindly removing all expect/unwrap calls. In some specs you actually need to panic if you reach certain state. for example: the Streams spec follows a calling convention where ! means “unwrap” or “expect.” and and ? means result #36906 for reference.

  15. simonwuelker commented on Nov 28, 2025

    @simonwuelker
    Member

    for example: the Streams spec follows a calling convention where ! means “unwrap” or “expect.” and and ? means result #36906 for reference.

    We don't have to panic and bring down the whole process if such an assertion fails though. We could just throw an exception and abort.

  16. gterzian commented on Dec 1, 2025

    @gterzian
    Member

    We don't have to panic and bring down the whole process if such an assertion fails though. We could just throw an exception and abort.

    It depends.

    If the error was recoverable, then the spec would have been written in such a way. Having a process making essentially random progress from such a wrong state might be worse than crashing. And in the case of Servo, given its prototype development stage, we would benefit from the visibility of the crash, and the assertion that comes with it. Intermittent issues are harder to fix than crashes, because the intermittent issue requires identifying the broken invariant first, which is otherwise documented by the assertion.

  17. mrobinson commented on Dec 1, 2025

    @mrobinson
    MemberAuthor

    If the error was recoverable, then the spec would have been written in such a way. Having a process making essentially random progress from such a wrong state might be worse than crashing. And in the case of Servo, given its prototype development stage, we would benefit from the visibility of the crash, and the assertion that comes with it. Intermittent issues are harder to fix than crashes, because the intermittent issue requires identifying the broken invariant first, which is otherwise documented by the assertion.

    In this case I think a debug_assert! is the right behavior. In production mode Servo should not crash unless in some kind of unrecoverable situation, regardless of what the specification says. It's better to work outside the specification than to crash.

  18. simonwuelker commented on Dec 2, 2025

    @simonwuelker
    Member

    A pattern I've used in #40945 is

    debug_assert!(invariant());
    if !invariant() {
        // fall back gracefully
    }
  19. gterzian commented on Dec 22, 2025

    @gterzian
    Member

    Here is a real-world example: #41437

    There was an invariant in place, so expect was the right thing to do because this wasn't a recoverable error, it was something that never should happen in production. A panic would prevent the PR from merging unless it maintained invariants.

    If the expect would have been a debug assert, I doubt it would have been noticed. I'm not aware that we gate PR merges on an absence of debug asserts. Do we?

    Re the above, the reason it merged despite breaking an invariant, is because we're not testing the multiprocess path in CI.

    So I'm all for debug asserts, but only in expected cases. For example if you know that the send can fail because it is in fact normal for the receiving end to go away. See this example.

    And I'll also all for introducing runtime recovery protocols where it would be useful.

    But the point of an expect for an invariant is not to recover from it in production: it's the kind of problem that should be caught before merging a change.

  20. simonwuelker commented on Dec 22, 2025

    @simonwuelker
    Member

    If the expect would have been a debug assert, I doubt it would have been noticed. I'm not aware that we gate PR merges on an absence of debug asserts. Do we?

    debug assertions are enabled in release builds:

    servo/Cargo.toml

    Lines 233 to 235 in 55c4f57

    [profile.release]
    opt-level = 3
    debug-assertions = true

    And release builds are what try runs use by default:

    profile: str = "release"

    So CI would have failed with a debug_assert in place.

  21. gterzian commented on Dec 23, 2025

    @gterzian
    Member

    Ok, thanks for the info.

    I'd be fine with using debug_assert instead of expect then. My main point is not: let's crash Servo; it is to have strong guardrails in place against regressions.

    It seems like debug_assert could be a strong guardrail, but on the other hand it is already turned off in one place for performance reasons.

    You'd also have to ensure problems are caught in debugging efforts for existing code, which are quite often noted via an issue that someone opens following a crash.

    I think the focus should be on making efforts to fix problems in the code and ensuring invariant are maintained in the first place. There is some value in avoiding a crash in production by replacing an expect with a construct using debug_assert, but I think most value is in ensuring the code works before it ships to production. A running engine that doesn't crash but only works as intended some of the time is only marginally better than one that crashes.

    I'll start using return debug_assert!(false, "Message"); as a pattern instead of doing an expect.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-high-valueE-very-complex

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions