-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Open
Description
vapor/Sources/Vapor/Request/Request+Body.swift
Lines 43 to 46 in e1002f3
switch self.request.bodyStorage.withLockedValue({ $0 }) { | |
case .stream(let stream): | |
return stream.consume(max: max, on: self.request.eventLoop).map { buffer in | |
self.request.bodyStorage.withLockedValue({ $0 = .collected(buffer) }) |
The linked code has an issue. It's something like
switch self.request.bodyStorage.withLockedValue({ $0 }) { // LOCK ACQUSITION
case .stream(let stream): // ACT UPON value
return stream.consume(max: max, on: self.request.eventLoop).map { buffer in
self.request.bodyStorage.withLockedValue({ $0 = .collected(buffer) }) // LOCK RE-ACQUISITON & OVERWRITE
Any code that re-acquires locks and assumes that the state underneath hasn't changed isn't quite right.
If collect()
is intended to only be run once, this should be documented. And to catch users doing a bad thing, it should probably be changed to something like
switch self.request.bodyStorage.withLockedValue({ value in let oldValue = value; value = .currentlyStreaming; return oldValue })
and then you can throw an error if the user attempts to consume the body twice whilst it's streaming.
Most importantly, we should document exactly how collect()
is supposed to be used.
Metadata
Metadata
Assignees
Labels
No labels