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

Make setting HTTPBody.iteratorCreated thread-safe #95

Merged
merged 2 commits into from
Jan 18, 2024
Merged

Make setting HTTPBody.iteratorCreated thread-safe #95

merged 2 commits into from
Jan 18, 2024

Conversation

LarsPetersHH
Copy link
Contributor

Motivation

Fixes apple/swift-openapi-generator#502

  • Ensure thread safety of HTTPBody.collect(upTo).
  • makeAsyncIterator(): Instead of crashing, return AsyncSequence which throws TooManyIterationsError thereby honoring the contract for IterationBehavior.single (HTTPBody, MultipartBody)

Modifications

  • HTTPBody, MultipartBody: makeAsyncIterator(): removed try!, catch error and create a sequence which throws the error on iteration.
  • This removed the need for try checkIfCanCreateIterator() in HTTPBody.collect(upTo).
    Note: This creates a small change in behavior: There may be a TooManyBytesError thrown before the check for iterationBehavior. This approach uses the simplest code, IMO. If we want to keep that iterationBehavior is checked first and only after that for the length, then the code needs to be more complex.
  • Removed try checkIfCanCreateIterator() in both classes (only used in HTTPBody).

Result

Test Plan

  • Added check in Test_Body.testIterationBehavior_single() to ensure that using makeAsyncIterator() directly yields the expected error.
  • Added tests to check iteration behavior of MultipartBody.

Lars Peters added 2 commits January 17, 2024 17:40
…ead of crashing which fixes thread safety issue and potential crash in `collect(upTo:)`
@czechboy0
Copy link
Collaborator

@swift-server-bot test this please

Copy link
Collaborator

@czechboy0 czechboy0 left a comment

Choose a reason for hiding this comment

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

This is great, thank you!

@czechboy0 czechboy0 merged commit 95307ba into apple:main Jan 18, 2024
8 checks passed
@czechboy0 czechboy0 added the semver/patch No public API change. label Jan 18, 2024
@czechboy0 czechboy0 changed the title Bug/502 crash thread safety fix Make setting HTTPBody.iteratorCreated thread-safe Jan 18, 2024
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.

Potential Crash: HTTPBody.collect(upTo:) is not thread safe
2 participants