Commit 95307ba
Bug/502 crash thread safety fix (#95)
### 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
- No intentional crash in `makeAsyncIterator()` anymore.
- Tests supplied as example in
apple/swift-openapi-generator#502 succeed.
### 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`.
---------
Co-authored-by: Lars Peters <software@lpeters.net>1 parent 7f86e4a commit 95307ba
File tree
4 files changed
+71
-30
lines changed- Sources/OpenAPIRuntime
- Interface
- Multipart
- Tests/OpenAPIRuntimeTests/Interface
4 files changed
+71
-30
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | 162 | | |
173 | 163 | | |
174 | 164 | | |
| |||
341 | 331 | | |
342 | 332 | | |
343 | 333 | | |
| 334 | + | |
344 | 335 | | |
345 | | - | |
346 | | - | |
347 | | - | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
348 | 340 | | |
349 | 341 | | |
350 | 342 | | |
| |||
381 | 373 | | |
382 | 374 | | |
383 | 375 | | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | 376 | | |
389 | 377 | | |
390 | 378 | | |
| |||
563 | 551 | | |
564 | 552 | | |
565 | 553 | | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
566 | 557 | | |
567 | 558 | | |
568 | 559 | | |
| |||
Lines changed: 9 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
209 | 209 | | |
210 | 210 | | |
211 | 211 | | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | 212 | | |
223 | 213 | | |
224 | 214 | | |
| |||
331 | 321 | | |
332 | 322 | | |
333 | 323 | | |
| 324 | + | |
334 | 325 | | |
335 | | - | |
336 | | - | |
337 | | - | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
338 | 330 | | |
339 | 331 | | |
340 | 332 | | |
| |||
355 | 347 | | |
356 | 348 | | |
357 | 349 | | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
358 | 354 | | |
359 | 355 | | |
360 | 356 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
176 | 181 | | |
177 | 182 | | |
178 | 183 | | |
| |||
Lines changed: 49 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
0 commit comments