Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid terminating when a precondition is not met in HTTPServerPipelineHandler #2550

Merged
merged 4 commits into from
Oct 18, 2023

Conversation

hamzahrmalik
Copy link
Contributor

Motivation:

HTTPServerPipelineHandler has a few preconditionFailures in its state machine. This isn’t really ideal, as while these are important invariants, it is fairly easy for users to accidentally break them.

Modifications:

  1. The first time a user hits an “illegal” state transition, we fire an error down the pipeline
  2. The second time they hit one, we forcibly close the pipeline (and send another error)

We continue to crash (by calling assertionFailure) when in debug mode, to make it obvious to users that they have violated an invariant
However, an internal flag allows us to disable this behaviour for testing

Result:

Making an illegal state change in HTTPServerPipelineHandler will no longer crash

@Lukasa Lukasa added semver/minor Adds new public API. semver/patch No public API change. and removed semver/minor Adds new public API. labels Oct 13, 2023
@Lukasa
Copy link
Contributor

Lukasa commented Oct 13, 2023

@swift-server-bot add to allowlist

var file: String
var line: Int

init(base: Base, file: String, line: Int) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This, and the various vars, may as well be private.

var description: String {
switch self {
case .preconditionViolated(let message):
return message
Copy link
Member

Choose a reason for hiding this comment

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

We should add a "precondition violated" to this description as well

Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

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

Looks really good, just one very minor nit.

}

/// Calls assert if and only if `self.failOnPreconditions` is true. This allows us to avoid terminating the program in tests
private func checkPrecondition(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think a minor nit here, but let's replace the word precondition in these two functions with assertion. They only fail in debug mode so they behave like assertions, not preconditions, and it'll be a little less confusing at the call site I think.

We can keep the word precondition in the error, that works well.

Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

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

Cool, this LGTM. Nice one!

@Lukasa Lukasa enabled auto-merge (squash) October 18, 2023 12:57
@Lukasa Lukasa merged commit a41b02c into apple:main Oct 18, 2023
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants