Repository navigation
Description
Marc-Christian Schulze opened SPR-16639 and commented
I'm facing exceptions in my WebFlux Spring Boot application that are all related to the handling of multipart web requests. Once these requests hit the threshold and need to be stored temporarly in the filesystem sporadic race conditions occur.
My Dependencies:
- Spring Boot 2.0.0.RC1
- Spring 5.0.4.RELEASE
- reactor-netty 0.7.5.RELEASE
- reactor-core 3.1.5.RELEASE
- reactor-spring 1.0.1.RELEASE
Typically it fails with the reason that it was unable to create the directory /tmp/nio-stream-storage but if I look into the /tmp directory I can see that it was not present before and has been created. If I run the application again it fails sporadically while looking up the temporary files inside of the folder /tmp/nio-stream-storage:
java.lang.IllegalStateException: Unable to create the inputStream.
at org.synchronoss.cloud.nio.stream.storage.FileStreamStorage.newFileInputStream(FileStreamStorage.java:324)
at org.synchronoss.cloud.nio.stream.storage.FileStreamStorage.getInputStream(FileStreamStorage.java:245)
at org.springframework.core.io.buffer.DataBufferUtils.lambda$readInputStream$1(DataBufferUtils.java:97)
at reactor.core.publisher.FluxUsing.subscribe(FluxUsing.java:75)
at reactor.core.publisher.FluxPeekFuseable.subscribe(FluxPeekFuseable.java:86)
at reactor.core.publisher.Flux.subscribe(Flux.java:6873)
at reactor.core.publisher.FluxConcatArray$ConcatArraySubscriber.onComplete(FluxConcatArray.java:200)
at reactor.core.publisher.FluxConcatArray.subscribe(FluxConcatArray.java:80)
at reactor.core.publisher.FluxDefer.subscribe(FluxDefer.java:54)
at reactor.core.publisher.Flux.subscribe(Flux.java:6873)
at reactor.core.publisher.FluxConcatArray$ConcatArraySubscriber.onComplete(FluxConcatArray.java:200)
at reactor.core.publisher.FluxConcatArray.subscribe(FluxConcatArray.java:80)
at reactor.core.publisher.Flux.subscribe(Flux.java:6873)
at reactor.core.publisher.FluxConcatArray$ConcatArraySubscriber.onComplete(FluxConcatArray.java:200)
at reactor.core.publisher.FluxConcatArray.subscribe(FluxConcatArray.java:80)
at reactor.core.publisher.Flux.subscribe(Flux.java:6873)
at reactor.core.publisher.FluxConcatIterable$ConcatIterableSubscriber.onComplete(FluxConcatIterable.java:141)
at reactor.core.publisher.FluxConcatIterable.subscribe(FluxConcatIterable.java:60)
at reactor.core.publisher.Flux.subscribe(Flux.java:6873)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:418)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.onSubscribe(FluxConcatMap.java:210)
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:128)
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:61)
at reactor.core.publisher.FluxConcatMap.subscribe(FluxConcatMap.java:121)
at reactor.core.publisher.Flux.subscribe(Flux.java:6873)
at reactor.core.publisher.FluxConcatArray$ConcatArraySubscriber.onComplete(FluxConcatArray.java:200)
at reactor.core.publisher.FluxConcatArray.subscribe(FluxConcatArray.java:80)
at reactor.core.publisher.FluxMap.subscribe(FluxMap.java:59)
at reactor.core.publisher.FluxContextStart.subscribe(FluxContextStart.java:49)
at reactor.core.publisher.Flux.subscribe(Flux.java:6873)
at reactor.ipc.netty.channel.ChannelOperationsHandler.drain(ChannelOperationsHandler.java:461)
at reactor.ipc.netty.channel.ChannelOperationsHandler.flush(ChannelOperationsHandler.java:191)
at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:776)
at io.netty.channel.AbstractChannelHandlerContext.invokeWriteAndFlush(AbstractChannelHandlerContext.java:802)
at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:814)
at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:794)
at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:831)
at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:1049)
at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:300)
at reactor.ipc.netty.NettyOutbound.lambda$sendObject$6(NettyOutbound.java:298)
at reactor.ipc.netty.FutureMono$DeferredFutureMono.subscribe(FutureMono.java:134)
at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:148)
at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56)
at reactor.core.publisher.Mono.subscribe(Mono.java:3080)
at reactor.core.publisher.FluxConcatIterable$ConcatIterableSubscriber.onComplete(FluxConcatIterable.java:141)
at reactor.core.publisher.FluxConcatIterable.subscribe(FluxConcatIterable.java:60)
at reactor.core.publisher.MonoSourceFlux.subscribe(MonoSourceFlux.java:47)
at reactor.core.publisher.Mono.subscribe(Mono.java:3080)
at reactor.core.publisher.FluxFlatMap.trySubscribeScalarMap(FluxFlatMap.java:172)
at reactor.core.publisher.MonoFlatMap.subscribe(MonoFlatMap.java:53)
at reactor.ipc.netty.channel.ChannelOperations.applyHandler(ChannelOperations.java:380)
at reactor.ipc.netty.http.client.HttpClientOperations.onHandlerStart(HttpClientOperations.java:501)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403)
at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:304)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.FileNotFoundException: /tmp/nio-stream-storage/nio-body-1-85f003fe-a58a-4b79-8563-e4b3e6054a40.tmp (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at org.synchronoss.cloud.nio.stream.storage.NameAwarePurgableFileInputStream.<init>(NameAwarePurgableFileInputStream.java:49)
at org.synchronoss.cloud.nio.stream.storage.FileStreamStorage.newFileInputStream(FileStreamStorage.java:322)
... 56 common frames omitted
This smells like a race condition in the handling of this temporary directory and files contained in.
BTW,
Is it actually intended that WebFlux writes uploaded multipart files to disk? I thought the actual intention of being reactive is to leverage backpressure propagation.
Unfortunately, I can't provide a reproducible example.
However, reducing my code to a skeleton it would look like (basically a multi-file upload proxy):
@ResponseBody
@RequestMapping(path = "/somePath", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Flux<SomeElement> entrypoint( @RequestBody Flux<Part> parts ) {
return parts.filter(part -> FilePart.class.isInstance(part))
.cast(FilePart.class)
.flatMap(part -> {
MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.asyncPart("file", part.content(), DataBuffer.class).headers(h -> {
h.setContentDispositionFormData("file", part.filename());
h.setContentType(MediaType.APPLICATION_OCTET_STREAM);
});
return client
.post()
.uri("http://somewhere/path")
.contentType(MediaType.MULTIPART_FORM_DATA)
.syncBody(builder.build())
.retrieve()
.bodyToMono(SomeElement.class);
});
}
Affects: 5.0.4
Issue Links:
- Make WebFlux multipart support fully Reactive [SPR-17122] #21659 Make WebFlux multipart support fully Reactive
- SynchronossPartGenerator should reuse PartBodyStreamStorageFactory [SPR-16727] #21268 SynchronossPartGenerator should reuse PartBodyStreamStorageFactory
Referenced from: commits a989ea0
Activity
spring-projects-issues commentedon Mar 24, 2018
Rossen Stoyanchev commented
Since there is no repro project, can you try with Spring Framework 5.0.5.BUILD-SNAPSHOT and Reactor Netty 0.7.6.BUILD-SNAPSHOT?
spring-projects-issues commentedon Mar 24, 2018
Marc-Christian Schulze commented
I've upgraded both dependencies as you advised but the error is still there:
spring-projects-issues commentedon Mar 24, 2018
Rossen Stoyanchev commented
For the Spring Boot dependency did you really mean 2.0.0.RELEASE (2.0.0.RC1 doesn't match the rest) ?
The Synchronoss multipart library doesn't quite go as far as supporting Reactive Streams, so currently the request body is consumed without back pressure. By default it seems, part content is kept in memory only if the Content-Length < 10K, and otherwise if greater (or unknown) it is written to disk. Reading from the files is then backpressured (via DataBufferUtils) but the writing to the files is not. We could probably streamline this by plugging in a custom
StreamStoragethat exposes aFlux, and hooking in back pressure end-to-end, but that would be a separate ticket for sure (and after 5.0.x).The error suggests when it's time to read, it can't find the temp file where the part content was written. Looking at the
filefield inFileStreamStorage, it's only set once in the constructor, (probably could be final). Also each part gets its ownStreamStorageinstance, so it seems unlikely that a race condition can mix up the file name.The only remotely plausible scenarios are -- temp file name clash (one getting cleaned up first) but that also seems unlikely since it's using UUID to create a unique file name; or multipart content consumed twice, possibly indirectly, e.g. via
HiddenHttpMethodFilter(you can put a breakpoint inFileStreamStorage.getInputStream()for example to check).Can you provide some more details on how you actually run the scenario? Is it one client running in a loop and uploading, or multiple clients running concurrently and if so how many, what is the file size, and when does it start failing?
spring-projects-issues commentedon Mar 24, 2018
Marc-Christian Schulze commented
Let me check my dependencies again and also investigate which
HttpMethodFilters are registered. I'll post additional information soon.Meanwhile I changed from Jetty to Undertow checking whether this makes any difference and indeed there is one.
Undertow consistently fails, not only sporadically, with a
StackOverflowException. Well, this might be another issue but maybe it is not.Here is the stack trace:
Seems there is an infinite recursive loop when new data is available.
Referring to the theory that there are multiple reads on-going: I have to admit that indeed I'm doing some content detection:
However, I was assuming that slicing the DataBuffer does not impact the Flux (below snipper from formatDetector):
spring-projects-issues commentedon Mar 24, 2018
Rossen Stoyanchev commented
The Undertow issue means you're not really on 5.0.5 snapshots. The issue was reported and fixed recently #21088.
Slicing at the level of
part.content()should be okay.spring-projects-issues commentedon Mar 30, 2018
Marc-Christian Schulze commented
I did additional tests but no luck so far to extract a reproducible showcase.
However, while investigating I got additional information.
You are right, the issue seems to be related to multiple subscriptions happening on the same multipart.
I now observe similar behavior in other parts on my application that are not related to multipart handling.
In the other cases I return a Mono<?> from a web controller method but the client complains that the server emits more than a single element.
Looking on the network level I can see 1 HTTP Request being sent to the server and 1 HTTP Response being sent back.
However, the payload of the HTTP response contains the very same payload twice.
This is how the code looks like:
In the payload I can see 2 times the data of MyDTO although this should be impossible since a Mono and not a Flux is returned.
spring-projects-issues commentedon Mar 30, 2018
Rossen Stoyanchev commented
Very strange. Maybe debug in AbstractJackson2Encoder and/or ResponseBodyResultHandler to get more insight?
spring-projects-issues commentedon Apr 3, 2018
Marc-Christian Schulze commented
OK, not exactly what I was looking for but I found one defect in DefaultPartBodyStreamStorageFactory (Line 57).
This class is instantiated for each multipart that causes race conditions when multiple requests arrive in parallel.
Error message will be:
Could be easily fixed:
However, I'm still debugging for the second defect causing the race condition on file level.
spring-projects-issues commentedon Apr 26, 2018
Sébastien Deleuze commented
Marc-Christian Schulze Since #21268 has been fixed, could you please test again with
5.0.6.BUILD-SNAPSHOTbuilds?spring-projects-issues commentedon Jul 17, 2018
Viktor commented
I have the similar issue.
Code that can reproduce this issue if file size > 10KB(file with <=10KB don't produce this issue):
If you will remove validation part, problem will gone away.
build.gradle file:
There is a stack trace:
Maybe, I do something wrong, but how that can be fixed?
spring-projects-issues commentedon Aug 6, 2018
Rossen Stoyanchev commented
Viktor, yes a couple of things.
One, with validate the code consumes the the content twice, once inside the validate method with getByteArray and then a second time in the flatMap also with getByteArray. However you can consume it once. If you want to validate the whole content, once it's buffered into a byte[], keep it that way, adn pass it down the chain. No need to consume it again.
Effectively you want to end up with a chain that looks like this:
An additional point is that the write to the OutputStream may block, depending on the Resource type, and that's not allowed in the concurrency model for WebFlux. You can use DataBufferUtils#write to write a publisher of data buffers to an OutputStream.